VI editor basic commands

Vi Editor basically has two modes: INSERT MODE and COMMAND MODE. In Insert mode, one can edit, make changes. On the other hand in command mode, we can enter commands to perform certain task, for example substitution, deletion.

  • To switch from INSERT MODE to COMMAND MODE press Esc key.

  • To switch from COMMAND MODE to INSERT MODE press I.

Basics commands

i    : Insert text at the current cursor position
I    : Insert text at the begining of the current line.
o    : Add text in a new line below the current line.
O    : Add text in a new line above the current line.
a    : Append text after the position of cursor.
A    : Append at the end of line. 
  • Move Cursor
h    : Move the cursor one character to the left 
l    : Move the cursor one character to the right
k    : Move the cursor up one line
j    : Move the cursor down one line
gg   : Go to the begining of file.
G    : Go to the end of the file.
^d   : Shift one page down [^: CTRL]
^u   : Shift one page up [^: CTRL]  
$    : Move cursor to the end of current line
0    : Move cursor to the beginning of current line
w    : Forward one word
b    : Backward one word 
  • Cut / Copy / Paste

  • Delete

x   : Delete a character
dd  : Delete current line
d$  : Delete from the position of the cursor to end of the line
dw  : Delete word from cursor on
db  : Delete word backward
  • Copy (Yank)
yy  : Yank current line
y$  : Yank to end of current line from cursor
yw  : Yank from cursor to end of current word
2yy : Yank 2 lines
y   : Yanks the Highlighted characters.
  • Paste
p  : Paste below the cursor
P  : Paste above the cursor
u  : Undo last change
U  : Restore line
J  : Join next line down to the end of the current line
  • Highlight characters
v   : activates the visual mode and one can move cursor to highlight required characters.
^v  : Highlight vertically. Very useful for inserting tab in multiple lines.
  • Exit from the Editor
:wq    : Write file to disk and quit the editor
:q!    : Quit (Without saving the changes)
:q     : Quit (Will print out a warning if changes have been made)
:w abc : Save the current file to a new file abc

more commands

  • Find and Replace

One of the widely used commands for me personally.

:[range]s/{pattern}/{string}/[flags] [count]

Possible Flags:

[c] : Confirm each substitution

[g] : Replace all occurrrences in the line.

[i] : Ignore case for the pattern

s : stands for substitute

  • Some examples
:s/XX/YY/g         [only in current line]
:%s/XX/YY/g        [in the entire file]
:5,12s/XX/YY/g     [only in specific lines 5 and 12]
:.,$s/XX/YY/g      [current + all the following lines.]
:s/XX/YY/g 4       [Current + following 4 lines]
:.,+2s/XX/YY/g     [current + next 2 lines same as above.]

Explanation

:s for substitution, . : current line , $ :end line, % : entire file, g : global search but locally global. only for that line.

  • insert spaces or indentation in multiple lines
highlight the line[s] and press >> or <<
  • deleting certain parts
:%normal 2x     [Delete the first n characters of every lines]
:%s/^ /         [Delete the first n characters of every lines only if they are spaces]
:%s/\s+$//e     [Delete any trailing space at the end of all the lines]
:%s/
:%s/^\s\+//e    [Delete all initial spaces(or tabs) of all the lines]
                [^: begining of line; \s looks for spaces(including tabs); \+ looks for one and or more occurance; //e: first / means substitute by and nothing after it means nothing and \e means if not found the string then suppress the error.]
:%le [==:left]           [Global left alignment (much more efficient)]
  • OPEN/ CLOSE/ SPLIT files
vim f1.txt             [open single file]
vim -o f1.txt f2.txt   [open two files in horizontal]
vim -O f1.txt f2.txt   [open two files vertically]
CTRL w [R/L/T/B arrow] [toggle between different split windows]
CTRL +w w              [  Toggle between different split windows]
:e new_file            [open new file in this window]

:sp new_file           [splits Horizontally and opens the new file]
:vsp new_file          [splits vertically and opens the new file]

CTRL +w , s            [split Horizontally ]  
CTRL +w , v            [split vertically ]
CTRL +w, Q             [Quit the current window]
  • global command

The global command is useful in many cases.

general pattern:

:[range]g/pattern/cmd

!!Examples

:g/[abcde]/d           [delete lines matching pattern abcde]
:!g/[abcde]/d          [delete lines not matching the pattern abcde]
:g/^\s*$/d             [^($) means start(end) of line and \s* means 0 or many spaces including tab, delete all empty lines.]
:g/[pat]/s/abc/def/gi  [In all lines containing pattern pat, substitute abc by def.]
  • Find a pattern in a line and delete everything after it. or delete after that pattern
:1,$s/pattern.*//g .  # to delete everything after  (and including) that pattern
:1,$s/pattern.*/pattern/g .  # to delete everything after  (excluding) that pattern
  • Some other Examples

Sometime you have to comment few lines. For that Usually for bash-script we put a # sign in the front of every line to comment that line. Easier way is following: go to top(bottom) of the lines press CTRL v to go to visual mode and press j or k to go down or up to whatever many lines you need to comment. Press I (capital i) and put a sign # now go to command mode by pressing Esc key and save the file :w. Should work!

Playing with tab and spaces in VIM

A lot of times I get old scripts and codes where a tab is used and while making changes and compiling the code I get indentation error due to tabs. Here is how to fix this.

  • Replace tab by spaces: :retab
  • Set length of spaces for a tab: :set tabstop=4, :set shiftwidth=4, :set expandtab
  • For more info, find my .vimrc file my vimrc file

Yank and Paste same thing multiple times.

xnoremap p pgvy p to paste, gv to re-select what was originally selected. y to copy it again.