Top Linux VIM Commands with Examples You Check in 2024 [Updated]

Are you looking to speed up/boost your productivity? Then check out these top Linux VIM Commands for helping you to achieve your goal.

Updated: 29 Jan, 24 by Antoniy Yushkevych 14 Min

List of content you will read in this article:

Before moving to know the best Linux VIM command list we have to understand the basics of vim and why you should use it. Vi Improved text editor or VIM text editor is one of the most reliable and advanced text editors available in the market. Let's dive into what is a VIM text editor? Let us study.

Commonly, the Vim text editor is also known as Vi. Initially, Bill Joy discovered this editor in the 1970s. At that time, Vi was a short form for the visual editor. Vim is an extremely fast and efficient UNIX text editor. It is also inclusive in Linux and macOS. Being a small application, it provides you with great ease of use. You can control it with the keyboard without the need for a mouse or menus. For more clarity, here is an example. 

More importantly, Vim is highly efficient. It is available on most of the major platforms, whether you are a user of Mac, Windows, or some Linux distribution. Vim makes your life simple. Even if you use ssd’ed terminal, don’t worry, Vim got you covered!

Vim is loaded with a number of impressive features. We have curated a list as stated below:

  1. Vim is persistent and a multi-level undo tree.
  2. It is an extensive plugin system.
  3. It has the ability to integrate with multiple tools.
  4. This editor is compatible with hundreds of programming languages and file formats.
  5. It is both powerful and accurate in searching and replacing text.

Being a small application, it provides you with great ease of use. You can control it with the keyboard without the need for a mouse or menus. For more clarity, here is an example.

Suppose you wish to insert some text into a particular file. To do that, you have to press I and then type. There are several key combinations that correspond to taking certain actions. Therefore, with the help of key combinations, you can execute the work.  Although Vim takes a different approach to edit the text as compared to what users expect generally. But, this is the way Unix works, in order to edit config files, scripts, changelogs, and much more. 

After understanding the basic concept of a Vim text editor, let us move to understand why you should use a vim text editor.

By default, Vim is the default editor on POSIX systems. There are instances where you are unable to access some editors due to various situations. For instance, in cases where you have just installed the OS, or performed booting on your device. Well, in that case, do not worry, you can use Vim instead of stalling your work!

Moreover, Vim is extremely efficient when it comes to design and functioning. The native interface is not dependent upon menus or some fancy peripherals. There are no extra keys too, for instance, Ctrl or Alt. Vim proves to be greatly simplified. It uses the keys that are common to any keyboard, no matter what the language, layout, or device is. 

Let us now look at several types of modes of Vim.

Modes in Vim

Vim has two modes, namely:

  • Insert mode
  • Command mode

Insert mode: With the insert mode, you can type in as you do in any regular text editor. Press I to initiate the insert mode. 

Command mode: Using this mode, you can give commands to the editor to get your work done. To initiate command mode, press ESC. 

Here are a few symbols used in command mode:

  • x: this is used to delete a character
  • A: you can append any text at the end
  • u: to undo the last command and U to undo the complete line
  • :wq : save and exit
  • :q! : you can trash all the changes 
  •  CTRL+R: to redo the command
  • 2w: move the cursor two words ahead 
  • 3e: move the cursor to the end of the third word
  • dw: to delete the word, move the cursor to the beginning of the word 
  • 0: to move to the start of the line
  • dd: to delete the line
  • d2w: this deletes two words. You can change the number from 2 to any desired one and it deletes those words
  • r: to replace a letter. For instance, use ra to replace the letter with a
  • ce: deletes the word and switches to insert mode
  • gg: takes you to the start of a file 
  • G: to move you to the bottom of a file
  • o: it opens a line below your cursor
  • O: opens a line above the cursor
  • A: it adds text after the end of a line
  • a: inserts text after the cursor
  • e: it takes you to the end of a word
  • y: copies the text
  • p: pastes the text
  • ctrl-w: switches you from one window to the other
  • R: enters replace mode till you press ESC
  • v: it initiates visual mode to select the lines and perform operations later on

Now, you may wonder how to use the Vim text editor! Well, it’s really simple. Take a look!

Vim has immense potential with regard to its simplicity and interactiveness. If you are new to this text editor, you can get a walk-through of the basics with the help of Vimtutor. Vimtutor provides you with basic lessons on how to operate Vim. 

Here are the steps that you must follow to begin your journey with Vim text editor.

Step 1: Go to the terminal and type vim.

Step 2: To initiate the insert text mode you have to press I. Here, you can type text into your document without the use of any commands.

Step 3: And if you wish to switch to the normal mode, press the Esc key. When you are operating in normal mode, here are some keys to remember for basic operations. h is for Left, k is for up, j is for down, and I is for right. 

Step 4: To save your work, type :q and to exit Vim.

You must take note of the fact that when you first launch Vim text editor, which creates and opens an empty text file. 

Your first action is to open a file or save an empty file with a specific name. You cannot directly type something in the file because the normal mode is on that issues commands to Vim and therefore if you wish to type without opening or saving a file press I on your keyboard. After doing this, you are ready to enter text in the text editor.

Now, let us understand the best Linux Vim commands with examples in detail.

We are going to cover the following vim commands under this section. Take a look:

  1. Multiple files in Vim
  2. Autocomplete text
  3. Brace Matching
  4. How to explore files in your system
  5. Split code window
  6. Re-map commands
  7. Syntax highlighting 
  8. Spell checking
  9. Delete multiple words
  10. Mark and fold code blocks
  11. Cut, Copy, Paste text
  12. Find and replace text - Substitution
  13. Check progress within a file
  14. Undo changes
  15. Add text from a different file

1. Multiple files in Vim

