The Vi text editor can be intimidating. But you, too, can survive Vi using this cheat sheet. While Vi and Vim isn’t technically the same thing, these commands will work in both.
Moving around Vi cheat sheet:
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. |
Vi save and exit controls cheat sheet:
General Save & Exit Controls | Command |
---|---|
Save | :w |
Save & Exit | 😡 |
Exit, No changes | :q |
Exit, Ignore changes | :q! |
Note: – Both ‘:w’ and ‘:x’ require you to have write permissions. |
Insert text and lines cheat sheet for Vi:
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. |
Vi cheat sheet to delete text and lines:
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 command 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(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.
Frequently asked questions
Vi is part of the POSIX standard. As such, it has seen numerous implementations. In fact, Vi is one of the most distributed text editors out there.
Vim in Vim editor stands for Vi IMproved. As mentioned before, Vi is part of the POSIX standard and, as seen, many implementations. As a result, Vim (or Vi IMproved) provides more features and functionality than Vi traditionally supports.
Firstly, everything that is in Vi is available in Vim. But Vim supports the following additional features: completion, spell check, comparison, Unicode, folding, among others. Moreover, Vim supports several popular programming languages (C/C++, Python, Perl, shell, etc.) for developers. More generally, Vim can edit files inside gzip, zip, tar, etc., compressed archive.
Vim editor is pre-installed in macOS and is located in /usr/bin/vim.
ESC – Terminate insert mode
u – Undo the last change
U – Undo all changes to the line
i – Insert at cursor
a – Write after the cursor
A – Write at the end of line
o – Open a new line (goes into insert mode)
dd – Delete line
D – Delete contents of the line after the cursor
dw – Delete word
x – Delete the character at the cursor
Don’t like Vi/Vim editor?
You could always install the Nano text editor and save yourself the effort of learning the 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
How to install Vim editor on Linux
So, you like Vi editor and want to try Vi IMproved (Vim)? Let’s see how you can install Vim editor.
To install Nano text editor on Debian or Ubuntu machines:
sudo apt-get install vim
To install the Nano text editor on CentOS or RHEL based platforms:
su -
yum install vim
Then, to see if Vim is correctly installed, execute the below command:
which vim
The result should show the file location of Vim if it were successful.