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. Show
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 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 [ 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 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. ] To start vi: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. Which of the command in vi editor is used to switch back to the previous file?Switching Files from vi. Since switching back to the previous file is something that tends to happen a lot, you don't have to move to the ex command line to do it. The vi command ^^ (the "control" key with the caret key) will do this for you. Using this command is the same as typing :e #.
Which command is used to undo the previous steps in vi editor?Remember, yo undo a change in Vim / Vi use the command u , and to redo a change which was undone use Ctrl-R .
How do I go back to vi editor?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!
How do I revert to a previous version of vim?Press Ctrl-O to jump back to the previous (older) location. Press Ctrl-I (same as Tab) to jump forward to the next (newer) location.
|