Vim is a text editor that can greatly increase your productivity when you are coding. Typing speed is irrelevant up to a certain level. Your ability to navigate through code is much more important. This is wear Vim along with its keybindings, layout and set up can help you speed up the process.
In the beginning of using Vim you may be losing time. But don’t lose hope. This is the case of paying a small upfront cost, which will pay dividends later down the track.
Here is a post to help you get you started, or maybe to look into Vim as your chosen text editor.
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 is a stable and open-source software and it supports many different programming languages including Python and Bash.
First opening Vim
When you first open Vim, you are in command mode. The first thing to learn is how to exit, which can be done by typing
:q! with the additional
! means you quit without saving.
When you open you are initially you are in command mode. To move the cursor:
You can put a number in front of commands and it will go the 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
: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.
To actually get started typing some words of writing some code. You use:
iputs you in insert mode.
More common keys for navigation
Back in command mode, here are some more keys to help with navigation:
- We already know that
lallow us to move the cursor around.
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.
Navigating on a line:
0will take you to the start of a line; using this in combination with
0w) will take you to the first word of the line.
shift+a) will take you to the end of the current line and into insert mod
bmoves you back by one word,
Bignores punctuation and we know
wwill take you to the next word,
Wwill 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.
- 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 enterwill take us to the 42nd line of code).
/then what ever you want to search and
enterwill take you whatever you want searched. Following this,
nwill take you to the next match;
Nwill take you to the previous.
*locates all of the similar words that the cursor is at.
Simple keys for editing
Before we get into editing, a useful tool is to use visual mode:
Vputs 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.
.is particularly useful in redoing the previous command
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 the character you want to delete.
yycopies the selected line onto the clipboard
pwill paste what is on the clipboard below
ocreates a new line below and enters insert mode.
shift+o) creates a new line above and enters insert mode.
>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.
Some other useful keys
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
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.
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.
We can repeat the macro multiple times by adding a number before calling the macro (i.e. number then
@ then your assigned key).
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 you have made 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.