X
    Categories: Linux

12 Simple methods to handle vi editor commands.

Vi Editor is most popular editor in UNIX. vi editor is also called as Visual editor. Below methods describes different vi editor commands from basic to advance.

vi editor modes.

  1. Command mode: used for giving commands to editor
  2. Insert mode: used for entering the data into the file.

Now How to switch between these two modes:

Press “i”  for INSERT mode now to return it to command mode press Esc button.

Vi editor commands.

1. Open

This method describes the vi editor commands regards to opening & viewing the files in UNIX.

  • # vi                   ===> Will  open editor
  • # vi <file>        ===> Will  open the file
  • # view <file>   ===> Will open the file as read-only

2. Save / Exit

This method describes the vi editor commands regards to saving & exiting the files in UNIX.

  • :q                   ===> Will exit the editor
  • :w                  ===> Will save
  • :w <file>       ===> Will save as
  • :wq               ===> Will save and exit
  • “:x”               ===>  Same as above
  • ZZ                ===> Same as above
  • :q!                ===> Will exit forcefully without saving
  • :w!               ===>  Will save forcefully

3. Switch between vi editor modes.

With this method of vi editor commands we can switch between the Vi editor modes .

  • i                   ===> go to INSERT mode
  • Insert          ===> Same as above
  • Esc              ===> return to COMMAND mode
  • v                  ===> go to VISUAL mode

4. Find

With this type of vi editor commands we can search words in the file.

  • /<word>     ===> find the word
  • n                 ===> go to the next word

5. Undo / Redo

With this type of vi editor commands we can undo & redo the changes made to the file.

  • u                ===> undo the last action
  • Ctrl+r        ===> redo the last action
  • .                 ===> repeat the last action

6. Moving between lines

With this type of vi editor commands we can move between lines of the file.

  • :1               ===> go to the first line
  • [[                ===> Same as above
  • gg              ===>Same as above
  • :$               ===> go to the last line
  • ]]                ===> Same as above
  • G               ===> Same as above
  • w               ===> go to the next word
  • b                ===> go to the previous word
  • :@              ===> go to the @ line ( @ = line number )
  • @G            ===> Same as above

7. Set options

With this type of vi editor commands we can set different options in vi editor.

  • :set all                 ===> show all set options
  • :set nu                 ===> show line numbers before every line
  • :set nonu            ===> do not show line numbers
  • :set ruler            ===> show position of cursor at bottom right of the screen
  • :set noruler        ===> do not show the above
  • :set ic                   ===> set ignore case in search on
  • :set noic              ===> set ignore case in search off
  • :set hls                 ===> highlight the word given for searching
  • :set nohls            ===> do not highlight the word
  • :set showmode  ===> set the indicator mode
  • :set noai              ===> set autoindenting
  • :set lm=@            ===> set the left margin
  • :set tw=@            ===> set the text width from left to right
  • :set wm=@          ===> set the wrap margin
  • :set ts=@             ===> set the tab stop
  • :set incsearch     ===> set up an incremental search

8. Insert

With this type of vi editor commands we can insert lines in the file as per requirement.

  • O                           ===> insert a blank line above the cursor and change to insert mode
  • o                            ===> insert a blank line below the cursor and change to insert mode
  • :r <file>                 ===> insert the file’s content in current file after the cursor

9. Cut, Copy & Paste

With this type of vi editor commands we can cut/copy or Paste the contents of the file.

  • x                             ===> cut the character
  • @x                          ===> cut @ characters ( @ = number of characters )
  • yw                           ===> copy the word
  • @yw                       ===> copy @ words ( @ = number of words )
  • cw                           ===> cut the word
  • @cw                       ===> cut @ words ( @ = number of words )
  • dw                          ===> delete the word
  • @dw                       ===> delete @ words ( @ = number of words )
  • yy                            ===> copy the line
  • @yy                        ===> copy @ lines ( @ = number of lines )
  • cc                            ===> cut the line
  • @cc                        ===> cut @ lines ( @ = number of lines )
  • dd                           ===> delete the line
  • @dd                        ===> delete @ lines ( @ – number of lines )
  • y$                            ===> copy words from the cursor up to the end of the line
  • c$                            ===> cut words from the cursor up to the end of the line
  • C                              ===> …and change to insert mode
  • d$                            ===> delete words from the cursor up to the end of the line
  • D                              ===> Same as above
  • p                              ===> paste the cut/copied/deleted line/s, word/s or character/s

10. Find & replace

With this type of vi editor commands we can find & replace the contents of the file.

  • :1,$ s/<old>/<new>/g ===> replace the character or word in the whole file

where 1 denotes first line & $ denotes last line

  • :% s/<old>/<new>/g ===> Same as above
  • :1,. s/<old>/<new>/g ===> replace the character or word between the lines

where 1 denotes first line & . denotes current cursor position

  • :@,. s/<old>/<new>/g ===> replace the character or word between the lines

where @ denotes line number & . denotes current cursor position

  • eg.
  • :3,. s/new/new1/g    ===> will replace all the word “new”with “new1″between the lines 3rd till current cursor position
  • :.,$ s/<old>/<new>/g ===> replace the character or word between the lines

where. denotes current cursor position & $ denotes last line

  • :.,@ s/<old>/<new>/g ===> replace the character or word between the lines

where. denotes current cursor position & @ denotes line number

  • :@,@ s/<old>/<new>/g ===> replace the character or word between the lines

where @ denotes line number

11. System

With this type of vi editor commands we can execute system commands in vi.

  • :!<command> ===> run the command and show output if any without exiting the vi editor
  • :sh ===> give a shell temporarily
  • exit ===> exit the shell and return to the vi editor
  • Ctrl+d ===>Same as above

12. Sessions

This type of vi editor commands describes how to split the screen for editing.

  • :sp ===> split the screen horizontally in two
  • :vsp ===> split the screen vertically in two
  • Ctrl+w ===> switch between the screens

To learn more about vi editor follow: https://www.ccsf.edu/Pub/Fac/vi.html

Related Post