Basic Vi (Vim) editor commands

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.

See our handy Vim Editor guide or FAQ further below for more information.
Moving around Vi cheat sheet:
Moving AroundCommand
Move the end of the fileG
Move leftLeft Arrow Key
Move rightRight Arrow Key
Move upUp Arrow Key
Move downDown 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 ControlsCommand
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 & LinesCommand
Insert at the cursori
Insert before the lineI
Append text after the cursora
Append text after the lineA
Create a new lineo
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 LinesCommand
Delete the remainder of the line from the cursorD
Delete the current linedd
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.

  1. Press i to enter the insert mode
  2. Write any data you want to write to that file
  3. After you have written the data, press esc key to get out of the insert mode (to go back to normal mode)
  4. Press :w to save the changes without exiting from the editor window. Here, w means writing the changes to the disk
  5. 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.
  6. However, if you want to discard the changes you have made then press :q alone instead of :wq
  7. 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

What is Vi editor?

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.

What is Vim editor?

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.

What are the differences between Vi and Vim editor?

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.

Does MacOS use Vi editor?

Vim editor is pre-installed in macOS and is located in /usr/bin/vim.

What are the common Vi editor commands?

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
You may also want to read the official documentation at https://www.vim.org/docs.php.

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.

You may also be interested in

About Anto Online

Anto, a seasoned technologist with over two decades of experience, has traversed the tech landscape from Desktop Support Engineer to enterprise application consultant, specializing in AWS serverless technologies. He guides clients in leveraging serverless solutions while passionately exploring cutting-edge cloud concepts beyond his daily work. Anto's dedication to continuous learning, experimentation, and collaboration makes him a true inspiration, igniting others' interest in the transformative power of cloud computing.

View all posts by Anto Online

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.