Show
The vi text editor has three modes: command, input, and ex. Command modeWhen starting, vi begins in command mode. If you are ever unsure which mode you're in, press Esc to return to command mode. In command mode, you can move around with the arrow keys, or by using the vi movement keys, as follows:
Several vi commands are listed in the table below:
Input modeThe input mode lets you insert or append text. To insert text before the cursor's current position, in command mode, press i. Similarly, to append after the cursor, you can type a. Remember that you can't move around with the cursor keys in this mode. When you're done entering text, press Esc to go back to command mode. Ex modeThe ex mode is an extension of command mode. To get into it, press Esc and then : (the colon). The cursor will go to the bottom of the screen at a colon prompt. Write your file by entering :w and quit by entering :q. You can combine these to save and exit by entering :wq. However, if you're finished with your file, it's generally more convenient to type Shift-z-z from command mode.
The lowly and now middle-aged vi editor is still a favorite among *nix system administrators and remains an essential tool for command-line file creation and editing.
Gray at the temples and in need of reading glasses, the middle-aged Vi (pronounced vee-eye) editor, released in 1976, is still a system administrator's best friend.
This advanced, yet simple to use, command-line editing program shows no sign of slowing down. You can use it to edit configuration files, create a grocery list, write a letter home to ask for money, create a new script, or even edit source code. Vi is often a symbolic link to Vim (Vi Improved) or an alias to Vim, an enhanced version of Vi. [ Get the Vim cheat sheet. ] It’s easy to invoke Vi. At the command line, you type vi <filename> to create a new file, or to edit an existing one. $ vi filename.txtVi edit modesThe Vi editor has two modes: Command and Insert. When you first open a file with Vi, you are in Command mode. Command mode means you can use keyboard keys to navigate, delete, copy, paste, and do a number of other tasks—except entering text. To enter Insert mode, press i. In Insert mode, you can enter text, use the Enter key to go to a new line, use the arrow keys to navigate text, and use vi as a free-form text editor. To return to Command mode, press the Esc key once. [ Looking for a different text editor? Download the Emacs cheat sheet. ] In Vi's Command mode, almost every letter on the keyboard has a function. To save a file, you must first be in Command mode. Press Esc to enter Command mode, and then type :wq to write and quit the file. The other, quicker option is to use the keyboard shortcut ZZ to write and quit. In Vi, write means save, and quit means exit. If you’ve made mistakes along the way while editing and want to back out (abandon) all non-saved changes, enter Command mode by pressing Esc and typing :q! This command quits without saving any changes and exits Vi. Note: Always make a copy of an existing file prior to editing with Vi or any editor. This is especially critical when editing system and configuration files. Vi shortcutsThe best way to learn Vi is to create a new file and try it out for yourself. Feel free to use the common keyboard shortcut list below to help you learn Vi’s extensive vocabulary. This list of shortcuts is by no means exhaustive, but they will enable you to edit files and learn Vi in a short amount of time.
[ Get more out of your text editor: 5 Vim features for power users. ]
Topics: Command line utilities Text editors Linux
Ken has used Red Hat Linux since 1996 and has written ebooks, whitepapers, actual books, thousands of exam review questions, and hundreds of articles on open source and other topics. Ken also has 20+ years of experience as an enterprise sysadmin with Unix, Linux, Windows, and Virtualization. More about me
Get the latest on Ansible, Red Hat Enterprise Linux, OpenShift, and more from our virtual event on demand.
OUR BEST CONTENT, DELIVERED TO YOUR INBOX
Example: > vi letter will open a new file called letter to edit, or if letter already exits, open the exiting file.
Command Mode vs. Insert ModeInsert mode is the mode to be in when inserting text into the file. Command mode is the mode to be in when giving commands which will move the cursor, delete text, copy and paste, save the file etc. When entering a file, vi is in command mode. To enter text, you must enter insert mode. If in insert mode, enter command mode by hitting the escape, <esc>, key. To insert text:
To move the cursor:You must be in Command Mode to use commands that move the cursor. Each of these commands can be preceded with a Repeat Factor. Examples:
To Delete Text:The d command removes text from the Work Buffer. The amount removed depends on the Repeat Factor and the Unit of Measure you enter after d. If you delete by mistake: give the command u (undo) immediately after you give the delete command. Examples: 3dd will delete 3 lines beginning with the current line.
Viewing Different Parts of the Work Buffer:^Character means that you should hold down the Control key while striking the indicated character key.
Yanking (copy) and Putting (paste) Text:Example: 3yy will yank (copy) 3 lines In the following list M is a Unit of Measure that you can precede with a Repeat Factor, n.
Changing TextExample: cw allows you to change a word. The word may be replaced by as many word as needed. Stop the change by hitting < esc >. Ending an Editing Session
Miscellaneous commands
Searching for an expressionWhen in command mode if your enter / the cursor will go to the bottom of the screen. Follow this with an expression and vi will find the next occurrence of that expression in the file. Example: /Bill will find the next occurrence of Bill. If this is followed by the command: n, the next occurrence of Bill is found. |