Pages

vi (vim) commands, examples

the world of vi (vim) is different from other editors.. this makes it special, some people don't recommend it for novice coders. thats because you need to know its commands, this post should give you ONLY a start point for using vim :)

down to business, well you may know or not yet that vim have different modes  probably as follows:

  • Insert mode: activated by pressing   i  character, disable by  Esc 
  • Replace mode : activated by pressing  r  or  R character, disables by  Esc 
  • Command mode: this is the default where you are passing commands to vim. commonly started by colon followed by command like :r filenme
  • Visual mode: only in vim not vi, invoked by  v 

Invoking vim

CommandDescription
vi file1 file2 ..opening file(s) to edit
vi -r filerecovering previously not saved file(crashed) from swp file
vi -n filestart without writing swp file , if your editing session crashed or your computer ran out of power then you'll loose your work.. solution save the file as soon as you change it



files handling

CommandDescription
:r fileread file and isert its contents inside the current document at the cursor postion
:nmove to next file to edit, this is mainly when you invoke vim by multiple files to edit, like vim file1 file2 file3. if you were editing file1 this command will open file2 and close file1
:Nmove to previous opened file, opposite of the previous command
:wsave changes to file but dont exit
:xsave and exit
:qquit of saved file(s)
:q!quit anyway
:n file4edit new file,and closing files list if already saved
:n! file4start editing new file anyway
:N!move to previous file, ignoring change to current file

searching

CommandDescription
/keywordsearch for keyword forward
?keywordsearch for keyword backward
nmove to next found match
Nmove to previous found match

replacing

:s/oldtext/newtextsearching for oldtext and replace it with newtext, only once in the line
:%s/oldtext/newtext/searching for oldtext and replace it with newtext, all occuarance in the line
:%s/oldtext/newtext/gsearching for oldtext and replace it with newtext, all occuarance in whole document


Inserting

istart insert text before cursor. pres Esc to stop
I(this is upper case i not l) start insert text at the beginning of the line. pres Esc to stop
astart insert text after cursor. pres Esc to stop
Astart insert text at the end of the line. pres Esc to stop
pput text already stored in buffer, after the current line
Pput text already stored in buffer, before the current line


yanking, selecting

yyank(copy) the current line
vstart selecting text, move up down right left to control the selected text


deleting, restoring

xdelete the character under the cursor
Xdelete the character before the cursor
dddelete current line
Ddelete the rest of the line
dwdelete current word
.(dot) redo the previous action
Urestore current line
uundo the last change, you can prepend it with anumber for example 7u undo previous 7 changes


moving the cursor

0move to the start of the line
$move to the end of the line
Gmove to the end of the document
wmove one word forward


replacing

rreplace character below the cursor
Rstart replacing mode, ESC to stop

Examples:

replacing all new lines with ; and new line: :%s/\n/;\r/g

displaying line numbers with set command:   :set number or :set nu 

searching help for something for example delete :help delete

Note: Debian doesn't install vim by default but vim_tiny (minmal installation of vim) to install full vim with all cool stuff and help files and docs run the following :

sudo apt-get install vim


 when you get used to vim.. you'll learn much more about this decent editor

-------------------
cheers


No comments:

Post a Comment