vim

Basic Vim Editor Commands

Vim is a popular text editor designed for efficient text editing right from the terminal. It is an advanced version of the Vi editor and is supported on different operating systems, such as macOS, BSD, Windows, and Linux. Vim is mainly known as the programmer’s tool, as it has many features that allow quick text editing.

The Vim editor community has many plugins to improve its capabilities. Many users prefer the Vim editor because of its flexibility and various features. However, there are basic Vim editor commands that everyone must know. So, let’s look at these commands and keyboard shortcuts you can use to work on the Vim editor efficiently.

Basic Shortcuts of Vim Editor

Remember, the Vim editor isn’t a pre-installed utility. Hence, we recommend reading this guide to install the Vim editor before proceeding to the next step. Now, let’s start by creating the “example.txt” file using the following command:

vim example.txt

Here, you can add the text and navigate to the lines through the below-given keys: 

Keys Description
H Move left
L Move right
K Move up
J Move down
W Go forward to the start of any word
B Go backward to the start of any word
E Go forward to the end of any word
O Move to the start of a line
$ Move to the end of a line
G Move to the last line of the whole text
gg Go to the first line of the whole text

Once you know how to navigate between lines, characters, and words, it is time to understand the keyboard shortcuts to edit the text. So here is the complete cheat sheet to edit the text using simple shortcuts in the Vim editor: 

I Key: If you want to enter text before your cursor, press the I button on the keyboard, and you will see “Insert” in the window. For example, let’s add “Important” word before the paragraph, so we have to press the I key and start typing the text:

I-key-shortcut-for-vim-editor

After inserting the text, please press Escape (Esc) key to exit the Insert mode. 

O Key: By simply pressing the O key, you can efficiently open a new line below the current line.

Shift + O Keys: It will open the new line above the current line.

A Key: Press the A key to access the Insert mode and add the text after the cursor.

Shift + A Keys: Press the Shift plus A keys to insert the text at the end of a line. 

Shift/CTRL + R Keys: The R key shortcut only replaces a single character, but if you want to replace multiple characters, press the Shift or CTRL + R keys. 

CW Shortcut: Press the C and then W keys to delete the entire word. After pressing these keys, you can enter the Insert mode to replace the whole word. 

CC Shortcut: The CC shortcut is similar to CW but will delete the entire line and let you add a new one. 

X Key: You can press the X key to delete a specific character at the cursor. However, make sure you are not in the Insert mode. 

Shift + X Keys: You can press the Shift and X keys to delete a specific line at the cursor.  

DD Shortcut: This shortcut will delete the entire line. Furthermore, if you want to delete multiple lines, press the number on the keyboard in the correct order. For example, if you want to delete five lines, press D, 5, and D in the same order.

DW Shortcut: The DW shortcut will delete the entire word.

YW Shortcut: This shortcut is called Yank, and it copies a word. 

YY Shortcut: This shortcut will copy a line.

P Key: It will paste the text at the end of the particular paragraph.

Shift + P Keys: The text will be pasted into the paragraph above.

Essential Commands of Vim Editor

:set nu: You can use this command to display the line number in the editor:

The-set-nu-command-in-vim-editor

:set nu!: Similarly, you can remove the line numbers.

:{line number}: Use this command when you want to go to a specific line number. For instance, type 17  to redirect to the  17th line.

line-number-in-vim-editor

:set mouse=a: This command will enable the mouse, and you can use it to copy and paste text. Moreover, you can also select the specific text and replace it with something else: 

:%s/old/new/g: You can use this command to replace a specific word with another. For example, let’s replace the word “Linux” with “OS” using the following command:

:%s/Linux/OS/g

the-substitute-command-in-vim-editor
In the above command, s stands for “substitute,” and % specifies the number of lines the command should implement the change. That’s why, if you want to replace the word in all the lines, use % or specify the line number instead. Moreover, g is “global.” which replaces all occurrences of old in the complete document.

Additional Shortcuts

Now, you can use a few additional shortcuts to find the texts and save the text file in the Vim text editor.

/<text>: Press the / key and enter the text you want to search. This command will perform the forward search.

?<text>: Similarly, you can use ?<text> to perform a backward search.

:w: You can use this command to write or save the file without exiting the Vim editor.

:wq: If you want to save and quit the file in the Vim editor, please use the :wq command.

:q: If you want to quit the text editor, please use the :q command, but save the file first, or it will show you the error.

:q!: Finally, if you want to force quit and discard unsaved changes, please use :q! command.

Wrapping Up

So this was the complete guide about the basic vim editor commands you need to know to become an expert. You can use the Vim text editor to perform editing tasks and enhance productivity efficiently. Hence, we recommend learning these shortcuts and commands if you frequently use the Vim editor in terminal environments.

About the author

Prateek Jangid