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 move cursor to first line in file
:n or nG move cursor to line n
:$ or G move cursor to last line in file
i insert text before cursor, until  hit
/string search 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-u Moves screen up ½ page
CTRL-d Moves screen down ½ page
CTRL-b Moves screen up one page
CTRL-f Moves screen down one page

Save&Quit

: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.

:%s/pattern//gn
OR word:
:%s/\<word\>//gn

undo

use

u

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

..

FROM HERE

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s