Mastering Vim Commands: The Ultimate List
Vim is a text editor that has been around for ages and is fairly popular among the coders and advanced users. In this article, we will be taking a look at some of the most popular and useful Vim commands.
Defining itself as “the ubiquitous text editor”, Vim is not something that the average computer user is likely to run everyday. However, it has a very loyal user base and comes loaded with a good set of features. As such, it is worth the effort to take a moment and first familiarize ourselves with the Vim editor itself.
What is Vim?
In simple words, Vim is a text editor. It is the successor to the Vi editor, and has been around ever since the 1970s. In fact, for several decades, Vim was the default text editor of choice on most UNIX operating systems, including popular Linux distros and certain Apple devices.
Of late, Vim has found itself falling out of favor. This is primarily due to the advent of newer text editors to the scene, such as gedit on Linux. Plus, modern code editors such as Atom can also fill in as a text editor when needed without causing too much of a strain on memory resources. Not a Vim user? Why not check out our list of awesome Brackets extensions?
That said, Vim still continues to be a popular text editing option. It is highly configurable and easily adaptable. While Vim is probably not the most eyecandy editor out there, it makes up for its less enticing appearance by providing a wide range of features.
Consider, for example:
- Memory-persistent, multi-level undo feature
- Support for various file formats and extensions, including several programming languages
- An extensive plugin repository to get the most out of the Vim text editor
- Integration with custom tools, as well as standard text editing features such as Find and Replace.
All such features, put together, make Vim a robust and reliable text editor. This is especially why it has been around for several years now — Vim is a simple yet powerful tool that just gets the job done.
How to Work with Vim?
Now that we have established what all Vim can bring to the table, the obvious question arises — how do we get it?
For a start, the first step is to download and install Vim. The installation process is fairly straightforward. As far as the download goes, Vim is a totally free and open source software. Windows users can grab the latest version from the official website here. Mac users, on the other hand, can find the respective version from this repository.
Most Linux distros tend to bundle Vim as an alternative text editor nowadays. It might be a good idea to search for Vim in the distro’s repository itself, if your Linux machine does not have it installed already.
With that out of the way, Vim needs a fair amount of getting used to, before you can actually master its interface.
Vim has 12 different editing modes. Yes, you read that right — most editors tend to have just one mode (that is, “editing”). There are 6 basic modes, and the other six modes are just variations of the same.
The six basic Vim modes are as follows:
- Normal Mode: The default mode that is used for editor commands.
- Visual Mode: Similar to Normal Mode, but generally used to visually “select” and highlight chunks of text or code.
- Select Mode: Operates in a similar fashion to Visual Mode.
- Insert Mode: Used for editing; identical to the default workflow of modern text editors.
- Command Line Mode: As the name suggests, this is the CLI mode.
- Ex Mode: Similar to Command Line Mode, except that in Ex Mode, a single line of CLI input does not cause the command line to exit.
The Normal Mode is the default mode in which Vim starts. As such, it is also referred to as the Command Mode (not to be confused with the Command Line Mode though).
Introducing: Vim Commands
Now, as we discussed earlier in this post, Vim is not your everyday run of the mill text editor. Instead, it is more geared towards coders and power users. As such, it comes loaded with a wide array of custom keyboard commands that can make life easier for its users.
In fact, in order to truly get the max out of Vim, it is almost essential to have a certain degree of familiarity and experience with Vim commands. So let us now take a look at some of the most useful Vim commands that we can use with this awesome text editor.
However, before that, we need to ask the big question: how do we use the keyboard commands in Vim?
In order to type Vim commands, we need to be in the Command or Normal Mode. Vim starts in this mode itself by default, or we can just press the Esc key to reach Command Mode.
Once in the Command Mode, we can input any Vim command as per our needs. In order to type text, we will need to be in Insert Mode, which can be achieved by pressing the i key in the Command Mode.
And now, on to Vim commands!
Basic Vim Commands
First up, let us discuss the most basic Vim commands.
Command | Information |
---|---|
:e myfile | Opens “myfile” for editing |
:w | Save the file |
:sav myfile.txt | Saves the file as myfile.txt |
😡 | Write changes to file and exit |
:q! | Quit without saving changes |
:q | Quit Vim |
Find and Replace Vim Commands
Every text editor has the Find and Replace feature nowadays, and Vim is no exception. Here are the Vim commands for the same:
Command | Information |
---|---|
/xyz | Search xyz from top to bottom |
?xyz | Search xyz from bottom to top |
* | Search the text that is under cursor |
/\ctext | Search for text (case-insensitive) |
/ra[av]i | Search for raai or ravi |
/abc\|xyz | Search for abc or xyz |
/\<\d\d\d\d\> | Search for exactly 4 digits |
/^\n\{3} | Search for 3 empty lines |
:bufdo /searchstr/ | Search in all open files globally |
bufdo %s/findme/replaceme/g | Search findme in all the open buffers and replace it with replaceme |
:%s/x/y/g | Replace all occurrences of x by y in file |
:%s/qwerty/ytrewq/gi | Replace qwerty by ytrewq, case-insensitive |
:%s/x/y/gc | Replace all occurrences after confirmation |
:%s/^/Begin/g | Replace the beginning of each line by Begin |
:%s/$/End/g | Replace the ending of each line by End |
:%s/x/y/gi | Replace x by y, case-insensitive |
:%s/ *$//g | Search and delete all white spaces |
:g/myname/d | Search and delete all lines containing myname |
:v/myname/d | Delete all lines not containing myname |
:s/John/Doe/ | Replace the first occurrence of John by Doe in current line |
:s/John/Doe/g | Replace John by Doe in current line |
:%s/John/Doe/g | Replace John by Doe in all files |
:%s/^M//g | Search and delete DOS carriage returns (^M) |
:%s/\r/\r/g | Replace DOS carriage returns with regular return key |
:%s#<[^>]\+>##g | Search and delete HTML tags but keep text |
:%s/^\(.*\)\n\1$/\1/ | Search and delete duplicate lines |
Ctrl+a | Increments number under the cursor |
Ctrl+x | Decrements number under cursor |
Vim Commands for Cut, Copy and Paste
Just like Find and Replace, Cut, Copy and Paste too is a common feature. Vim commands to work with cut/copy and pasting of text can be found below:
Command | Information |
---|---|
y | Copy the selected text |
p | Paste the selected text |
dd | Cut the current line |
yy | Copy the current line |
y$ | Copy to EOL |
D | Cut to EOL |
Vim Commands to Change Case
These are Vim commands that can help us change the case of text, such as uppercase to lowercase, or vice versa.
Command | Information |
---|---|
Vu | Lowercase the entire line |
VU | Uppercase the entire line |
g~~ | Invert selected case |
vEU | Switch selected word to uppercase |
vE~ | Modify the current word case |
ggguG | Set all text to lowercase |
gggUG | Set all text to uppercase |
:set ignorecase | Ignore case when searching text |
:set smartcase | Ignore case in search, but not if an uppercase letter is used |
:%s/\<./\u&/g | Sets the first letter of each word to uppercase |
:%s/\<./\l&/g | Sets the first letter of each word to lowercase |
:%s/.*/\u& | Sets the first letter of each line to uppercase |
:%s/.*/\l& | Sets the first letter of each line to lowercase |
Navigation within the File
Vim Commands given in the below table help us smoothly navigate within the opened file.
Command | Information |
---|---|
k or Up Arrow | Move the cursor position up one line |
j or Down Arrow | Move the cursor down one line |
e | Move the cursor to the end of the word |
b | Move the cursor to the beginning of the word |
0 | Move the cursor to the beginning of the line |
G | Move the cursor to EOF |
gg | Move the cursor to the beginning of the file |
L | Move the cursor to the bottom of the screen |
:80 | Move the cursor to line number 80 |
% | Move the cursor to matching parenthesis |
[[ | Move the cursor to function start |
[{ | Move the cursor to block start |
File R/W and File Explorer
What good is a text editor that cannot read/write files properly? Vim comes with its own File Explorer that can help us access the files on our disk in no time. Here are some handy file-related Vim commands.
Command | Information |
---|---|
:1,10 w myfile | Saves lines 1 to 10 in myfile |
:1,10 w >> myfile | Appends lines 1 to 10 to myfile |
:r myfile | Inserts the content of myfile to current file |
:23r myfile | Inserts the content of myfile under line 23 |
:e . | Open the File Explorer |
:Sex | Split window and open File Explorer |
:Sex! | Same as :Sex but splits window vertically |
:browse e | Graphical File Explorer |
:ls | List buffers |
:cd .. | Move to parent or root directory |
:args | List files |
:args *.php | Open file list with .php extension |
:grep something *.php | Returns list of .php files containing something |
gf | Open file name under cursor |
Interface-Related Vim Commands (Tabs and Windows)
Need to split the window into two? Open a new tab or work with multiple tabs? Yes, Vim supports it all, and has custom Vim commands for such interface-related operations.
Command | Information |
---|---|
:tabnew | Opens a new tab |
gt | Go to next tab |
:tabfirst | Go to first tab |
:tablast | Go to last tab |
:tabm n(position) | Rearrange open tabs |
:tabdo %s/foo/bar/g | Execute same command in all tabs |
:tab ball | Puts all open files in different tabs |
:new myfile.txt | Edit myfile.txt in new window |
:e filename | Edit filename in current window |
:split myfile | Split the window and open myfile |
ctrl-w + Up arrow | Puts cursor in top window |
ctrl-w ctrl-w (twice) | Puts cursor in next window |
ctrl-w_ | Maximize current window vertically |
ctrl-w| | Maximize current window horizontally |
ctrl-w= | Make all windows of the same size |
100 ctrl-w+ | Add 100 lines to file in current window |
:vsplit file | Split windows vertically |
:sview file | Split windows vertically (read-only) |
:hide | Close current window |
:nly | Close all windows, except the current |
:b 4 | Open tab #4 in current window |
Text Alignment and Indentation
To indent or align text in Vim, following commands can be used:
Command | Information |
---|---|
:set autoindent | Turn on auto-indentation |
:%!fmt | Align all the lines |
!}fmt | Align all lines at the current position |
2!!fmt | Align the next two lines |
:set smartindent | Turn on smart auto-indentation |
:set shiftwidth=8 | Defines 8 spaces as indent size |
ctrl-t, ctrl-d | Indent and un-indent in Insert Mode |
>> | Indent the current line |
<< | Un-indent the current line |
=% | Indent the code between parenthesis |
1GVG= | Indent the entire file |
Autocomplete Vim Commands
To autofill or autocomplete text, Vim has a few commands of its own.
Command | Information |
---|---|
Ctrl+n Ctrl+p | Complete the suggested word (Insert Mode) |
Ctrl+x Ctrl+l | Complete the suggested line |
:set dictionary=en | Define en as active dictionary |
Ctrl+x Ctrl+k | Complete with the active dictionary |
UNIX-Only Vim Commands
Vim comes with certain commands that can be used only on a UNIX environment. This implies the following commands can be used on Linux computers, or Mac, but sadly not on Windows.
Command | Information |
---|---|
:!get | Execute the get Unix command, then return to Vim |
!!get | Execute the get Unix command and insert output in current file |
:sh | Return to Unix shell |
$exit | Exit the Unix shell and return to Vim |
It is also worth noting, by the way, that Vim is not exclusive to desktop machines. Vim can be used on any web hosting server, and be accessed via SSH as well. This is why Vim has specialized Vim commands for UNIX machines as the vast majority of web hosting servers are running Linux/UNIX operating systems.
Miscellaneous Vim Commands
And now, let us look at some misc Vim commands as well:
Command | Information |
---|---|
m {q-p} | Marks the current position as {q-p} |
‘ {q-p} | Move to position {q-p} (used after marking) |
“ | Move to previously marked position |
:ab mail hi@designbombs.com | Define mail as abbreviation of hi@designbombs.com |
Conclusion
And that brings us to the end of this post about Vim commands. If you are a Vim power-user, which Vim commands do you use the most? More importantly, which Vim commands are the most useful for saving time and boosting productivity? Share your views in the comments below!
Leave a Reply