Vim is among the oldest text editors that are highly popular among programmers. Launched in 1991 as an improved variant of the ‘vi’ text editor, Vim is a minimalistic text editor or source-code editor. One remarkable feature of Vim is its ability to operate from a command-line interface as well as a GUI-based application. For many coders and programmers who love to write source code using Vim, mastering Vim commands is a dream. Well, if you are also among such individuals, this write-up is for you. Here, we are going to list down almost all the Vim commands that you should know in order to make the most of the Vim text editor. However, before we rush to review the vim commands, let’s quickly take a look at some of the key reasons behind the ever-growing popularity of the Vim text editor.
Why is Vim Editor Popular?
Following are some of the key reasons behind the success and growing popularity of the Vim text editor:
- It is completely free to use and is open-source. You can easily view and examine the source code of Vim and make changes to customize Vim as per your requirements.
- There are several plugins available for Vim that can enhance its functionality. You simply need to choose the plugin that incorporates a certain functionality that you are looking for in your Vim text editor.
- Vim is lightweight and requires very few hardware resources to work efficiently. Thus, it can perform exceptionally well even in a system with limited computing resources.
- The documentation available for Vim is comprehensive and covers almost everything you need to master writing code with Vim.
- The community around Vim is large and active. Thus, you can easily get all the necessary help you need to overcome certain challenges while working with Vim.
- It supports a wide range of programming languages, including Python, Ruby, and Perl.
Popular Vim Commands
Vim is a specially designed code editor that is pretty different from many other popular text editors . One of the most interesting features of Vim is its command mode, which allows you to perform various actions in a jiffy. Also, you can input and run these commands simply by using a keyboard. Whenever you open the Vim editor, it by default enters into the command mode. However, in case the Vim editor is in some other mode and you want to switch to the command mode, you only need to hit the Escape (Esc) key. Talking about the Vim commands, they can be divided into several categories based on what they do. We are going to discuss the most common Vim commands that you should know so that you can accomplish your work faster while using the Vim text editor.
1. Basic Vim Commands
Here are some of the basic commands that every newbie to Vim editor should know.
Command | Description |
:e samplefile | Opens samplefile for editing. |
:w | Saves the file. |
:sav samplefile.txt | Saves the file as samplefile . |
:x | Writes changes to the opened file and exit. |
:q | Quits Vim. |
:q! | Quits Vim without saving changes. |
2. Navigating within an Opened File
While editing a file, you may need to navigate the cursor to different positions. The following table highlights the Vim commands that allow you to navigate the cursor throughout the file with ease:
Command | Description |
0 | Moves the cursor to the start of the line. |
$ | Moves the cursor to the end of the line. |
w | Moves the cursor to the start of the next word. |
b | Moves the cursor to the start of the previous word. |
e | Moves the cursor to the end of the word. |
k (or Up Arrow) | Moves the cursor up one line. |
j (or Down Arrow) | Moves the cursor down one line. |
H | Moves the cursor to the top of the screen. |
L | Moves the cursor to the bottom of the screen. |
M | Moves the cursor to the middle of the screen. |
h | Moves the cursor to the left by one character. |
l | Moves the cursor to the right by one character. |
gg | Moves the cursor to the beginning of the file. |
G | Moves the cursor to the end of the file. |
:28 | Moves the cursor to the 28th line of the file. |
% | Moves the cursor to the matching parenthesis. |
) | Moves the cursor to the start of the next sentence. |
( | Moves the cursor to the start of the previous sentence. |
3. Find and Replace
Find & replace is an essential feature that you will find in almost all popular text editors. Vim also lets you find or search for specific words, sentences, empty lines and also replace the occurrences of certain words. Here are the various find and replace vim commands that you need to know:
Command | Description |
/word | Finds word in the file from top to bottom. |
?word | Finds word in the file from bottom to top. |
* | Finds the word under the cursor. |
/\cword | Finds word [case-insensitive]. |
/text1\|text2 | Finds text1 or text2 . |
/a[bc]d | Finds abc or acd. |
/\<an | Finds words that start with an , such as and, ant, analyze, and so on. |
/an\> | Finds words that end with an , such as than, man, clan, ran, and so forth. |
/^\n\{2} | Finds two consecutive empty lines. |
:bufdo /searchstr/ | Searches a string in all the buffers. |
bufdo %s/word1/word2/g | Searches for word1 in all the buffers and replaces it with word2 . |
:%s/word1/word2/g | Searches for all the occurrences of word1 in the file and replaces it with word2. |
:%s/word1/word2/gi | Searches for all the occurrences of word1 in the file and replaces it with word2 (case-insensitive). |
:%s/^/Word/g | Adds Word at the start of each line. |
:%s/$/Word/g | Adds Word at the end of each line. |
:g/Word/d | Searches for lines that contain the term Word and deletes them. |
:v/myname/d | Searches for lines that do not contain the term Word and deletes them. |
:s/Word1/Word2/g | Replaces Word1 with Word2 in the current line. |
:%s/Word1/Word2/g | Replaces Word1 with Word2 in all the files. |
:s/Word1/Word2/ | Replaces the first occurrence of Word1 with Word2 in the current line. |
:%s/^\(.*\)\n\1$/\1/ | Finds and deletes all the duplicate lines within the file. |
:%s#<[^>]\+>##g | Finds and deletes all the HTML tags present in the file (and keeps text). |
4. Cut, Copy and Paste
The following table lists the commands that you can use to cut, copy, and paste certain parts of the text:
Command | Description |
y | Copies selected text. |
p | Pastes selected text. |
yy | Copies the whole line. |
dd | Cuts the whole line. |
y$ | Copies to the end of line (EOL). |
D | Cuts to the end of line (EOL). |
5. Read and Write Files
Vim comes with certain commands that allow you to manipulate the content of files and even import/export file content easily.
Command | Description |
:1,5 w samplefile | Saves lines 1 to 5 in samplefile . |
:1,5 w >> samplefile | Appends lines 1 to 5 in samplefile . |
:r samplefile | Inserts the content of the samplefile into the current file. |
:28r samplefile | Inserts the content of the samplefile at line 28 into the current file. |
6. Text Indentation and Alignment
Indentation and Alignment play a major role in making the text or code more readable. Here are vim commands that let you change the indentation and alignment of text or code.
Command | Description |
:set autoindent | Turns on the Auto Indent. |
:set shiftwidth=5 | Sets the indent size to 5 spaces. |
:set smartindent | Turns on the Smart Auto Indent. |
ctrl-t | Indents in the insert mode. |
ctrl-d | Un-indents in the insert mode. |
>> | Indents the current line. |
<< | Un-indents the current line. |
=% | Indents the code present between the parenthesis. |
1GVG= | Indents the complete file. |
:%!fmt | Aligns all the lines. |
4!!fmt | Aligns the next 4 lines. |
!}fmt | Aligns all the lines at the current position. |
7. Tabs and Windows
As Vim allows you to open and edit multiple files simultaneously, there are certain commands that you can use to manipulate tabs and change the appearance of the vim editor windows on your system.
Command | Description |
:tabnew | Creates and opens a new tab. |
:tabfirst | Shows the first tab in the stack. |
:tablast | Displays the last tab in the stack. |
gt | Shows the next tab in the stack. |
:tabm n(position) | Rearranges all the opened tabs. |
:tabdo %s/foo/bar/g | Runs one single command across all the tabs. |
:new samplefile.txt | Opens and edits the samplefile.txt file in a new window. |
:tab ball | Puts all the opened files across different tabs. |
:split samplefile | Splits the current window and open the samplefile file. |
ctrl-w_ | Maximizes the size of the current window vertically. |
ctrl-w| | Maximizes the size of the current window horizontally. |
ctrl-w= | Resizes all the windows to a common size. |
:vsplit file | Splits windows in the vertical direction. |
:sview file | Splits windows in the vertical direction (read-only). |
:nly | Closes all the windows except for the one opened currently. |
:hide | Closes the currently opened window. |
To Sum it Up
Being a Vim user, learning Vim commands is essential because it can help you speed up your work. Also, most Vim commands are easy to learn and use. So, it would be a good idea to invest some time in getting familiar with the popular Vim commands that you can use to accomplish work faster. The Vim commands discussed in this article are the ones that are popular and relevant for most Vim users. It’s good to practice these commands a few times so that you can remember their usage.
What Vim commands did you find most useful? Let us know in the comments below.
People are also reading:
Leave a Comment on this Post