DEV Community

Cover image for 4 levels of vim editor commands
Vishal Chovatiya
Vishal Chovatiya

Posted on • Edited on • Originally published at vishalchovatiya.com

4 levels of vim editor commands

This article will not make you an expert in very less time and learning vim editor commands will be painful at first. So, It will take time and It will be a lot like playing a musical instrument. Also, don't expect to be more efficient with vim than any another editor in less than 3 days. In fact, it will certainly take weeks instead of 3 days. Vim editor commands are hard to learn but incredible to use.

/!\: Originally published @ www.vishalchovatiya.com.

Brief on learning vim editor commands

I have designed this article in different four levels, so you won't lose enthusiasm & step up slowly and I suggest you start slow with level 1.

Level 1: Beginner
Level 2: Feel Comfortable
Level 3: Better, Stronger, Faster
Level 4: Feel the power in your fingers

Level 1: Beginner

  • Open vim editor by typing vim filename (i.e. vim main.c).
  • In a standard editor, typing on the keyboard is enough to write something and see it on the screen, but not this time. Because vim is in normal mode.
  • To switch to insert mode, you need to type the letter i. As a result, you can type letters like in a standard editor.
  • After that, to get back to normal mode just press the ESC key.
  • By above all, you know how to switch between insert and normal mode. And, here are the commands that you need in order to survive in normal mode:
Command Detail
p Paste
l Insert mode. Type ESC to return to Normal mode.
hjkl cursor move (←↓↑). Hint: j looks like a down arrow.
dd (and copy) the current line
:wq Save and Quit (:w save, :q quit)
:help Show help about.

Level 2: Feel comfortable 

Basic Moves

Command Detail
o Go to the first column
$ Go to the end of line
:/pattern Search for a pattern in the current file
gg Go to the start of the file
G Go to the last line

Load / Save / Quit file

Command Detail
:w Save current file
:saveas Save to
:x, ZZ or :wq Save and quit (:x only save if necessary)
:q! Quit without saving, also:qa! to quit even if there are modified hidden buffers

Undo / Redo

Command Detail
u Undo
Ctrl+r Redo

Copy / Paste

Command Detail
p paste before, remember p is paste after the current position.
yy copy the current line, easier but equivalent to ddP
v Start visual selection from the current cursor position (Mostly used to copy multiple lines by select area press yy and then paste by pressing p)

Level 3: Better, Stronger, Faster

Put the line numbers in each line (Better)

  • :set number

Copy/Paste multiple lines (Stronger)

  • Firstly, go to address where you want to start copy.
  • Then, go to normal mode by pressing ESC and then v (visual selection).
  • After that, Go up and down until copy area complete.
  • Then, press yy which is a copy(yank) the selected area(press y key twice).
  • After that, Go with the cursor where you want to paste copied data and press p.

Vim find and replace

 :%s/text to search for/text to replace/g

Search text occurrences in a file and replace all with the text given.

Auto detecting/completing words (Faster)

  • In Insert mode, just type the start of a word, then type Ctrl+p, just see the magic…

Use mouse in vim

  • :set mouse=a

Level 4: Feel the power in your fingers

This level will take you to a high of vim. When you master all following vim editor commands, you will be able to increase your productivity and I am sure you would never use any other editor than Vim.

Open the file and go to a specific function or line number

1. Go to a specific line number, vim fileName +LineNumber i.e. vim main.c +3
2. Go to a specific function, vim filename +/pattern i.e. vim main.c +/main
3. Go to a specific line number, if your file is already opened i.e. :linenumber
For example, In normal mode type ':42' and press enter, you will be at line number 42 location.

Commenting or editing multiple lines in a single stroke

Firstly, Go to Normal mode by pressing ESC key.
Then, Press Ctrl+v (visual block selection) from where you want to edit.
After that, Move cursor up and down to a number of the line you want to edit and press I//.
Finally, Press ESC again and wait for a second it will comment all the selected line or you can say add // in front of each selected line.

Typically: ESC, Ctrl+v,  or  , I, <PatternToAdd>, ESC

Align code in curly braces (Indent)

Firstly, Go inside the curly braces in which you want to align code properly.
Then, Go to Normal mode by pressing ESC then press =.
After that, press i.
Finally, To see the magic press either { or }.

Typically: ESC, =, i, {

Working with multiple files in a single screen

Command Detail
:vs filename Split window vertically
:sp filename Split window horizontally

Note:- use Ctrl+w to shuffle between two opened section in normal mode.

Bonus point

Top comments (2)

Collapse
 
moopet profile image
Ben Sinclair

Level 5: beginner.
;)

Collapse
 
vlasales profile image
Vlastimil Pospichal

Level 6 :)