Vi basics

Introduction

Vi is a terminal based editor. Vi's strength lies in its keyboard centric usage. It is so powerful that you can navigate and edit huge files with few keyboard shortcuts and no mouse. And, even if you do not use terminal based applications, learning vi basics can still help you give a new perspective to keyboard based workflow efficiency.

Major advantages of vi

  1. Vi comes preinstalled on most of the unix systems (including servers).
  2. Similar keyboard shortcuts are generally available or extendable in other applications.

Lesson 00: Open and close files

Open a file with the command:

vi filename

Press <Esc>:q!<CR> to exit the application. Explanation for the keys are given in the below lessons.

Lesson 01: Modes

Vi tries to perform actions with minimal key presses. This is possible through the concept of modes. Vi has multiple modes with each mode for a specific task. The modes are as follows:

Normal mode: It is the default mode set when a file is opened. You can navigate through the files and edit text data in this mode. Press <Esc> to switch to Normal mode.

Insert mode: In this mode, you can type out text data. Press i in Normal mode to switch to Insert mode. -- Insert -- can be seen at the bottom left in this mode.

Visual mode: Visual mode helps to see data being selected. Press v in Normal mode to switch to Visual mode. -- Visual -- can be seen at the bottom left in this mode.

Lesson 02: Basic motions key bindings

These are the basic motions key bindings. Use these keys in Normal mode.

In the table below, [n] refers to number. It is optional with default value being 1.

key action
[n]h move left by n characters
[n]l move right by n characters
[n]j move down by n lines
[n]k move up by n lines
gg move to the top of the file
G move to the bottom of the file
^ move to the start of the line
$ move to the end of the line
[n]w move to the start of the next n words
[n]b move to the start of the previous n words
[n]e move to the end of the next n words

Lesson 03: Editing key bindings

These keys bindings are for editing and should be used in Normal mode.

In the table below, [m] refers to any of the motions mentioned in lesson 02.

key action
d[m] delete by motion
c[m] change / update by motion
y[m] yanks / copy by motion
p[m] paste by motion
x delete the character under cursor
yy yank the line under cursor
dd delete the line under cursor

Example:

d2w deletes 2 words.

y5j yanks 5 lines (line under cursor and 2 line below).

Lesson 04: Commands

These keys bindings are for file manipulations and should be used in Normal mode.

Here <CR> refers to Carriage Return or Enter key. [filename] is optional. If not provided the current opened file is used.

key action
:w [filename]<CR> save file
:e [filename]<CR> edit/open file
:q<CR> quit vi
:q!<CR> quit vi without saving file changes
:wq<CR> write to file and quit

Conclusion

Congratulations, you have now successfully learnt the basics of vi. You can now use the same key bindings for many terminal applications like less.

Keep practicing until the key bindings become muscle memory. If you are interested to take the next steps and learn more, you can follow the reference link.

References