Programming

Basic Vi Editor Commands of Unix/Linux

Gautam Gautam Donate Nov 27, 2019 · 3 mins read
Basic Vi Editor Commands of Unix/Linux

The vi editor (short for visual editor) is a screen editor which is available on almost all Unix systems. To use vi editor we must know it commands so we can operate easily.

To Start vi -

  1. vi filename - Create or Edit filename starting at line 1
  2. vi -r filename - Recover filename that was being edited when system crashed

To Exit vi -

  1. :x<return> - Quit vi, writing out modified file to file named in original invocation
  2. :wq<return> Quit vi, writing out modified file to file named in original invocation
  3. :q<return> Quit or exit vi 4. :q!<return> Quit vi even though latest changes have not been saved for this vi call

Moving the cursor - 

  1. j or <return> [or down-arrow] - Move cursor down one line
  2. k[or up-arrow] - Move cursor up one line
  3. h or<backspace> [or left-arrow] - Move cursor left one line l or<space>[or right-arrow] Move cursor right one line
  4. 0(zero) - Move cursor to start of current line(the one with the arrow)
  5. $ - Move cursor to end of the current line
  6. w - Move cursor to beginning of next word
  7. b - Move cursor back to beginning of preceding word
  8. :0<return> or 1G - Move cursor to first line in file
  9. :n<return>or nG - Move cursor to line n in file
  10. :$<return>or G - Move cursor to last line in file

Screen Manipulation -

  1. ^f - Move forward one screen
  2. ^b - Move backward one screen
  3. ^d - Move down(forward) one half screen
  4. ^u - Move up(back)one half sreen
  5. ^l - Redraws the screen
  6. ^r - Redraws the screen,removing the deleted lines

Adding, Changing, Deleting the text -

  1. u - Undo whatever you just did(a simple toggle)

Inserting or Adding text -

  1. i - Insert text before cursor, until<esc>hit
  2. I - Insert text at beginning of current line, until<esc>hit
  3. a - Append text after cursor, until<esc>hit
  4. A - Append text to end of current line, until<esc>hit
  5. o - Open and put text in a new line below current line, until<esc>hit
  6. O - Open and put text in a new line above current line,until<esc>hit

Changing the text -

  1. r - Replace single character under cursor(no <esc>needed)
  2. R - Replace characters,starting with current cursor position, until<esc>hit
  3. cw - Change the current word with new text,starting with the character under cursor, until<esc>hit
  4. cNw - Change N words beginning with the character under cursor, until<esc>hit
  5. c - change(replace) the characters in the current line, until<esc>hit
  6. cc - change(replace) the characters in the current line, stopping when <esc>hit
  7. Ncc or cNc  - change(replace) the next N lines, starting with the current line,stopping when <esc>hit

Deleting the text -

  1. x - Delete single character under cursor
  2. Nx - Delete N characters, starting with character under cursor
  3. dw - Delete the single word beginning with character under cursor
  4. dNw - Delete N words beginning with character under cursor
  5. D - Delete the remainder of the line,starting with current cursor position
  6. dd - Delete entire current line
  7. Ndd or dNd - Delete N lines beginning with the current line

Cutting and pasting the text -

  1. yy - Copy (yank, cut) the current line into buffer
  2. Nyy or yNy - copy (yank, cut) the next N lines, including the current line, into the buffer
  3. p - put(paste) the lines in the buffer into the text after the current line

Searching the text -

  1. /string - Search forward for occurrence of string in text
  2. ?string - Search backward for occurrence of string in text
  3. n - Move to next occurrence of search string
  4. N - Move to next occurrence of search string in opposite direction

Determining line numbers -

  1. :.= - Returns line number of current line at bottom of screen
  2. := - Returns the total number of lines at bottom of the screen
  3. ^g - Provides the current line number, along with the total number of lines, in the file at the bottom of the screen

Saving and reading files -

  1. :r filename<return> - Read file named filename and insert after current line (the line with arrow)
  2. :w<return> - Write current contents to a file named in original vi call
  3. :w newfile<return> - Write current contents to a new file named newfile
  4. :12,35w smallfile<return> - Write the contents of the line numbered 12 through 35 to a new file named smallfile
  5. :w! prevfile<return> - Write current contents over a pre-existing file named prevfile

Thanks for reading, you can find more command from here - https://www.ccsf.edu/Pub/Fac/vi.html


Tags

Join Newsletter
Get the latest news right in your inbox. We never spam!
Gautam
Written by Gautam Sharma Profile
Developer, Writer, Emotional, loves to help, blogger

Connect with Gautam