Vim

http://www.thegeekstuff.com/2009/01/vi-and-vim-macro-tutorial-how-to-record-and-play/

http://vim.sourceforge.net/tips/tip.php?tip_id=589
http://vim.sourceforge.net/scripts/script.php?script_id=1908
http://www.linux.com/archive/feed/57727
Vim Essential Plugin: SnipMate
Vim Essential Plugin: Markdown to HTML
Vim Essential Plugin: PeepOpen
http://net.tutsplus.com/tutorials/other/vim-essential-plugin-nerdtree/
http://zmievski.org/2010/06/vim-for-programmers-on-slideshare
http://net.tutsplus.com/tutorials/other/vim-essential-plugin-markdown-to-html/
http://net.tutsplus.com/tutorials/other/vim-essential-plugin-tcomment/
http://net.tutsplus.com/sessions/vim-essential-plugins/
http://net.tutsplus.com/tutorials/other/vim-essential-plugin-snipmate/
http://net.tutsplus.com/tutorials/other/vim-essential-plugin-sparkup/
http://net.tutsplus.com/tutorials/other/vim-essential-plugin-surround/
http://net.tutsplus.com/tutorials/other/vim-essential-plugin-easymotion/

Wish to know

  1. Delete matching parans without deleting the text in between
  2. highlight multiple search pattern (perhaps with different colors)
  3. vimdiff
  4. cvs integration
  5. subversion integration
  6. vim when type without specifying a file name, automatically open pre-registered / previously opened files

Ex Mode

:%s/t/x/g // search and replace the whole file
:v/warning/d  // delete all lines not matching warning
:.,$s/t/x/g // search and replace from current line to the end of file
:1,20y   // yank lines 1 through 20
:1,20ya  // yank lines 1 through 20 and put them in buffer 'a'
:f filename // change the name of the current file being edited
:w       // write to file
:w newfilename // write to a new file
:10,50w newfilename // write lines 10 through 50 to newfilename
:.,$w! filename // force write current line through end of file to filename
:.+2,'aw filename // write two lines  down from current line through marker 'a' to filename
:/pattern1/,/pattern2/w filename // write text from pattern1 to pattern2 to filename
:w >>filename // append current file to filename
:2,5w >>filename // append lines 2 through 5 to filename
:r filename // reads filename into current file below the current line
:r !command // reads the result of the shell command  into the current file below the current line
:e newfilename // open newfilename
:e#         // open previous file
:e! newfile // edit newfile, discarding buffer
:q          // quit out of vi
:q!         // quit out of vi without saving changes
:wq         // save and quit out of vi
:x          // save and quit
:%l         // show all control character
:!command   // run and external shell command
:!sh        // spawn a shell
:!ls        // list the current directory
:set nu     // display line number
:set nonu   // hide line number
:set sm     // show matching parens
:se sm   // show matching brace or paran while inserting
:reg     // show the content of all registers
:reg x   // show the content of register x
:1,2dx   // delete range into register x
:set ignorecase 
:set icsearch 
:set hlsearch 
:colo evening // change the foreground color

Command Mode:

4rx      // replace next four characters with single character 'x'
.        // repeat last command
u        // undo last command
gf       // load a file whose name is under the cursor
Ctrl+G   // display filename, line, and column
3s       // substitute next 3 characters and go into insert mode
3S       // substitute next 3 lines, and go into insert mode

%        // find matching paran
d%       // delete until matching paran
>%       // shift all lines between matching parens

>        // increase indentation
<        // decrease indentation

'0       // (single quote followed by zero) take you to place you last edited
0 (zero) // jump to the start of line (column zero)
^        // jump to the start of line (first non-whitespace)
$        // jump to the end of line
fx       // jump to next occurrence of 'x'
gg       // go to beginning of file
G        // jump to the end of file
1G       // jump to the start of file
3w       // jump forward 3 words
3W       // jump forward 3 words, ignore punctuation
2b       // jump backward 2 words
2B       // jump backward 2 words, ignore punctuation
e        // jump to the end of current word
7e       // jump to the end of the seventh word
E        // jump to the end of word, ignore punctuation
6E       // jump to the end of sixth word, ignore punctuation
2fa      // jump to the second occurrence of character 'a' (forward)
2Fa      // jump to the second occurrence of character 'a' (backward)
2ta      // jump to the second occurrence of character 'a' (not including character 'a')
;        // repeat last find of character
,        // repeat last find of character in reverse

*        // this search command will find the next occurrence of the word the cursor is currently one
/pattern // locate next occurrence of pattern
?pattern // locate last occurrence of pattern
n        // go to next occurrence of pattern (after using / or ?)
N        // go to last occurrence of pattern (after using / or ?)
*                     // search for word currently under the cursor
g*                    // search for partial word under the cursor (repeat with n)

