Let’s face it! The VI text editor (also know as VIM) can be quite intimidating. This VI cheat sheet shows the most commands to help even the newest VIM user survive. This is not the full list of VI commands, but these will help you whilst you are getting familiar with them.
Moving Around | Command |
---|---|
Move the end of the file | G |
Move left | Left Arrow Key |
Move right | Right Arrow Key |
Move up | Up Arrow Key |
Move down | Down Arrow Key |
Note: – You can also use: ‘j’ for down, ‘k’ for up, ‘h’ for left and ‘l’ for right. |
General Save & Exit Controls | Command |
---|---|
Save | :w |
Save & Exit | :x |
Exit, No changes | :q |
Exit, Ignore changes | :q! |
Note: – Both ‘:w’ and ‘:x’ requires you to have write permissions. |
Insert Text & Lines | Command |
---|---|
Insert at the cursor | i |
Insert before the line | I |
Append text after the cursor | a |
Append text after the line | A |
Create a new line | o |
Note: – Each of these commands puts the vi into insert mode. – Press the <Esc> key to put vi back into command mode. – The ‘i’, ‘a’ and ‘o’ command supports undo if you press the * key. |
Delete Text and Lines | Command |
---|---|
Delete the remainder of the line, from the cursor | D |
Delete the current line | dd |
Note: – The command ‘dd’ supports undo if you press the * key. |
VI Cheat Sheet Examples
Insert data into a new or existing file
When you run the command vim filename, an editor window will open.
Follow these steps to write data into a new or existing file.
- Press i to enter the insert mode
- Write any data you want to write to that file
- After you have written the data, press esc key to get out of the insert mode (to go back to normal mode)
- Press :w to save the changes without exiting from the editor window. Here, w means writing the changes to the disk
- Press :wq to both save the changes and exit from the editor. Here, w means you want to write changes to disk and q means you want to exit from the editor window.
- However, if you want to discard the changes you have made then press :q alone instead of :wq
- If :wq gives error then press :wq!
Copy and Paste data
To copy any line, press yy by taking the cursor to any position of any line.
To paste the already copied line, press p by first going to the normal mode(by pressing esc).
Moreover, you can also copy multiple lines by pressing Nyy Where N can be any number. For instance, to copy 5 lines we can press 5yy.
Search a string
To search any word from a file, first, open the file. Make sure you are in the normal mode (by pressing Esc). Now, press / followed by the search string e.g.
/search_string
Moreover, if there are multiple occurrences of the search_string, then press n to go to the next occurrence. You can also go back to the previous occurrence by pressing shift+n.
Don’t like VI/VIM editor?
You could always install Nano text editor and save yourself the effort of learning vi editor.
To install Nano text editor on Debian or Ubuntu machines:
sudo apt-get install nano
To install the Nano text editor on CentOS or RHEL based platforms
sudo yum install nano
About the Authors
Each member of Anto's editorial team is a Cloud expert in their own right. Anto Online takes great pride in helping fellow Cloud enthusiasts. Let us know if you have an excellent idea for the next topic!
Support the Cause
Support Anto Online and buy us a coffee. Anything is possible with coffee and code.