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.
|Opens samplefile for editing.
|Saves the file.
|Saves the file as samplefile .
|Writes changes to the opened file and exit.
|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:
|Moves the cursor to the start of the line.
|Moves the cursor to the end of the line.
|Moves the cursor to the start of the next word.
|Moves the cursor to the start of the previous word.
|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.
|Moves the cursor to the top of the screen.
|Moves the cursor to the bottom of the screen.
|Moves the cursor to the middle of the screen.
|Moves the cursor to the left by one character.
|Moves the cursor to the right by one character.
|Moves the cursor to the beginning of the file.
|Moves the cursor to the end of the file.
|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:
|Finds word in the file from top to bottom.
|Finds word in the file from bottom to top.
|Finds the word under the cursor.
|Finds word [case-insensitive].
|Finds text1 or text2 .
|Finds abc or acd.
|Finds words that start with an , such as and, ant, analyze, and so on.
|Finds words that end with an , such as than, man, clan, ran, and so forth.
|Finds two consecutive empty lines.
|Searches a string in all the buffers.
|Searches for word1 in all the buffers and replaces it with word2 .
|Searches for all the occurrences of word1 in the file and replaces it with word2.
|Searches for all the occurrences of word1 in the file and replaces it with word2 (case-insensitive).
|Adds Word at the start of each line.
|Adds Word at the end of each line.
|Searches for lines that contain the term Word and deletes them.
|Searches for lines that do not contain the term Word and deletes them.
|Replaces Word1 with Word2 in the current line.
|Replaces Word1 with Word2 in all the files.
|Replaces the first occurrence of Word1 with Word2 in the current line.
|Finds and deletes all the duplicate lines within the file.
|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:
|Copies selected text.
|Pastes selected text.
|Copies the whole line.
|Cuts the whole line.
|Copies to the end of line (EOL).
|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.
|:1,5 w samplefile
|Saves lines 1 to 5 in samplefile .
|:1,5 w >> samplefile
|Appends lines 1 to 5 in samplefile .
|Inserts the content of the samplefile into the current file.
|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.
|Turns on the Auto Indent.
|Sets the indent size to 5 spaces.
|Turns on the Smart Auto Indent.
|Indents in the insert mode.
|Un-indents in the insert mode.
|Indents the current line.
|Un-indents the current line.
|Indents the code present between the parenthesis.
|Indents the complete file.
|Aligns all the lines.
|Aligns the next 4 lines.
|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.
|Creates and opens a new tab.
|Shows the first tab in the stack.
|Displays the last tab in the stack.
|Shows the next tab in the stack.
|Rearranges all the opened tabs.
|Runs one single command across all the tabs.
|Opens and edits the samplefile.txt file in a new window.
|Puts all the opened files across different tabs.
|Splits the current window and open the samplefile file.
|Maximizes the size of the current window vertically.
|Maximizes the size of the current window horizontally.
|Resizes all the windows to a common size.
|Splits windows in the vertical direction.
|Splits windows in the vertical direction (read-only).
|Closes all the windows except for the one opened currently.
|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: