The VI editor (basic text editor in Linux) is a screen editor which is available on almost all Unix systems even in minimal installations. Start using VI and you will see how simple and easy it is. The VI tool is a command-line only editor, it has no navigation, windows, complicated logic, it only uses key management options in order to process commands.
In this article we will share all popular commands for VI.
VI has basically two modes:
1. Command mode − This mode enables you to perform administrative tasks such as saving the files, executing the commands, moving the cursor, cutting (yanking) and pasting the lines or words, as well as finding and replacing. In this mode, whatever you type is interpreted as a command.
2. Insert mode − This mode enables you to insert text into the file. Everything that’s typed in this mode is interpreted as input and placed in the file.
VI always starts in the command mode. To enter text, you must be in the insert mode for which simply type ‘i’. To come out of the insert mode, press the ‘Esc’ key, which will take you back to the command mode.
Tip: If you are not sure which mode you are in, press the Esc key twice; this will take you to the command mode. You open a file using the VI editor. Start by typing some characters and then come to the command mode to understand the difference.
VI Basic commands
|k||Move one line upwards|
|l||Move one character to the right|
|h||Move one character to the left|
|w||Move one word to the right|
|W||Move one word to the right past punctuation|
|b||Move one word to the left|
|B||Move one word to the left past punctuation|
|e||Move to the end of the current word|
|1G||Move to the beginning of the file|
|H||Move to the top of the current screen|
|M||Move to the middle of the current screen|
|L||Move to the bottom of the current screen|
|Ctrl-G||Move to the last line in the file|
|Ctrl-F||Move one screen towards the end of the file|
|Ctrl-D||Move 1/2 screen towards the end of the file|
|Ctrl-B||Move one screen towards the beginning of the file|
|Ctrl-U||Move 1/2 screen towards the beginning of the file|
|Ctrl-L||Refresh the screen|
|5G||Move to line 5 of the file (5 can be any line number)|
|/string||Find text string forward|
|?string||Find text string backward|
|n||Find forward next string instance after a string search|
|N||Find backward next string instance after a string search|
|ZZ||Save the file exit vi|
|x||Delete the character at the cursor|
|X||Delete the character behind the cursor|
|dd||Delete the line the cursor is on|
|10dd||Delete the 10 lines following the cursor|
|yy||Yank the current line|
|p||Put the yanked line below the current line|
|P||Put the yanked line above the current line|
VI commands in Command-mode
|:g/X/s//x/g||Global Search and replace (X=search object x=replace object)|
|:r file||Import a file into the current file|
|:34 r file||Import a file into the current file after line 34|
|:w||Write out the file to save changes|
|:w file||Write the file to named file|
|:wq||Save the file exit vi|
|:w!||Force save the file|
|:q!||Quit vi but don’t save changes|
VI commands in Input-mode
|a||Insert characters to the right of the cursor|
|A||Append characters to the current line|
|i||Insert characters to the left of cursor|
|I||Insert characters at the beginning of the current line|
|o||Add a new line after current line|
|O||Insert a new line above the current line|
Quitting and Saving / Unsaving in VI
The command to quit out of VI is ‘:q’. Once in the command mode, type colon ‘:’, and ‘q’, followed by return. If your file has been modified in any way, the editor will warn you of this, and not let you quit. To ignore this message, the command to quit out of VI without saving is :q!. This lets you exit vi without saving any of the changes.
The command to save the contents of the editor is :w. You can combine the above command with the quit command, or use :wq and return.
The easiest way to save your changes and exit VI is with the ZZ command. When you are in the command mode, type ZZ. The ZZ command works the same way as the :wq command.
If you want to specify/state any particular name for the file, you can do so by specifying it after the :w. For example, if you wanted to save the file you were working on as another filename called ‘my-new-file’, you would type ‘:w my-new-file’ and return.