Programmers need to work with multiple files on a regular basis. Although there are certain keyboard shortcuts to switch to different tabs, Vim does it way better. 

Take a look at the following command:

:tabedit <tab key to scroll through files in directory>

VIM Commands

Here, you do not have to touch the mouse at all. Simple use get or gT to switch tabs. Moreover, you can also use :tabfirst and :tablast to switch back and forth from all the tabs currently open.

Additionally, if you wish to open all the files in the starting, type the following command:

vim -p source_file1.py source_file2.py source_file3.py 

2. Autocomplete text

Vim is robust enough to execute the auto-complete features without using artificial intelligence or some machine learning algorithms.

If you wish to execute auto-completion, use the following key combination and you’re good to go!

Ctrl+n

3. Brace matching

Vim makes things pretty easy for you while you’re working with complicated codes. To accomplish this task, take your cursor to the opening or closing press and enter % from your keyboard. After doing this, the cursor jumps to the matching brace. 

4. How to explore files in a new system

In Vim, you have to use two commands, 

1. Vexplore

:Vexplore

2. Sexplore

:Sexplore.

These commands help you to split your window into two sections and search your file system throughout. 

Take a look at the execution process for more clarity.

As it is evident from the snapshots that the file manager is opened and you can easily navigate through the files without leaving the page of the code file. 

Now, you have seen that navigation through the files at the same time on a single screen is possible. But, what if you wish to split the screen and edit two codes simultaneously.

Tread on!

Use the vsplit or split commands as shown below.

5.1 Vertical split

:vsplit <filename>

or

:vsp <filename>

5.2 Horizontal split

:split <filename>

or

:sp <filename>

After you execute this command the screen splits into two parts vertically as shown in the snapshot below.

There are instances when you are working with an SSH client software using the Vim commands to switch between the split-screen, it terminates the connection. 

Whereas, the text editor helps you to remap keys and avoid such conflicts. 

6. Remap commands

There are several shortcuts that can save you from putting in an immense effort. 

Here are the commands:

:map <new key-combo> <existing key-combo>

For example: 

:map <C-J> <C-W><C-J>

:map <C-K> <C-W><C-K>

:map <C-L> <C-W><C-L>

:map <C-H> <C-W><C-H>

:map l gg

This mapping helps to change the split window switching commands to Ctrl+J and Ctrl+K.

Subsequently, the last line changes the gg command to l. Therefore, if you now press I the text editor directs you to the first line. 

You can try this for as many commands as you want. You must remember that these mappings are session-based, therefore, as soon as you quit the text editor the mapping is reset. 

Furthermore, if you wish to set up the key mappings by default in the vim configuration, carry out the following step:

root@ubuntu:~# ln -s ~/code/dotfiles/vim/vimrc ~/.vimrc

7. Syntax highlighting 

In order to enable the syntax highlighting, you can add the following command. Take a look at the following snapshot for Syntax off vim and Syntax on vim respectively.

8. Spell Checking

Well, to your surprise the text editor also does spellcheck. For that, you need to set up the language first and then continue further.

Add the following command in the ~/.vimrc file.

set spell spelllang=en_us

And you’re done.

9. Delete multiple words

What if you wish to delete some words on the right of a person but not the complete sentence?

Well, to execute the same you need to follow the below-given command:

d3w #This will delete 3 words to the right 

d3b #This will delete 3 words to the left

Use this command as and when required according to your needs.

10. Mark and fold code blocks

The Vim code editor works on the basis of pattern matching. To fold the text you need to bring the cursor where you want to initiate the folding process. From there, use the letter V to begin highlighting the code, up till the point you wish to fold. After marking the code, press zf to fold. 

Now, the next question is, how to open the code after folding it? Well, to open the code, bring the cursor to the line of your folded code. Once you reach the line, press zo, here o stands for open. 

We recommend this method for small pieces of code. In case the code is large, here is another method. 

  • Move your cursor to the line where are you want to initiate the folding process of the code
  • Press m and then type any letter to name the marking. For instance, mp
  • Press zf’p

Here, p directs the fold command to search for the p mark which we used initially to highlight the code. 

11. Cut, Copy, Paste

In Vim, the copy action is known as Yank and the cut action is known as delete. While the paste option stays as it is. 

For instance, if you wish to yank some part of the text simply type y. To cut a particular text, type the letter d. Now, to paste your text, simply type p to paste the text after your cursor and P to paste before the cursor.

12. Find and replace - Substitution

To replace any desired text, use the following command:

:%s /text/paragraph /g

The above-mentioned command replaces every presence of the word ‘text’ with the word ‘paragraph’.

13. Check progress within a file

If you are in between a long file and you want to know where you are at the moment, use this command. It gives you a percentage figure of where you are presently in the file. To do this, type in the following command while you are in command mode:

Ctrl-G 

For instance, this snapshot shows that the cursor’s current position is 40% from the end of the line. 

CTRL-G

Use this command as and when required according to your needs.

14. Undo changes

To undo any changes made in the Vim text editor, simply type in the letter u. 

15. Add text from different file

The command here reads the text from another file and inserts the text right after the cursor in your current one. 

:r <file name>

This article helps you to understand the basic Linux Vim commands related to the vim text editor. therefore after reading this article you will have a great knowledge to speed up/boost up your productivity by executing these above-listed vim text editor shortcuts. There is a bundle of features and commands associated with the same. Vim has a lot for you to explore. 

We hope that the information provided in this article helps you to start the journey with Vim!

People are also reading:

Antoniy Yushkevych

Antoniy Yushkevych

Master of word when it comes to technology, internet and privacy. I'm also your usual guy that always aims for the best result and takes a skateboard to work. If you need me, you will find me at the office's Counter-Strike championships on Fridays or at a.yushkevych@monovm.com