rx       // replace character below cursor with single character 'x'
J        // join lines

dd       // delete lines
dw       // delete words
dd       // delete line
D        // delete to the end of line
dG       // delete all lines from cursor to the end of file
d8G      // delete all lines from cursor to line 8
d/pattern // delete from cursor until the next occurrence of pattern
d?pattern // delete from cursor until the last occurrence of pattern
dfa      // delete upto and including next character 'a'
d2fa     // delete upto and including the second occurrence of character 'a'
dta      // delete upto but including next character 'a'

cc       // changes lines
cw       // change word
cc       // change the current line
cfx      // change until the next occurrence of 'x'
C        // change until the end of line
c/pattern // change until the next occurrence of pattern

mx       // set marker (where x is a-z)
:marks   // find what marks are set
`x       // go to marker x
'x       // go to the start of the line containing the marker

yw       // yank word
y8w      // yank 8 words
Y        // yank line
8Y       // yank 8 lines
y/pattern // yank from cursor up to but not including pattern

p        // put the content of the default buffer after/below the cursor
2p       // paste from delete buffer number 2
"aY      // yank line into named buffer 'a'
"a6dd    // delete 6 lines and put them in buffer 'a'
"AdG     // delete from cursor to the end of file and put it in buffer A
"ap      // put the content of buffer 'a' after/below cursor
x        // delete the character below the cursor

xp        // swap characters
dwwP      // swap words
ddp       // swap lines

50i*ESC  // insert 50 stars (*) where you are
20I-ESC  // insert 20 hyphen at the beginning of line
10a#ESC  // append 10 #'s after the next character
20A#ESC  // append 20 #'s at the end of line

]p       // paste with indentation adjusted
[p       // like P with indentation adjusted
gp       // like p, leaving the cursor after new text
gP       // like P, leaving the cursor after new text
2 CTRL+A // add 2 to the number under the cursor (command mode)
2 CTRL+X // substract 2 to the number under the cursor (command mode)
gm       // go to middle of line

M        // go to middle line of window
g~2w     // switch case for the next 2 words
gu2w     // lowercase the next 2 words
gU2w     // uppercase the next 2 words
`[ and `]  // move to either end of the chunk of test most recently modified
`<       // move to the start of most recent selection
`>       // move to the end of most recent selection
[{       // handy for discovering which function of 'if' clause this is in.
viwU  //convert word under the cursor to upper case 
viwu   //convert word under the cursor to lower case 
viw~  // flip case for the word under the cursor 
~ // flip case for the character under the cursor 
villl~ //flip case for a block of text 
8|       // jump to the 8th column
8(       // jump back 8 sentences
8)       // jump forward 8 sentences
8{       // jump back 8 paragraphs
8}       // jump forward 8 paragraphs
)        // jump to the start of next sentence
(        // jump to the start of last sentence
~        // change the case of the character
a        // append text after the cursor position
A        // append text at the end of current line
i        // insert text before the cursor position
I        // insert text at the beginning of line
o        // insert a new line below the current line and go into insert mode
O        // insert a new line above the current line and go into insert mode
8G       // jump to line 8th in the file

Visual Mode:

V movement  // select lines
v movement  // select blocks
ctrl+v                // select columns
gv                    // reselect block
=        // re-indent

Insert Mode:

Ctrl+V      // the following character is a control character
Ctrl+O   // jump back to where you were
Ctrl+I   // Ctrl+O and Ctrl+I take you up and down the list of recent positions
Ctrl+P   // autocompletion
Ctrl+N   // autocompletion
CTRL-T   // increase indentation 
CTRL-D   // decrease indentation 
CTRL+R bufferName // paste
Ctrl+T   // increase indentation (normal-mode keystroke: > )
Ctrl+D   // decrease indentation (normal-mode keystroke: < )
Ctrl+X   // copy, Ctrl+X Ctrl+P, Ctrl+X Ctrl+L
Ctrl+Y   // copying above characters
Ctrl+X Ctrl+F // filename completion
Ctrl+W f // load a file whose name is under the cursor
Ctrl+L   // refresh screen
CTRL+A   // insert previously inserted text
CTRL+Rx  // insert the content of register x
CTRL+R CTRL+Rx // do something
CTRL+W   // delete word before the cursor
CTRL+U   // delete all inserted characters in the current line
CTRL+D   // shift left one shift width
CTRL+T   // shift right one shift width
CTRL+Oc  // execute c in temporary command mode (c is a shell command)
CTRL+X CTRL+E // scroll up
CTRL+X CTRL+Y // scroll down

Folding:

Use folds to collapse selected block of code or comment. First select a block of text, then :fold.

zo     // open
zc     // close

Also see :help foldmethod, mkview, loadview.

Multiple windows:

:split filename       // split window and load another file
ctrl+w up arrow       // move cursor up a window
ctrl+w ctrl+w         // move cursor to another window (cycle)
ctrl+w ctrl+p         // switch back to previous window
ctrl+w_               // maximize current window
ctrl+w=               // make all equal size
10 ctrl+w +           // increase current window size by 10 lines
10 ctrl+w -           // decrease current window size by 10 lines
10 ctrl+w >           // increase current window size horizontally by 10 characters
10 ctrl+w <           // decrease current window size horizontally by 10 characters
:vsplit file          // vertical split
:hide                 // close current window
ctrl+w o           // keep only this window open
:only                 // keep only this window open
ctrl+w T           // open the current window in its own tab
:ls                   // show current buffers
:b 2                  // open buffer #2 in this window
:new                  // open a new empty window
:new filename         // open filename in a new window

http://web.cs.swarthmore.edu/help/vim/windows.html

ctrl+x ctrl+l         // line completion (in insert mode)
:set dictionary=/usr/share/dict/words
ctrl+x ctrl+k         // dictionary completion
ctrl+w                // erase word (insert mode)
ctrl+u                // erase line (insert mode or command mode)
ctrl+w, ctrl+i        // go through jump location
[I                    // show lines with matching word under the cursor

mkview, loadview, mksession

vim -S Session.vim

Tag file for PHP:

ctags -R --exclude=".svn" --PHP-kinds=+cf --regex-PHP='/abstract class ([^ ]*)/\1/c/' 
--regex-PHP='/interface ([^ ]*)/\1/c/' --regex-PHP='/(public |static |abstract |protected |private )+function ([^ (]*)/\2/f/'

Position the cursor over a tag, and press CTRL-] to jump. To jump back, press CTRL-T

Misc:

10a#ESC    // append 10#'s after the next character

Plugins:

tidy.vim
caramel.vim
less.vim
commenter.vim
pdoc4vim.vim
taglist.vim
perl-support.vim
tagmenu.vim
tagexplorer.vim
comments.vim
perl.vim
ctag.vim
perl_synwrite.vim
context_complete.vim
perl prove.vim
folding
pleasant.vim
perlcritic.vim
perl_synwrite.vim // check perl syntax before allowing to write

HOWTO

How to comment out a block of Perl code

Press Ctrl+V and use the down/up arrow to select a block
Press I# followed by ESC

How to uncomment a block of Perl code

Press Ctrl+V and use the down/up arrow to select a block
Press x

How to view what changes you have made to the buffer before saving?

:%w tempfile
:diffsplit tempfile

How to always open files in tab?

vim -p file1 file2 file3

How to edit a file that start with a dash?

Preceed the filelist with a '--' (2 hyphens)

How to start vim in read-only mode?

Use the -R command line option.

How to prevent vim from accessing the shell?

Alias vim to  rvim, rview, rgvim, rgview, or vim -Z

How to prevent vim from using the ~/.viminfo fiel?

Specify -i NONE

How to make a script file?

Specifile -w {scriptfile} or -W {scriptfile}.  All the characters that you type will be recorded until you exit vim.

How to apply a script file?

Not Categorized:

@a       // execute the content of buffer a
.           // repeat last command
@@      // repeat last @
:@a
:set list // identify true space (tab is displayed as ^I, end of line is displayed as $ )
%         // find matching paren
:set showmatch        // show matching paren as you type
:! ctags *.c                // generate the tag file
:tag function_name   // look at the tag file, determine which file contain the function, and load that file
CTRL+R                      // redo
~                               // flip the case of selected text

Useful Vim plugins:

comments.vim. Download the file and put it in your ~/.vim/plugin directory. Ctrl+C to comments lines. Ctrl+X to uncomments lines.

perl-support
http://www.linuxjournal.com/article/7581
http://www.perlmonks.org/index.pl?node_id=257782
http://mamchenkov.net/wordpress/2004/05/10/vim-for-perl-developers/

Reference:

http://www.ukuug.org/events/linux2004/programme/paper-SMyers/Linux_2004_slides/vim_tips/
vi fast reference guide
vi
VIM QUICK REFERENCE CARD
http://www.pixelbeat.org/vim.tips.html
http://www.rayninfo.co.uk/vimtips.html
man vim, man vimtutor
http://clipboarded.blogspot.com/2007/10/vim-as-ide.html

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License