My Basic VI commands

vi filename edit filename starting at line 1
:q quit (or exit) vi

:q!  quit vi even though latest changes have not been saved for this vi call

:0 or 1G or ggmove cursor to first line in file
:n or nGmove cursor to line n
:$ or Gmove cursor to last line in file
iinsert text before cursor, until  hit
/stringsearch forward for occurrence of string in text

 n move to next occurrence of search string

N move to next occurrence of search string in opposite direction

:e  refresh the currently loaded file

CTRL-uMoves screen up ½ page
CTRL-dMoves screen down ½ page
CTRL-bMoves screen up one page
CTRL-fMoves screen down one page


:w    write to disk
     ZZ    write to disk and exit
     :q!   exit without writing to disk

Actually, the command for quitting vi is :q. You can save and quit by typing :wq but ZZ does the same thing1 and takes one less keystroke. If there are unsaved changes to the text and you try to quit using :qvi will warn you that you have unsaved changes and will prevent you from quitting. In order to quit without saving the changes you must use the override switch, !.

For the purist, :wq and ZZ are not exactly the same.  :wq always saves, whereas ZZ saves only if changes have been made since the last save.

To paste from clipboard: First, make sure you’re in edit mode (press i). Then you can paste with Ctrl+Shift+V

remove a char -> x

delete the line -> dd

delete the rest of the line -> D

start and end of the line

start of the line -> 0  OR ^

end of the line  -> $

Move cursor around

use e and b to move word back(end) and forth(begin).

use ( and ) to move cursor between sentences(by .).

use f + , or . to find next char. and followed with ; for the next(in long line).

count occurrence

this would take advantage of the substitute() command.

% is for the entire file

s is for substituion

g is global,

n is for making no change to the document.

OR word:

the :set inccommand=nosplit is a nice neovim feature that let you preview substitution(:%s/<search_string>/<replace_string>/g) live.




cannot save file due to not-root/not-sudo

In the page Top Ten One-Liners from CommandLineFu Explained is suggested this trick (the #3):

:w !sudo tee %

this write the current buffer to the stdin of the command after the !. The % symbol is substituted with the current filename.

show line numbers

:set nu

delete multiple lines

  1. from current line to the end: dG
    Just to note, the action d indicates delete, and G indicates the last line of the file. So, while at any line you press dG, it deletes all the line starting from the current line till the last one.
  2. from current line to certain line, first we show line number using the above command, let say we want to delete from 55 to 100, we first :set nu to see the line number and then navigate to line 55, then we do d100G to remove the line from current to 100.

in the last line and edit

Often time, we use G and in the last line, if I want to start a new line to edit, I have to use $ to the end and press i to edit mode and then move cursor to right and then hit enter to the new line. Turns out i could use one command to do all above:

o Open a new line after current line  // this IS the solution!!
O Open a new line before current line

indent/format file

our standard is to use 2 spaces. so have the below setting in .vimrc :

set expandtab
set shiftwidth=2
set softtabstop=2

Then use gg=G to format the file from beginning to end. Here = is the key for format/indent. to indent a line, just use ==.


select in visual mode

In visual mode, v is select by char, V is line mode, and ctrl-v is block mode that we can have multiple cursors in multiple lines. So select all, we can use gg and then Vthen G , if we use v, then the last line will not be selected.


To paste, use p, however to paste before the cursor like the beginning of the line, we have to use P, uppercase.

Remove newLine/lineBreak

The normal way is to press 0 or ^ to the beginning of the line and then i to edit then backspace to remove. This SO way is great, just go to the first line and use gJ to join the next line. Or use 3gJ to join 3 lines. Neat!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s