When we think about programming efficiently, we think about how fast we can type on a keyboard.
This is true to a certain point. But it comes to a point where the faster you type doesn’t improve your ability to program. What now becomes more important is your ability to reduce the number of keystrokes, navigate through your code, as well as remain in that all important flow.
That’s where Vim and its keybindings can greatly improve your productivity as a programmer, and that is what I will hopefully cover in this post.
As a quick note: in the beginning, learning something takes time. There is a small cost to pay. Aren’t we trying to save time? Yes, we are but you are paying a small upfront cost for what will pay you many dividends down the track later.
Update in 2022
I have now switched to VSCode for the vast extension and support for extensions, but I will use Vim for quickly accessing and editing files. I also use Vim for writing draft posts as its distraction free. I do use Vim keybinding, which we will learn about soon in VSCode.
Update #2: I have switched again from VSCode to NeoVim.
What is Vim?
Vim is a highly configurable text editor built to make creating and changing any kind of text very efficient.
From the Vim webpage
Vim (or Vi IMproved) is a stable and open-source text editor that runs on the terminal. It supports many different programming languages like Python and C++.
When first opening Vim, we are met with a pleasant welcome page.
A common gag for first time Vim users is how to close the application. That’s the first thing we need to learn. Firstly, when we open Vim, we are in “Command Mode”. That means we can write commands, and the command we use to quit Vim is
:q!. The additional
! means we will quit without saving.
Let’s move onto some more practical stuff. Movement!
Imagine we have a large body of text to move around in, we move the cursor using:
You can put a number in front of commands and it will do that command that number of times. (e.g.
20h will take your cursor to the left 20 times).
One of the first things to learn it how to exit Vim. This can be done:
:qThis is the most important being able to quit, following this with an ! such as :q! allows you to quit without saving
:wwill save your created file
:wqwill save and quit in one commands
:xwill save and quit as well
Moving to start and end:
G(note that this
shift+g) takes you to the start of the bottom line
ggtakes you to the top of the code
If we are at the top and we typing in a number then enter, this will take us to the that particular line of code (e.g.
gg42 enter will take us to the 42nd line of code)
Navigating on a line:
0will take you to the start of a line; using this in combination with w (so 0w) will take you to the first word of the line.
shift+^will move you to the start of the line as an alternative to
shift+$will move you to the end of the line. This can be used in visual mode (press
vin command mode and this will allow you to selected text).
bmoves you back by one word, B ignores punctuation and we know w will take you to the next word, W will ignore punctuation.
More ways of gross navigation:
}navigates by blocks of code
%will take you back and forth between parenthesis, curly braces or square brackets
Keeping the cursor in the same position, but being able to move the window is useful as well:
zzwill move the window to the cursor becomes the center of the screen.
ctrl+ywill move the window up by one line.
ctrl+ewill move the window down by one line.
Some more useful key combinations to help with deleting and inserting:
dwwill delete a word but remain in command mode.
ciwwill delete a word and put into insert mode.
ci)will delete a word in a parenthesis and put into insert mode.
ca)will delete a word and surrounding parenthesis and put into insert mode.
/then what ever you want to search and enter will take you whatever you want searched. Following this, n will take you to the next match; N will take you to the previous
/sallows replacement of searchable text (e.g.
/s/<text>/<replace with>). This will work for one match, for all matches in a line suffix this with
*locates all of the similar words that the cursor is at
While in visual mode (press
v when in command mode) you can make movements:
Some of the above commands will actually take you from “Command Mode” into “Insert Mode”, but the way to get into “Insert Mode” is by using the
shift+i) will take you to the start of the current line and then insert mode.
shift+a) will take you to the end of the current line and into insert mode.
ocreates a new line below and enters insert mode
O(shift+o) creates a new line above and enters insert mode
A useful combination for selecting words:
viwwill select a word, no matter where you are in the word.
vi)will insert whatever is inside the next set of parenthesis.
Before we get into editing, a useful tool is to use visual mode, which we can get into by using
shift+v) puts you into visual mode. This allows you to select code using your navigation keys. This comes in handy if you want to see what code you will be manipulating.
Undo and redo:
uwill undo (can repeat)
ctrl+rwill redo (can repeat)
Deleting, copying and pasting:
dddeletes the line you are on and this also copies it to clipboard so its like using ‘cut’
Ddeletes from where the cursor is to the end.
ctand then whatever character will delete to the where you are to the character you want to delete.
yycopies the selected line onto the clipboard
pwill paste what is on the clipboard below
More useful ways of copying:
yiwwill copy a selected word.
>can be used to indent you code. You can use this in combination with visual mode to select and indent your code. Using . will allow you to repeat this action. If you use a number in front, then it will indent the number of lines below your selection.
>>is useful when you are in command mode.
Here are some other useful keys and settings that can help you:
~will swap the case of the character (i.e. make a lower case to upper and vice-versa).
:set numberwill provide lines numbers
.is particularly useful in redoing the previous command
shift+J) removes a line breaks.
Creating a macro
A macro a set of commands that you can map to any key. This is extremely powerful if you need to do a set of commands multiple times. You can set this to any key you want as well.
Here is the process:
qand then any key after (e.g.
qw) is used to record the macro and set it to that particular key (e.g.
We perform our commands, entering in and out of command, insert, visual mode etc.
We can replay our macro using
@and then the key we used to save the macro, which was
We can repeat the macro multiple times by adding a number before calling the macro (i.e. number then
@ then your assigned key, e.g.
Now it’s your turn!
Get Vim on your system.
Make a simple program using Vim. Or play around with the commands with some code or text you have already. It will feel awkward at first, but it will get better as you progress.
So, I’m hoping this helps you out. How did you find it? What other keys did you discover useful that I haven’t listed here? What were useless? Please comment below and share this with others who might find this useful.