Vim: The editor you need, but not the one you deserve

What is vim and why should you care?

If you have never heard of vim, you can check out vim.org but essentially it is a text editor like many others.

Vim modes:

There are 3 main modes in vim:

  1. Normal mode: Move around the text and execute commands. Vim by default starts in this mode and you get back to it from other modes by pressing ESC
  2. Insert mode: Insert/delete text. This Works similar to other text editors. To get in press i
  3. Visual mode: Select some text and then execute commands on top. To get into this press v, vim by default goes in visual mode if you select something using trackpad or mouse.

Basic commands:

As with learning any thing new you always need to know the basics. Vim has so many resources out there to learn and become better. However in this article i would make a humble attempt to walk you through some of the basic commands which i most commonly use.

Moving around:

As programmers most of the time we spend is actually navigating in a source file and understanding how the code works right? In vim, remember you do not need to use mouse or even arrows keys. We would usually use the 26 alphabet keys and some others to do most of the job.

In normal mode:h: Move left
l: Move right
j: Move down
k: Move up

Moving even faster 😉

You might be thinking thats good but do i need to always press 20 followed by some hjkl command to move to some place where i want to edit? How would i even know how many chars to end of the word where i currently am?

w - Skip to start of next word
W - Skip to next word (excluding symbols)
e - Move to end of word
E - Move to end of word (excluding symbols)
b - Move to beggining of a word in backward direction
B - Move to beggining of a word in backward dir direction (excluding symbols)
Symbols: Special characters like brackets/commas etc.
def foo(name, age):
print("My name is" + name)
print("My age is" + age)

Lets Insert some texts

So once you are able to move around in a file, the next natural thing to do is to insert some text right? Go ahead and play around a little with getting into and out of insert mode using i and ESC and enter some text

  • Press i to get into insert
  • Press arrow keys to go to end
  • Press ENTER to go to next line
  • Then type the text that you want to enter
i - insert before current cursor position
I - insert at start of line (similar to Ctrl + Left arrow key)
a - insert after current cursor position
A - insert at end of line
o - opens a new line after current line
O - opens a new line before current line

Editing:

So as coders, most of the times we need to either rename a variable or function name rather than just inserting text right?

Copy Paste (Ctrl + C/ Ctrl + V):

Below is vims way of doing copy or paste. However usually if you want to copy something you need to select it first. So to do that Visual mode comes really handy. Essentially you just need to go in visual mode v and then select the word/sentence that you want to copy using moving around commands hjkl or wWbBeE and then just press y

y - yank a.k.a copy
Y - yank current line
p - paste (after the cursor)
P - paste (before the cursor)

Cut (Ctrl + X)

Thats great, so you can copy and paste now, but if you want to delete some word or line? Use below commands.

x - delete single character (equivalent to DELETE)
X - deletes single character before current cursor position (equivalent to BACKSPACE)
d - delete based on some more modifiers (some variations below)
dw - delete word
de - delete till end of word
dE - delete excluding symbols
dW - delete to next word excluding symbolds
D or dd - delete current line

Insert or change:

If you want to just edit a single char or sentence, use below.

r - replace single character
R - replace all following characters (Enter insert mode, instead of insert it basically modifies what even is present)

Saving files/Exiting:

So till now you have been able to move in a file, able to insert or cut/paste texts and so on… How do you make sure your work is saved? 😉

:w <file_name> - save untitled file to specified name
:w - write the changes to the file
:q - quit, if there are changes since last write we would get an error.
To override this error and still quit without saving, type :q! (with exclamation)

Vim with other editors/IDE’s:

This might hurt the vim purists out there a little, but i like most coders out there use popular IDEs for their development workflows like IntelliJ for Java or Pycharm/Vscode for python. So how does this vim knowledge get applied there?

Summary:

These command above are by no means exhaustive and there is always more to learn with vim.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store