Learning the vi Editor
October 5, 2021 in linux

I tried learning vi quite a few times in the last several years. Each time, I gave up after learning the basics. Being a Windows .NET developer, I just couldn’t find that many use cases for it. On the handful of occasions I found myself editing files on my MacBook, nano was good enough. So I never really learned vi.

Now that I am programming full-time on Ruby and Rails and learning Linux, I realize that vi is an essential skill to have. So I finished the Gentle Introduction to vi chapter from The Linux Command Line, and happy to report that I feel much more comfortable editing files in vi now than just a day before.

Of course, there’s a long way to go, but this book teaches you 20% of the shortcuts you’d be using 80% of the time, which is pretty neat. This post tries to summarize everything I learned, including why you should learn this editor.

Vim Editor

Why Learn vi

  • vi is always available. If you are working with a remote server, vi is guaranteed to be there.
  • vi is lightweight and fast. A skilled vi user never has to lift their fingers from the keyboard while editing.

vim is an enhanced replacement of vi that stands for vi improved, written by Bram Moolenaar.

Move Around

0 (zero)

To the beginning of the current line.

^

To the first non-whitespace character on the current line.

$

To the end of the current line.

G

To the end of the file.

gg

To the top of the file.

w

To the beginning of the next word or punctuation character.

W

To the beginning of the next word, ignoring punctuation characters.

b

To the beginning of the previous word or punctuation character.

B

To the beginning of the previous word, ignoring punctuation characters.

'n'G

To line number. For example, 1G moves to the first line of the file.

G

To the last line of the file.

Insert

i

Switch to the insert mode.

a

Append text at the cursor position.

A

Append text at the end of the line.

o

Insert a blank line below

O

Insert blank line above

Delete Text

(n)x

Delete the character the cursor is on. 3x for three characters.

dd

Delete the current line.

dW

Delete the word from the current cursor.

d$

Delete from the current cursor up to the end of the line.

d0

Delete from the cursor to the beginning.

dG

From the current line to the end of the file.

Cut, Copy, and Paste

All the d commands cut text.

(n)yy

Copy the current line. 5yy to copy the current line and next four lines.

yW

Copy the word from the cursor.

y$

Copy the line from the cursor.

yG

Copy from the line to the end of the file.

p

Paste text on the next line.

Search and Replace

fa

Searches the current line for character a and places the cursor there.

/Example

Searches for the word Example in the file. Press n to move to the next search term.

:%s/line/Line/gc

Replace all occurrences of line with Line with a confirmation.

Useful Commands

(n)u

Undo action. 5u to undo the last five actions.

J

join line, i.e. cut and paste the next line at the end of the current line.

:q

Exit vim. Press :q! to exit without saving.

:w new_file

Save as new_file.

:wq / ZZ

Save and exit.

Learning the Linux command line, like becoming an accomplished pianist, is not something that we pick up in an afternoon. It takes years of practice.

Hope it helps.