Vi editor and its shortcuts

Subash NeupaneSubash Neupane
3 min read

Vi editor stands out from other text editors due to its unique approach to text manipulation. It operates in different modes, each serving a specific purpose:

  1. Normal Mode: This is the default mode where you can navigate, manipulate text, and execute commands. Here, single keystrokes perform actions like moving the cursor or deleting characters.

  2. Insert Mode: In this mode, you can type and edit text just like in any other text editor. To enter Insert Mode from Normal Mode, press "i".

  3. Visual Mode: This mode enables text selection for operations like copying, cutting, and pasting. It comes in handy when you want to manipulate a block of text.

  4. Command-Line Mode: This mode is used to execute commands like saving files, searching, and replacing text. To access it, press ":" in Normal Mode.

Basic Vi commands

: -To get into command mode

Esc -To get into normal mode

i - To get into insert mode

v or V - To get into visual mode

: help - Using the help command you can read the documentation of any command

:w - Save the file you are working on

:wq - Save the file and close Vim.

:q! - Quit without saving the file you were working on.

: set number - To enable line numbers

: Set number! - To disable line numbers

Vi commands for navigation

Basic navigation

To navigate left, right, up, and down in the file

h – Move the cursor left by one position

l – Move the cursor right by one position

k – Move the cursor in an upward direction by one line

j – Move the cursor in a downward direction by one line

To jump to a particular line, use numbers along with k/j.For example to jump to line number 5 below from the current line, use 5j

0 – Jump to the start of the current line

$ – Jump to the end of the current line

Ctrl + f – Scroll down an entire page

Ctrl + b – Scroll up an entire page

NOTE - For this, you have to be in normal mode.

Navigate to lines

:n – Jump to the nth line

:0 – Jump to the start of the file

:$ – Jump to the end of the file

NOTE - For this, you have to be in command mode.

Word navigation

w – Move the cursor to the beginning of the next word

e – Move the cursor to the end of the current word

b – Move the cursor to the beginning of the previous word

Vi commands for editing

Like any other text editor, different editing actions like cut, copy, and paste can be performed in Vi. In vi, Copy is called yank (y), Paste as put (p), and cut is delete(d).

y – Copy(yank) a single character from the cursor position

yy – copy (yank) a line

2yy – Copy (yank) 2 lines, You can copy as many lines as you want but add the number of lines you want.

yw – Copy (yank) characters of the word from the cursor position to the start of the next word

y$ – Copy (yank) to the end of the line

p – Paste the character after the cursor position

P – Paste the character before the cursor position

x– Delete the character from the cursor position

X– Delete the previous character from the cursor position

dd – Delete (cut) a line

2dd – delete (cut) 2 lines

dw – Delete word from cursor position

D – Delete the entire line from the cursor position

u– Undo single-action

3u– To perform multiple undo operations. Here 3u will undo the last 3 actions.

This was a small description of the basic commands of Vi. Vi is more than just a text editor; it's a skill that, once acquired, can revolutionize the text editing experience.

Happy Learning!

0
Subscribe to my newsletter

Read articles from Subash Neupane directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Subash Neupane
Subash Neupane

Computer Science graduate