Mastering Vim Commands: The Ultimate List

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.

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 eye candy 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.

CommandInformation
:e myfileOpens “myfile” for editing
:wSave the file
:sav myfile.txtSaves the file as myfile.txt
😡Write changes to file and exit
:q!Quit without saving changes
:qQuit 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:

CommandInformation
/xyzSearch xyz from top to bottom
?xyzSearch xyz from bottom to top
*Search the text that is under cursor
/\ctextSearch for text (case-insensitive)
/ra[av]iSearch for raai or ravi
/abc\|xyzSearch 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/gSearch findme in all the open buffers and replace it with replaceme
:%s/x/y/gReplace all occurrences of x by y in file
:%s/qwerty/ytrewq/giReplace qwerty by ytrewq, case-insensitive
:%s/x/y/gcReplace all occurrences after confirmation
:%s/^/Begin/gReplace the beginning of each line by Begin
:%s/$/End/gReplace the ending of each line by End
:%s/x/y/giReplace x by y, case-insensitive
:%s/ *$//gSearch and delete all white spaces
:g/myname/dSearch and delete all lines containing myname
:v/myname/dDelete all lines not containing myname
:s/John/Doe/Replace the first occurrence of John by Doe in current line
:s/John/Doe/gReplace John by Doe in current line
:%s/John/Doe/gReplace John by Doe in all files
:%s/^M//gSearch and delete DOS carriage returns (^M)
:%s/\r/\r/gReplace DOS carriage returns with regular return key
:%s#<[^>]\+>##gSearch and delete HTML tags but keep text
:%s/^\(.*\)\n\1$/\1/Search and delete duplicate lines
Ctrl+aIncrements number under the cursor
Ctrl+xDecrements 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:

CommandInformation
yCopy the selected text
pPaste the selected text
ddCut the current line
yyCopy the current line
y$Copy to EOL
DCut 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.

CommandInformation
VuLowercase the entire line
VUUppercase the entire line
g~~Invert selected case
vEUSwitch selected word to uppercase
vE~Modify the current word case
ggguGSet all text to lowercase
gggUGSet all text to uppercase
:set ignorecaseIgnore case when searching text
:set smartcaseIgnore case in search, but not if an uppercase letter is used
:%s/\<./\u&/gSets the first letter of each word to uppercase
:%s/\<./\l&/gSets 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.

CommandInformation
k or Up ArrowMove the cursor position up one line
j or Down ArrowMove the cursor down one line
eMove the cursor to the end of the word
bMove the cursor to the beginning of the word
0Move the cursor to the beginning of the line
GMove the cursor to EOF
ggMove the cursor to the beginning of the file
LMove the cursor to the bottom of the screen
:80Move 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.

CommandInformation
:1,10 w myfileSaves lines 1 to 10 in myfile
:1,10 w >> myfileAppends lines 1 to 10 to myfile
:r myfileInserts the content of myfile to current file
:23r myfileInserts the content of myfile under line 23
:e .Open the File Explorer
:SexSplit window and open File Explorer
:Sex!Same as :Sex but splits window vertically
:browse eGraphical File Explorer
:lsList buffers
:cd ..Move to parent or root directory
:argsList files
:args *.phpOpen file list with .php extension
:grep something *.phpReturns list of .php files containing something
gfOpen 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.

CommandInformation
:tabnewOpens a new tab
gtGo to next tab
:tabfirstGo to first tab
:tablastGo to last tab
:tabm n(position)Rearrange open tabs
:tabdo %s/foo/bar/gExecute same command in all tabs
:tab ballPuts all open files in different tabs
:new myfile.txtEdit myfile.txt in new window
:e filenameEdit filename in current window
:split myfileSplit the window and open myfile
ctrl-w + Up arrowPuts 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 fileSplit windows vertically
:sview fileSplit windows vertically (read-only)
:hideClose current window
:­nlyClose all windows, except the current
:b 4Open tab #4 in current window

Text Alignment and Indentation

To indent or align text in Vim, following commands can be used:

CommandInformation
:set autoindentTurn on auto-indentation
:%!fmtAlign all the lines
!}fmtAlign all lines at the current position
2!!fmtAlign the next two lines
:set smartindentTurn on smart auto-indentation
:set shiftwidth=8Defines 8 spaces as indent size
ctrl-t, ctrl-dIndent 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.

CommandInformation
Ctrl+n Ctrl+pComplete the suggested word (Insert Mode)
Ctrl+x Ctrl+lComplete the suggested line
:set dictionary=enDefine en as active dictionary
Ctrl+x Ctrl+kComplete 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.

CommandInformation
:!getExecute the get Unix command, then return to Vim
!!getExecute the get Unix command and insert output in current file
:shReturn to Unix shell
$exitExit 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:

CommandInformation
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 [email protected]Define mail as abbreviation of [email protected]

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 section below!

Fancy you stumbling on my piece of the internet. Bonjour!

My name is Anmol and I'm the Blogger-In-Chief of this joint & working as the Chief Technology Officer at Azoora, Inc. I'm putting up my views here trying to help creative solopreneurs, developers & designers build their business using the power of websites, apps & social media, this, is, my jam.

If you're looking to start your own online business with a professional high quality website or mobile app, just get in touch. I'd be more than happy to assist.

SKYPE | FACEBOOK | LINKEDIN | TWITTER | EMAIL

Leave a Comment

Your email address will not be published.