Sysleaf

VI command cheatsheet

. .
VI command cheatsheet

Introduction

As a developer, we love editor, we spent our most of the time using GUI editor. But sometimes, we do use CLI editor for deployment or DevOps operation. Due to infrequent or rare use of CLI editor, we forget their commands.

VI is one of such popular command line editor. Some shell can also be run in VI mode. So knowing VI command will not only help in editing but will also help in working with Shell. This post is all about creating a cheat sheet of commonly used VI command for quick reference.

Open, Mode & Quit

By default when we open a file with VI, it opens in command mode. You won’t be able to edit it until you switch it to insert mode. VI operate in two modes: Insert & Command. ‘Command mode’ is used to run command and ‘Insert mode’ is used to edit the file.

Description Command
opend/edit file vi filename
switch to insert mode at cuurent cursor i
switch to command mode esc
save file :w
save file & quit :wq
quit without saving file :q!
switch to insert mode by moving cursor at start of line I
switch to insert mode by moving cursor next to current character a
switch to insert mode by moving cursor at end of line A
switch to insert mode by creating new blank line below current line o
switch to insert mode by creating new blank line above current line O

Move

Move command is about moving the cursor from one point to another point inside vi editors. These commands are very important because these same commands are combined with another command like cut, copy, paste & delete.

Description Command
go up k
go down j
go left h
go right l
go to start of line 0
go to end of line $
go to start of next word w
go to start of previous word b
go to first line of file 1G
go to last line of file G
go to nth line of file nG
go to start of next screen ctrl+f
go to start of previous screen ctrl+b

Cut, Copy & Paste

Main key button for copy(yank) is y, we can combine this key with ‘move key’ to acheive various copy requirement. Cutting means deleting, So use all the delete command for cut operation. General Steps to copy & paste:

  1. Move to place from where you want to cut/copy using ‘move command’
  2. Copy using key y or Cut using key d
  3. Move to place till where you want to copy/cut using ‘move command’
  4. Move to place where you want to paste using ‘move command’
  5. Paste using key p
Description Command
cut d
copy y
paste p
copy next word from current cursor yw
copy next n number of words from current cursor nyw
copy current line yy
copy next n number of line from current line nyy
copy characters from current cursor to start of line y0
copy characters from current cursor to end of line y$
copy from current line to nth line ynG
copy from line number x to line numer z xGyzG
copy from first line to last line of file 1GyG
paste copied line below current cursor p
paste copied line above current cursor P

Undo & Redo

Description Command
undo u
redo ctrl + r

Delete

Main key button for delete is d, we can combine this key with ‘move key’ to acheive various delete requirement.

Description Command
delete next character from current cursor x
delete next n number of characters from current cursor nx
delete next word from current cursor dw
delete next n number of words from current cursor ndw
delete current line dd
delete next n number of line from current line ndd
delete characters from current cursor to start of line d0
delete characters from current cursor to end of line d$
delete from current line to nth line dnG
delete from line number x to line numer y xGdyG
delete from first line to last line of file 1GdG

Find & Replace

Description Command
find text ‘search_txt’ in forward direction /search_txt
find text ‘search_txt’ in backward direction ?search_txt
move to next occurrence of search result in forward direction n
move to next occurrence of search result in backward direction N
find for text ‘search_text’, use key n to go to next search result /search_text
find text ‘search_txt’ and replace with text ‘replace_txt’ /search_txt/replace_txt
find text ‘search_txt’ and replace with text ‘replace_txt’ at line level /search_txt/replace_txt/n
find text ‘search_txt’ and replace with text ‘replace_txt’ at file level /search_txt/replace_txt/gn
find text ‘search_txt’ and replace with text ‘replace_txt’ but confirm before replace /search_txt/replace_txt/gcn

Setting

Setting is used to modify the default behavior of VI editor.

Description Command
set line number :set nu
set auto indentetion :set au
set to ignore case while searching :set ic