\documentclass[10pt]{article}
\usepackage{fullpage, setspace, multirow, longtable, graphicx, url, fancyhdr}
\usepackage[unicode]{hyperref}
\hypersetup{
  pdftoolbar=false,
  pdfmenubar=false,
  unicode=false,
  pdflang={en},
  pdftitle={Vim tips},
  pdfauthor={David Rayner, Gavin Gilmour},
  colorlinks=true,
  citecolor=black,
  filecolor=black,
  linkcolor=black,
  urlcolor=blue
}
\setlength{\parskip}{1ex}
\setlength{\parindent}{0ex}
\overfullrule=5pt

\pagestyle{fancyplain}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0.4pt}

\newenvironment{code}
{\begin{list}{}{\setlength{\leftmargin}{1em}}\item\scriptsize\bfseries}
{\end{list}}

\newenvironment{tinycode}
{\begin{list}{}{\setlength{\leftmargin}{1em}}\item\tiny\bfseries}
{\end{list}}

\begin{document}
\title{Vim tips}

\maketitle
\tableofcontents
\newpage

\section{Text manipulation}

\subsection{Searching}
\begin{center}
\begin{longtable}{l|l}
\hline
Command & Description \\
\hline
\endfirsthead
\endhead
/joe/e & cursor set to end of match\\
/joe/e+1 & cursor set to end of match plus 1\\
/joe/s-2 & cursor set to start of match minus 2\\
/\^{}joe.*fred.*bill/ & normal\\
/\^{}[A-J]$\backslash$+/ & search for lines beginning with one or more A-J\\
/begin$\backslash$\_.*end & search over possible multiple lines\\
/fred$\backslash$\_s*joe/i & any whitespace including newline\\
/fred$\backslash$$|$joe & search for fred or joe\\
/.*fred$\backslash$\&.*joe & search for fred and joe in any order\\
/$\backslash$$<$fred$\backslash$$>$/i & search for fred but not alfred or frederick\\
/$\backslash$$<$$\backslash$d$\backslash$d$\backslash$d$\backslash$d$\backslash$$>$ & search for exactly 4 digit numbers\\
/$\backslash$D$\backslash$d$\backslash$d$\backslash$d$\backslash$d$\backslash$D & search for exactly 4 digit numbers\\
/$\backslash$$<$$\backslash$d$\backslash$\{4\}$\backslash$$>$ & same thing\\
/$\backslash$([\^{}0-9]$\backslash$$|$\^{}$\backslash$)\%.*\% & search for absence of a digit or beginning of line\\
/\^{}$\backslash$n$\backslash$\{3\} & find 3 empty lines\\
/$\backslash$(fred$\backslash$).*$\backslash$(joe$\backslash$).*$\backslash$2.*$\backslash$1 &  using rexexp memory in a search \\
/\^{}$\backslash$([\^{},]*,$\backslash$)$\backslash$\{8\} &  using rexexp memory in a search \\
/$<$$\backslash$zs[\^{}$>$]*$\backslash$ze$>$ & search for tag contents, ignoring chevrons (:h /$\backslash$zs) \\
/$<$$\backslash$@$<$=[\^{}$>$]*$>$$\backslash$@= & search for tag contents, ignoring chevrons\\
/$<$$\backslash$@$<$=$\backslash$\_[\^{}$>$]*$>$$\backslash$@= & search for tags across possible multiple lines\\
/<!--\_p\{-\}--> & search for multiple line comments\\
/fred$\backslash$\_s*joe/i & any whitespace including newline ($\backslash$\_) \\
/bugs$\backslash$($\backslash$\_.$\backslash$)*bunny & bugs followed by bunny anywhere in file\\
:h $\backslash$\_ & help\\
:bufdo /searchstr/ & multiple file search (use :rewind to recommence search) \\
:bufdo \%s/searchstr/\&/gic & multiple file search better but cheating (n, then a to stop) \\
?\url{http://www.vim.org/} & search backwards for a URL without backslashing\\
/$\backslash$c$\backslash$v([\^{}aeiou]\&$\backslash$a)\{4\} & search for 4 consecutive consonants
\end{longtable}
\end{center}

\subsubsection{Search for declaration of subroutine/function under cursor}
:nmap gx yiw/$\backslash$(sub$\backslash$$<$bar$>$function$\backslash$)$\backslash$s$\backslash$+$<$C-R$>$"$<$CR$>$

\subsubsection{Search for visually highlighted text}
:vmap $<$silent$>$// y/$<$C-R$>$"$<$CR$>$\\
:vmap $<$silent$>$ //    y/$<$C-R$>$=escape(@", '$\backslash$$\backslash$/.*\^{}$\sim$[]')$<$CR$>$$<$CR$>$ (with spec chars)

\subsection{Substitution}
\begin{center}
\begin{longtable}{l|l}
 :\%s/fred/joe/igc & general substitute command\\
 :\%s/$\backslash$r//g & delete dos returns \^{}M\\
 :\%s/$\backslash$r/$\backslash$r/g & turn dos returns \^{}M into real returns (fixes joined lines) \\
 :\%s= *\$== & delete end of line blanks\\
 :\%s= $\backslash$+\$== & same as above\\
 :\%s\#$\backslash$s*$\backslash$r$\backslash$?\$\#\# & clean both trailing spaces AND dos returns\\
 :\%s\#$\backslash$s*$\backslash$r*\$\#\# & same thing deleting empty lines\\
 :\%s/\^{}$\backslash$n$\backslash$\{3\}// & delete blocks of 3 empty lines\\
 :\%s/\^{}$\backslash$n$\backslash$+/$\backslash$r/ & compressing empty lines\\
 :\%s\#$<$[\^{}$>$]$\backslash$+$>$\#\#g & delete html tags, leave text\\
 :'a,'bg/fred/s/dick/joe/igc & VERY USEFUL\\
 :\%s= [\^{} ]$\backslash$+\$=\&\&= & duplicate end column\\
 :\%s= $\backslash$f$\backslash$+\$=\&\&= & same as above\\
 :\%s= $\backslash$S$\backslash$+\$=\&\& & usually the same\\
 :s/$\backslash$(.*$\backslash$):$\backslash$(.*$\backslash$)/$\backslash$2 : $\backslash$1/ & reverse fields separated by :\\
 :\%s/\^{}$\backslash$(.*$\backslash$)$\backslash$n$\backslash$1\$/$\backslash$1/ & delete duplicate lines\\
 :\%s/\^{}.$\backslash$\{-\}pdf/new.pdf/ & delete to 1st pdf only\\
 :\%s\#$\backslash$$<$[zy]$\backslash$?tbl\_[a-z\_]$\backslash$+$\backslash$$>$\#$\backslash$L\&\#gc & lowercase with optional leading characters\\
 :\%s/// & delete possibly multi-line comments\\
 :help /$\backslash$\{-\} & help non-greedy\\
 :s/fred/a/g & sub ``fred'' with contents of register ``a''\\
 :s/fred/$\backslash$=@a/g & better alternative as register not displayed\\
 :\%s/$\backslash$f$\backslash$+$\backslash$.gif$\backslash$$>$/$\backslash$r\&$\backslash$r/g $|$ v/$\backslash$.gif\$/d $|$ \%s/gif/jpg/ & multiple commands on one line\\
 :\%s/a/but/gie$|$:update$|$:next & then use @: to repeat\\
 :\%s/suck$\backslash$$|$buck/loopy/gc & ORing (must break pipe)\\
 :s/\_\_date\_\_/$\backslash$=strftime(``\%c'')/ & insert datestring\\
 :\%s:$\backslash$($\backslash$($\backslash$w$\backslash$+$\backslash$s$\backslash$+$\backslash$)$\backslash$\{2\}$\backslash$)str1:$\backslash$1str2: & working with columns sub any str1 in col3\\
 :\%s:$\backslash$($\backslash$w$\backslash$+$\backslash$)$\backslash$(.*$\backslash$s$\backslash$+$\backslash$)$\backslash$($\backslash$w$\backslash$+$\backslash$)\$:$\backslash$3$\backslash$2$\backslash$1: & swapping first and last column (4 columns)\\
 :\%s/$\backslash$d$\backslash$+/$\backslash$=(submatch(0)-3)/ & decrement numbers by 3\\
 :g/loc$\backslash$$|$function/s/$\backslash$d/$\backslash$=submatch(0)+6/ & increment numbers by 6 on certain lines only\\
 :\%s\#txtdev$\backslash$zs$\backslash$d\#$\backslash$=submatch(0)+1\#g & better version of above\\
 :\%s/$\backslash$(gg$\backslash$)$\backslash$@$<$=$\backslash$d$\backslash$+/$\backslash$=submatch(0)+6/ & increment only numbers gg$\backslash$d$\backslash$d by 6 (another way)\\
 :let i=10 $|$ 'a,'bg/Abc/s/yy/$\backslash$=i/ $|$let i=i+1 & convert yy to 10,11,12 etc\\
 :let i=10 $|$ 'a,'bg/Abc/s/xx$\backslash$zsyy$\backslash$ze/$\backslash$=i/ $|$let i=i+1 & convert xxyy to xx11,xx12,xx13 (more presise) \\
 :\%s/''$\backslash$([\^{}.]$\backslash$+$\backslash$).*$\backslash$zsxx/$\backslash$1/ & find replacement text, use $\backslash$zs to simplify substitute\\
 :nmap z :\%s\#$\backslash$$<$=expand(``'')$\backslash$$>$\# & pull word under cursor into LHS of a substitute\\
 :vmap z :\%s/$\backslash$$<$*$\backslash$$>$/ & pull visually highlighted text into LHS of a substitute
\end{longtable}
\end{center}

\subsubsection{Filter all form elements into paste register}
:redir @*$|$sil exec 'g\#$<$$\backslash$(input$\backslash$$|$select$\backslash$$|$textarea$\backslash$$|$/$\backslash$=form$\backslash$)$\backslash$$>$\#p'$|$redir END \\
:nmap ,z :redir @*sil exec 'g@$<$$\backslash$(input$\backslash$select$\backslash$textarea$\backslash$/$\backslash$=form$\backslash$)$\backslash$$>$@p'redir END


\subsubsection{Substitue within substituion}
\begin{center}
\begin{longtable}{l|l}
:\%s,$\backslash$(all/.*$\backslash$)$\backslash$@$<$=/,\_,g & replace all / with \_ AFTER ``all/''\\
:s\#all/$\backslash$zs.*\#$\backslash$=substitute(submatch(0), '/', '\_', 'g')\# & same thing\\
:s\#all/\#\&\^{}M\#$|$s\#/\#\_\#g$|$-j! & sub by splitting line, re-joining\\
:\%s/.*/$\backslash$='cp '.submatch(0).' all/'.substitute(submatch(0),'/','\_','g')/ & sub inside sub
\end{longtable}
\end{center}

\subsubsection{Substituting a visual area}
\begin{center}
\begin{longtable}{l|l}
 :'$<$,'$>$s/Emacs/Vim/g & remember you DONT type the '$<$.'$>$\\
 gv & re-select the previous visual area (ULTRA)
\end{longtable}
\end{center}

\subsection{Global command display}
\begin{center}
\begin{longtable}{l|l}
 :g/gladiolli/\# & display with line numbers (YOU WANT THIS!)\\
 :g/fred.*joe.*dick/ & display all lines fred,joe \& dick\\
 :g/$\backslash$$<$fred$\backslash$$>$/ & display all lines fred but not freddy\\
 :g/\^{}$\backslash$s*\$/d & delete all blank lines\\
 :g!/\^{}dd/d & delete lines not containing string\\
 :v/\^{}dd/d & delete lines not containing string\\
 :g/fred/,/joe/d & not line based (very powerfull)\\
 :g/-------/.-10,.d & delete string \& 10 previous lines\\
 :g/\{/ ,/\}/- s/$\backslash$n$\backslash$+/$\backslash$r/g & delete empty lines but only between \{...\}\\
 :v/$\backslash$S/d & delete empty lines (both types)\\
 :v/./,/./-j & compress empty lines\\
 :g/\^{}\$/,/./-j & compress empty lines\\
 :g/$<$input$\backslash$$|$$<$form/p & ORing\\
 :g/\^{}/put\_ & double space file (pu = put)\\
 :g/\^{}/m0 & reverse file (m = move)\\
 :'a,'b/\^{}/m'b & reverse a section a to b\\
 :g/\^{}/t. & duplicate every line\\
 :g/fred/t\$ & copy lines matching fred to EOF\\
 :g/stage/t'a & copy lines matching stage to marker a\\
 :g/$\backslash$(\^{}I[\^{}\^{}I]*$\backslash$)$\backslash$\{80\}/d & delete all lines containing at least 80 tabs\\
 :'a,'bg/somestr/co/otherstr/ & match all lines containing ``somestr'' between markers a \& b\\
 :'a,'bg/str1/s/str1/\&\&\&/$|$mo/str2/ & as above but also do a substitution\\
 :\%norm jdd & delete every other line\\
 :.,\$g/\^{}$\backslash$d/exe ``norm! $\backslash$''& increment numbers\\
 :'a,'bg/$\backslash$d$\backslash$+/norm! \^{}A & increment numbers\\
 :g/fred/y A & append all lines fred to register a (empty reg a first with qaq.) \\
 :g/fred/y A $|$ :let @*=@a & put into paste buffer\\
 :let @a=''$|$g/Barratt/y A $|$:let @*=@a\\
 :'a,'b g/\^{}Error/ . w $>$$>$ errors.txt & write out to errors.txt\\
 :g/./yank$|$put$|$-1s/'/''/g$|$s/.*/Print '\&'/ & duplicate every line in a file wrap a print '' around each duplicate\\
 :g/\^{}MARK\$/r tmp.ex $|$ -d & replace string with contents of a file, -d deletes the ``mark''\\
 :g//z\#.5 & display with context\\
 :g//z\#.5$|$echo ``=========='' & display beautifully\\
 :g/$|$/norm 2f$|$r* & replace 2nd $|$ with a star\\
 :nmap  :redir @a:g//:redir END:new:put! a  & send output of previous global command to a new window
\end{longtable}
\end{center}

\subsection{Global combined with substitute (power editing)}
\begin{center}
\begin{longtable}{l|l}
 :'a,'bg/fred/s/joe/susan/gic & can use memory to extend matching\\
 :g/fred/,/joe/s/fred/joe/gic & non-line based (ultra)\\
 :/fred/;/joe/-2,/sid/+3s/sally/alley/gIC & find fred before beginning search for joe
\end{longtable}
\end{center}

\subsection{Changing case}
\begin{center}
\begin{longtable}{l|l}
guu & lowercase line\\
gUU & uppercase line\\
Vu & lowercase line\\
VU & uppercase line\\
g$\sim$$\sim$ & flip case line\\
vEU & upper case word\\
vE$\sim$ & flip case word\\
ggguG & lowercase entire file\\
vmap ,c :s/$\backslash$$<$$\backslash$(.$\backslash$)$\backslash$($\backslash$k*$\backslash$)$\backslash$$>$/$\backslash$u$\backslash$1$\backslash$L$\backslash$2/g & titlise visually selected text (map for .vimrc)\\
:\%s/[.!?]$\backslash$\_s$\backslash$+$\backslash$a/$\backslash$U\&$\backslash$E/g & uppercase first letter of sentences\\
g$<$C-G$>$ & count words in text file
\end{longtable}
\end{center}

\subsection{Reformatting text}
\begin{center}
\begin{longtable}{l|l}
 gq\} & format a paragraph\\
 gqap & format a paragraph\\
 ggVGgq & reformat entire file\\
 Vgq & current line\\
 :s/.$\backslash$\{,69$\backslash$\};$\backslash$s*$\backslash$$|$.$\backslash$\{,69$\backslash$\}$\backslash$s$\backslash$+/\&$\backslash$r/g & break lines at 70 chars, if possible after a ';'\\
\end{longtable}
\end{center}

\subsection{Deletion without destroying buffer}
\begin{center}
\begin{longtable}{l|l}
 ``\_d & what you've ALWAYS wanted\\
 ``\_dw & delete word (use blackhole)
\end{longtable}
\end{center}

\subsection{Essential}
\begin{center}
\begin{longtable}{l|l}
* \# g* g\# & find word under cursor () (forwards/backwards)\\
\% & match brackets and tags \{\}, [], (), etc. \\
. & repeat last modification \\
@: & repeat last : command (then @@)\\
$<$C-N$>$$<$C-P$>$ & word completion in insert mode\\
$<$C-X$>$$<$C-L$>$ & line complete SUPER USEFUL\\
/$<$C-R$>$$<$C-W$>$ & pull onto search/command line\\
/$<$C-R$>$$<$C-A$>$ & pull onto search/command line\\
:set ignorecase & you nearly always want this\\
:syntax on & colour syntax in perl, HTML, PHP etc. \\
:h regexp & list all help topics containing regexp (TAB to step through list)\\
:nmap ,s :source \$VIM/\_vimrc & read from vimrc\\
:nmap ,v :e \$VIM/\_vimrc & open and edit local vimrc \\
:vmap sb "zdi$<$b$>$$<$C-R$>$z$<$/b$>$$<$ESC$>$ & wrap $<$b$>$$<$/b$>$ around VISUALLY selected text\\
:vmap st "zdi$<$?= $<$C-R$>$z ?$>$$<$ESC$>$ & wrap $<$?=   ?$>$ around VISUALLY selected text
\end{longtable}
\end{center}

\section{File Manipulation}

\subsection{Exploring}
\begin{center}
\begin{longtable}{l|l}
 :Exp(lore) & file explorer (note: capital E)\\
 :Sex(plore) & file explorer in split window\\
 :ls & list of buffers\\
 :cd .. & move to parent directory\\
 :args & list of files\\
 :lcd \%:p:h & change to directory of current file\\
 :autocmd BufEnter * lcd \%:p:h & change to directory of current file automatically \footnote{Script required: bufexplorer.vim \url{http://www.vim.org/script.php?script}\_id=42} (put in \_vimrc)\\
 $\backslash$be & buffer explorer list of buffers\\
 $\backslash$bs & buffer explorer (split window)
\end{longtable}
\end{center}

\subsection{Opening files \& other tricks}
\begin{center}
\begin{longtable}{l|l}
 gf & open file name under cursor (SUPER)\\
 :nnoremap gF :view  & open file under cursor, create if necessary\\
 ga & display hex,ascii value of char under cursor\\
 ggVGg? & rot13 whole file\\
 ggg?G & rot13 whole file (quicker for large file)\\
 :8 $|$ normal VGg? & rot13 from line 8\\
 :normal 10GVGg? & rot13 from line 8\\
 , & increment, decrement number under cursor\\
 =5*5 & insert 25 into text (mini-calculator)\\
 :e main\_ & tab completes\\
 main\_ & include NAME of file in text (insert mode)
\end{longtable}
\end{center}

\subsection{Multiple files management}
\begin{center}
\begin{longtable}{l|l}
 :bn & goto next buffer\\
 :bp & goto previous buffer\\
 :wn & save file and move to next (super)\\
 :wp & save file and move to previous\\
 :bd & remove file from buffer list (super)\\
 :bun & buffer unload (remove window but not from list)\\
 :badd file.c & file from buffer list\\
 :b 3 & go to buffer 3\\
 :b main & go to buffer with main in name eg main.c (ultra)\\
 :sav php.html & save current file as php.html and ``move'' to php.html\\
 :sav! \%$<$.bak & save current file to alternative extension (old way)\\
 :sav! \%:r.cfm & save current file to alternative extension\\
 :sav \%:s/fred/joe/ & do a substitute on file name\\
 :sav \%:s/fred/joe/:r.bak2 & do a substitute on file name \& ext.\\
 :!mv \% \%:r.bak & rename current file (DOS use rename or del)\\
 :e! & return to unmodified file\\
 :w c:/aaa/\% & save file elsewhere\\
 :e \# & edit alternative file (also cntrl-\^{})\\
 :rew & return to beginning of edited files list (:args)\\
 :brew & buffer rewind\\
 :sp fred.txt & open fred.txt into a split\\
 :sball,:sb & split all buffers (super)\\
 :scrollbind & in each split window\\
 :map $<$F5$>$ :ls$<$CR$>$:e \# & pressing F5 lists all buffers, just type number\\
 :set hidden & allows to change buffer w/o saving current buffer
 \end{longtable}
\end{center}

\subsection{File-name manipulation}
\begin{center}
\begin{longtable}{l|l}
 :h filename-modifiers & help\\
 :w \% & write to current file name\\
 :w \%:r.cfm & change file extention to .cfm\\
 :!echo \%:p & full path \& file name\\
 :!echo \%:p:h & full path only\\
 $<$C-R$>$\% & insert filename (insert mode)\\
 ``\%p & insert filename (normal mode)\\
 /$<$C-R$>$\% & search for file name in text
\end{longtable}
\end{center}

\subsection{Command over multiple files}
\begin{center}
\begin{longtable}{l|l}
 :argdo \%s/foo/bar/e & operate on all files in :args\\
 :bufdo \%s/foo/bar/e\\
 :windo \%s/foo/bar/e\\
 :argdo exe '\%!sort'$|$w! & include an external command
\end{longtable}
\end{center}

\subsection{Sessions (set of files)}
\begin{center}
\begin{longtable}{l|l}
 gvim file1.c file2.c lib/lib.h lib/lib2.h & load files for ``session''\\
 :mksession & create a session file (default session.vim)\\
 gvim -S Session.vim & reload all files
\end{longtable}
\end{center}

\subsection{Modelines}
\begin{center}
\begin{longtable}{l|l}
 vim:noai:ts=2:sw=4:readonly: & makes readonly\\
 vim:ft=html: & says use HTML syntax highlighting\\
 :h modeline & help with modelines
\end{longtable}
\end{center}

\subsubsection{Creating your own GUI Toolbar entry}
amenu Modeline.Insert$\backslash$ at$\backslash$ VIMt$\backslash$ modeline $<$Esc$>$$<$Esc$>$ggOvim:ff=unix ts=4 ss=4\\
$<$CR$>$vim60:fdm=marker$<$Esc$>$gg\\\\
(All on one line!)

\subsection{Markers \& moving about}
\begin{center}
\begin{longtable}{l|l}
 '. & jump to last modification line (SUPER)\\
 `. & jump to exact spot in last modification line\\
 g; & cycle through recent changes (oldest first) \footnote{(new in vim 6.3)}\\
 g, & reverse direction \footnote{(new in vim 6.3)}\\
 :changes & show entire list of changes\\
 :h changelist & help for above\\
 $<$C-O$>$ & retrace your movements in file (starting from most recent)\\
 $<$C-I$>$ & retrace your movements in file (reverse direction)\\
 :ju(mps) & list of your movements\\
 :help jump-motions & explains jump motions\\
 :history & list of all your commands\\
 :his c & commandline history\\
 :his s & search history\\
 q/ & search history window (puts you in full edit mode)\\
 q: & commandline history window (puts you in full edit mode)\\
 : & history Window
 \end{longtable}
\end{center}

\subsection{Editing/moving within insert mode}
\begin{center}
\begin{longtable}{l|l}
$<$C-U$>$                             & delete all entered\\
$<$C-W$>$                             & delete last word\\
$<$HOME$>$$<$END$>$                   & beginning/end of line\\
$<$C-LEFTARROW$>$$<$C-RIGHTARROW$>$   & jump one word backwards/forwards\\
$<$C-X$>$$<$C-E$>$,$<$C-X$>$$<$C-Y$>$  & scroll while staying put in insert
\end{longtable}
\end{center}

\subsection{Abbreviations \& maps}
\begin{center}
\begin{longtable}{l|l}
:map $<$f7$>$   :'a,'bw! c:/aaa/x\\
:map $<$f8$>$   :r c:/aaa/x\\
:map $<$f11$>$  :.w! c:/aaa/xr$<$CR$>$\\
:map $<$f12$>$  :r c:/aaa/xr$<$CR$>$\\
:ab php & list of abbreviations beginning php\\
:map , & list of maps beginning ,\\
set wak=no & allow use of F10 for win32 mapping (:h winaltkeys)\\
$<$CR$>$             & enter\\
$<$ESC$>$            & escape\\
$<$BACKSPACE$>$      & backspace\\
$<$LEADER$>$         & backslash\\
$<$BAR$>$            & $|$\\
$<$SILENT$>$         & execute quietly\\
iab phpdb exit("$<$hr$>$Debug $<$C-R$>$a  "); & yank all variables into register a
\end{longtable}
\end{center}

\subsubsection{Display RGB colour under the cursor eg \#445588}
:nmap $<$leader$>$c :hi Normal guibg=\#$<$c-r$>$=expand("$<$cword$>$")$<$cr$>$$<$cr$>$

\section{Registers}

\subsection{List your registers}
\begin{center}
\begin{longtable}{l|l}
:reg & display contents of all registers\\
:reg a & display content of individual registers\\
``1p.... & retrieve numeric registers one by one\\
:let @y='yy@''' & pre-loading registers (put in .vimrc)\\
qqq & empty register ``q''\\
:let @a=@\_ & clear register a\\
:let @a=''`` & clear register a\\
:let @*=@a & copy register a to paste buffer\\
map $<$F11$>$ ``qyy:let @q=@q.''zzz''
\end{longtable}
\end{center}

\subsection{Appending to registers}

Yank 5 lines into ``a'' then add a further 5\\
\begin{enumerate}
\item ``a5yy
\item 10j
\item ``A5yy
\end{enumerate}

\subsection{Using a register as a map (preload registers in .vimrc)}
\begin{center}
\begin{longtable}{l|l}
 :let @m='':'a,'bs/''\\
 :let @s='':\%!sort -u''
 \end{longtable}
\end{center}

\subsection{Redirection \& paste register}
\begin{center}
\begin{longtable}{l|l}
 :redir @* & redirect commands to paste buffer\\
 :redir END & end redirect\\
 :redir $>$$>$ out.txt & redirect to a file\\
 ``*yy & yank to paste\\
 ``*p & insert from paste buffer\\
 :'a,'by* & yank range into paste\\
 :\%y* & yank whole buffer into paste\\
 :.y* & yank current line to paster\\
 :nmap p :let @* = substitute(@*,'[\^{}[:print:]]','','g')''*p & filter non-printable characters\
\end{longtable}
\end{center}

\subsubsection{Copy full path name}
unix: nnoremap $<$F2$>$ :let @*=expand(``\%:p'')\\
win32: nnoremap $<$F2$>$ :let @*=substitute(expand(``\%:p''), ``/'', ``$\backslash$$\backslash$'', ``g'')

\subsection{Useful tricks}
\begin{center}
\begin{longtable}{l|l}
 ``ayy@a & execute ``vim command'' in a text file\\
 yy@'' & same thing using unnamed register\\
 u@. & execute command JUST typed in\\
 :norm qqy\$jq &  paste ``normal commands'' without entering insert mode
 \end{longtable}
\end{center}

\section{Advanced}

\subsection{Command line tricks}
\begin{center}
\begin{longtable}{l|l}
cat xx $|$ gvim - -c ``v/\^{}$\backslash$d$\backslash$d$\backslash$$|$\^{}[3-9]/d `` & filter a stream\\
ls $|$ gvim - & edit a stream!\\
gvim \url{ftp://www.somedomain.com/index.html} & uses netrw.vim\\
gvim -h & help\\
gvim -o file1 file2 & open into a split\\
gvim -c ``/main'' joe.c & open joe.c \& jump to ``main''\\
gvim -c ``\%s/ABC/DEF/ge $|$ update'' file1.c & execute multiple command on a single file\\
gvim -c ``argdo \%s/ABC/DEF/ge $|$ update'' *.c & execute multiple command on a group of files\\
gvim -c ``argdo /begin/+1,/end/-1g/\^{}/d $|$ update'' *.c & remove blocks of text from a series of files\\
gvim -s ``convert.vim'' file.c & automate editing of a file (ex commands in convert.vim)\\
gvim -u NONE -U NONE -N & load vim without .vimrc and plugins (clean vim)\\
gvim -c 'normal ggdG''*p' c:/aaa/xp & access paste buffer contents (put in a script/batch file)\\
gvim -c 's/\^{}/$\backslash$=@*/$|$hardcopy!$|$q!' & print paste contents to default printer\\
gvim -d file1 file2 & vimdiff (compare differences)\\
dp & ``put'' difference under cursor to other file\\
do & ``get'' difference under cursor from other file\\
:grep somestring *.php & internal grep creates a list of all matching files\\
:h grep & use :cn(ext) :cp(rev) to navigate list
\end{longtable}
\end{center}

\subsection{External programs}
\begin{center}
\begin{longtable}{l|l}
 :r!ls.exe & reads in output of ls\\
 !!date & same thing (but replaces/filters current line)\\
 :\%!sort -u & sort unique content\\
 :'a,'b!sort -u & as above\\
 !1\} sort -u & sorts paragraph (note normal mode!!)\\
 map $<$F9$>$ :w:!c:/php/php.exe \% & run file through php\\
 map $<$F2$>$ :w:!perl -c \% & run file through perl\\
 :runtime! syntax/2html.vim & convert txt to html
 \end{longtable}
\end{center}

\subsection{Recording}
\begin{center}
\begin{longtable}{l|l}
qq & record to q\\
q & end recording\\
@q & to execute\\
@@ & to repeat\\
5@@ & to repeat 5 times\\
``qp & display contents of register q (normal mode)\\
$<$ctrl-R$>$q & display contents of register q (insert mode)\\
''qdd & put changed contacts back into q\\
@q & execute recording/register q\\
nnoremap ] @l:wbd & combining a recording with a map (to end up in command mode)
\end{longtable}
\end{center}

 \subsubsection{Operating a Recording on a Visual BLOCK}

 \begin{enumerate}
 \item define recording/register

 \begin{list}{}
    \item qq:s/ to/ from/g\^{}Mq\
 \end{list}

 \item Define Visual BLOCK
 \begin{list}{}
    \item V\}
 \end{list}

 \item hit : and the following appears
 \begin{list}{}
    \item :'$<$,'$>$
 \end{list}

 \item Complete as follows
 \begin{list}{}
    \item :'$<$,'$>$norm @q
 \end{list}

 \end{enumerate}

\subsection{Quick jumping between splits}
map $<$C-J$>$ $<$C-W$>$j$<$C-W$>$\_ \\
map $<$C-K$>$ $<$C-W$>$k$<$C-W$>$\_

\subsection{Visual mode basics}
\begin{center}
\begin{longtable}{l|l}
v & enter visual mode\\
V & visual mode whole line\\
$<$C-V$>$ & enter VISUAL BLOCK mode\\
gv & reselect last visual area (ultra)\\
o & navigate visual area\\
``*y & yank visual area into paste buffer\\
V\% & visualise what you match\\
V\}J & join visual block (great)\\
V\}gJ & join visual block w/o adding spaces\\
0$<$C-V$>$10j2ld & delete first 2 characters of 10 successive lines
\end{longtable}
\end{center}

\subsection{vimrc essentials}
\begin{center}
\begin{longtable}{l|l}
set incsearch & jumps to search word as you type\\
set wildignore=*.o,*.obj,*.bak,*.exe & tab complete now ignores these\\
set shiftwidth=3 & for shift/tabbing\\
set vb t\_vb=''. & set silent (no beep!)\\
set browsedir=buffer & make 'open directory' use current directory\\
\end{longtable}
\end{center}

\subsubsection{Launching IE}
nmap ,f :update:silent !start c:$\backslash$progra$\sim$1$\backslash$intern$\sim$1$\backslash$iexplore.exe \url{file://}\%:p\\
nmap ,i :update: !start c:$\backslash$progra$\sim$1$\backslash$intern$\sim$1$\backslash$iexplore.exe

\subsubsection{Ftping from vim}
cmap ,r :Nread \url{ftp://209.51.134.122/public}\_html/index.html\\
cmap ,w :Nwrite \url{ftp://209.51.134.122/public}\_html/index.html

\subsubsection{Autocmd}
\begin{center}
\begin{longtable}{l|l}
autocmd bufenter *.tex map $<$F1$>$ :!latex \% & programming keys depending on file type\\
autocmd bufenter *.tex map $<$F2$>$ :!xdvi -hush \%$<$.dvi\&  & launch xdvi with current file dvi\\
autocmd BufRead * silent! \%s/[$\backslash$r $\backslash$t]$\backslash$+\$// & automatically delete whitespace, trailing dos returns\\
autocmd BufEnter *.php :\%s/[ $\backslash$t$\backslash$r]$\backslash$+\$//e & same but only for php files
\end{longtable}
\end{center}

\subsection{Conventional shifting and indenting}
\begin{center}
\begin{longtable}{l|l}
 :'a,'b$>$$>$ & conventional Shifting/Indenting\\
 :vnoremap $<$ $<$gv & visual shifting (builtin-repeat)\\
 :vnoremap $>$ $>$gv & visual shifting (builtin-repeat)\\
 $>$i\{ & block shifting (magic)\\
 $>$a\{\\
 $>$\%\\
 $<$\%
\end{longtable}
\end{center}

\subsection{Pulling objects onto command/search line}
\begin{center}
\begin{longtable}{l|l}
$<$C-R$>$$<$C-W$>$ & pull word under the cursor into a command line or search\\
$<$C-R$>$$<$C-A$>$ & pull WORD under the cursor into a command line or search\\
$<$C-R$>$-                  & pull small register (also insert mode)\\
$<$C-R$>$[0-9a-z]           & pull named registers (also insert mode)\\
$<$C-R$>$\%                  & pull file name (also \#) (also insert mode)\\
$<$C-R$>$=somevar           & pull contents of a variable (eg :let sray="ray[0-9]")
\end{longtable}
\end{center}

\subsection{Capturing output of current script}
\begin{center}
\begin{longtable}{l|l}
 :new $|$ r!perl \# & opens new buffer,read other buffer\\
 :new! x.out $|$ r!perl \# & same with named file\\
 :new+read!ls\\
 :new +put q$|$\%!sort & create a new buffer, paste a register ``q'' into it, then sort new buffer
\end{longtable}
\end{center}

\subsection{Inserting DOS carriage returns}
\begin{center}
\begin{longtable}{l|l}
 :\%s/\$/$\backslash$\&/g & that's what you type\\
 :\%s/\$/$\backslash$\&/g & for Win32\\
 :\%s/\$/$\backslash$\^{}M\&/g & what you'll see where \^{}M is ONE character\\
 :set list & display ``invisible characters''
\end{longtable}
\end{center}

\subsection{Perform an action on a particular file or file type}
autocmd VimEnter c:/intranet/note011.txt normal! ggVGg?\\
autocmd FileType *.pl exec('set fileformats=unix')\\

`` Retrieving last command line command for copy \& pasting into text\\
i:\\
`` Retrieving last Search Command for copy \& pasting into text\\
i/\

\subsection{Inserting line number}
 :g/\^{}/exec ``s/\^{}/''.strpart(line(``.'').'' ``, 0, 4)\\
 :\%s/\^{}/$\backslash$=strpart(line(``.'').'' ``, 0, 5)\\
 :\%s/\^{}/$\backslash$=line('.'). ' '

\subsection{Numbering lines}
\begin{center}
\begin{longtable}{l|l}
 :set number & show line numbers\\
 :map $<$F12$>$ :set number!$<$CR$>$ & map to toggle line numbers\\
 :\%s/\^{}/$\backslash$=strpart(line('.').'' ``,0,\&ts)\\
 :'a,'b!perl -pne 'BEGIN\{\$a=223\} substr(\$\_,2,0)=\$a++' & number lines starting from arbitrary number\\
 qqmnYP`n\^{}Aq & in recording q repeat with @q\\
 :.,\$g/\^{}$\backslash$d/exe ``normal! $\backslash$'' & increment existing numbers to end of file\\
 o23qqYpq40@q & generate a list of numbers 23-64
\end{longtable}
\end{center}

\subsection{Advanced incrementing}

\begin{code}

\begin{verbatim}
    let g:I=0
    function! INC(increment)
        let g:I =g:I + a:increment
        return g:I
     end function
\end{verbatim}
\end{code}

\subsubsection{Create list starting from 223 incrementing by 5 between markers a,b}
:let I=223\\
:'a,'bs/\^{}/$\backslash$=INC(5)/

\subsubsection{Create a map for INC}
cab viminc :let I=223 $\backslash$$|$ 'a,'bs/\$/$\backslash$=INC(5)/

\subsection{Digraphs (non alpha-numerics)}
\begin{center}
\begin{longtable}{l|l}
:digraphs                         & display table\\
:h dig                            & help\\
i$<$C-K$>$e'                      & enters é\\
i$<$C-V$>$233                     & enters é (Unix)\\
i$<$C-Q$>$233                     & enters é (Win32)\\
ga                                & View hex value of any character\\
:%s/\[$<$C-V$>$128-$<$C-V$>$255\]//gi       & where you have to type the Control-V\\
:%s/\[€-ÿ\]//gi                     & Should see a black square & a dotted y\\
:%s/\[$<$C-V$>$128-$<$C-V$>$255$<$C-V$>$01-$<$C-V$>$31\]//gi & All pesky non-asciis\\
%:exec "norm /\[\\x00-\\x1f\\x80-\\xff\]/"    & same thing\\
yl/$<$C-R$>$" &  Pull a non-ascii character onto search bar
\end{longtable}
\end{center}

\subsection{Complex vim}
\begin{center}
\begin{longtable}{l|l}
:\%s/$\backslash$$<$$\backslash$(on$\backslash$$|$off$\backslash$)$\backslash$$>$/$\backslash$=strpart(``offon'', 3 * (``off'' == submatch(0)), 3)/g & swap two words\\
:vnoremap $<$C-X$>$ $<$Esc$>$`.``gvP``P & swap two words
\end{longtable}
\end{center}

\subsection{Syntax highlighting}
\begin{center}
\begin{longtable}{l|l}
 :set syntax=perl & force Syntax coloring for a file that has no extension .pl\\
 :set syntax off & remove syntax coloring (useful for all sorts of reasons)\\
 :colorscheme blue & change coloring scheme (any file in $\sim$vim/vim??/colors)\\
 vim:ft=html: & force HTML Syntax highlighting by using a modeline\\
 :syn match DoubleSpace `` `` & example of setting your own highlighting\\
 :hi def DoubleSpace guibg=\#e0e0e0 & sets the editor background
\end{longtable}
\end{center}

\subsection{Preventions and security}
\begin{center}
\begin{longtable}{l|l}
 :set noma (non modifiable) & prevents modifications\\
 :set ro (Read Only) & protect a file from unintentional writes\\
 :X & encryption (do not forget your key!)
\end{longtable}
\end{center}

\subsection{Taglist}
\footnote{Script required: taglist.vim (\url{http://www.vim.org/scripts/script.php?script_id=273)}}
\begin{center}
\begin{longtable}{l|l}
:Tlist & display tags (list of functions)\\
$<$C-]$>$  & jump to function under cursor
\end{longtable}
\end{center}

\subsection{Folding}
\begin{center}
\begin{longtable}{l|l}
 zf\} & fold paragraph using motion\\
 v\}zf & fold paragraph using visual\\
 zf'a & fold to mark\\
 zo & open fold\\
 zc & re-close fold
\end{longtable}
\end{center}

\subsection{Renaming files}
 Rename files without leaving vim\\
 :r! ls *.c\\
 :\%s/$\backslash$(.*$\backslash$).c/mv \& $\backslash$1.bla\\
 :w !sh\\
 :q!

\subsection{Reproducing lines}
\begin{center}
\begin{longtable}{l|l}
 imap ] @@@hhkyWjl?@@@P/@@@3s & reproduce previous line word by word\\
 nmap ] i@@@hhkyWjl?@@@P/@@@3s & reproduce previous line word by word
\end{longtable}
\end{center}

\subsection{Reading MS-Word documents}
\footnote{Program required: Antiword (\url{http://www.winfield.demon.nl/})}
 :autocmd BufReadPre *.doc set ro\\
 :autocmd BufReadPre *.doc set hlsearch!\\
 :autocmd BufReadPost *.doc \%!antiword ``\%''

\subsection{Random functions}

\subsubsection{Save word under cursor to a file}

\begin{code}
\begin{verbatim}
    function! SaveWord()
        normal yiw
        exe ':!echo '.@0.' $>$$>$ word.txt'
    endfunction
\end{verbatim}
\end{code}

\subsubsection{Delete duplicate lines}

\begin{code}
\begin{verbatim}
    function! Del()
        if getline(``.'') == getline(line(``.'') - 1)
            norm dd
        endif
    endfunction
\end{verbatim}
\end{code}

\subsubsection{Columnise a CSV file for display}
:let width = 20\\
:let fill=' ' $|$ while strlen(fill) $<$ width $|$ let fill=fill.fill $|$ endwhile\\
:\%s/$\backslash$([\^{};]*$\backslash$);$\backslash$=/$\backslash$=strpart(submatch(1).fill, 0, width)/ge\\
:\%s/$\backslash$s$\backslash$+\$//ge

\begin{code}
\begin{verbatim}
    function! CSVH(x)
        execute 'match Keyword /\^{}$\backslash$([\^{},]*,$\backslash$)$\backslash$\{'.a:x.'\}$\backslash$zs[\^{},]*/'
        execute 'normal \^{}'.a:x.'f,'
    endfunction
\end{verbatim}
\end{code}

command! -nargs=1 Csv :call CSVH()\\
:Csv 5 : highlight fifth column

\subsection{Miscallaenous commands}
\begin{center}
\begin{longtable}{l|l}
 :scriptnames & list all plugins, \_vimrcs loaded (super)\\
 :verbose set history? & reveals value of history and where set\\
 :function & list functions\\
 :func SearchCompl & List particular function
\end{longtable}
\end{center}

\subsection{Vim traps}
\begin{list}{}
    \item In regular expressions you must backslash + (match 1 or more)
    \item In regular expressions you must backslash $|$ (or)
    \item In regular expressions you must backslash ( (group)
    \item In regular expressions you must backslash \{ (count)
\end{list}

\begin{center}
\begin{longtable}{l|l}
/fred$\backslash$+/ & matches fred/freddy but not free\\
/$\backslash$(fred$\backslash$)$\backslash$\{2,3\}/ & note what you have to break\\
/codes$\backslash$($\backslash$n$\backslash$|$\backslash$s$\backslash$)*where & normal regexp\\
/$\backslash$vcodes($\backslash$n$|$$\backslash$s)*where & very magic
\end{longtable}
\end{center}

\subsection{Help}
\begin{center}
\begin{longtable}{l|l}
 :h quickref & vim quick reference sheet (ultra)\\
 :h tips & vim's own tips help\\
 :h visual$<$C-D$>$$<$TAB$>$ & obtain list of all visual help topics\\
 :h ctrl$<$C-D$>$ & list help of all control keys\\
 :helpg uganda & grep help files use :cn, :cp to find next\\
 :h :r & help for :ex command\\
 :h CTRL-R & normal mode\\
 :h /$\backslash$r & what's $\backslash$r in a regexp (matches a $<$CR$>$)\\
 :h $\backslash$$\backslash$zs & double up backslash to find $\backslash$zs in help\\
 :h i\_CTRL-R & help for say $<$C-R$>$ in insert mode\\
 :h c\_CTRL-R & help for say $<$C-R$>$ in command mode\\
 :h v\_CTRL-V & visual mode\\
 :h tutor & vim tutor\\
 $<$C-[$>$, $<$C-T$>$  & Move back \& forth in help history\\
 gvim -h & vim command line help\\
 :helptags /vim/vim64/doc & rebuild all *.txt help files in /doc\\
 :help add-local-help
\end{longtable}
\end{center}

\section{Fun}
 :h 42\\
 :h holy-grail\\
 :h!\\
 vim -c ``:\%s\%s*\%Cyrnfr)fcbafbe[Oenz(Zbbyranne\%$|$:\%s)[[()])-)Ig$|$norm Vg?''

\subsection{Contact}

\subsubsection{Author (David Rayner)}
Please email any errors or further tips etc to \url{david(at)rayninfo.co.uk}\\
Updated version at \url{http://www.rayninfo.co.uk/vimtips.html}

\subsubsection{Maintainer (Gavin Gilmour)}
Please email any comments, suggestions including spelling, grammatical and/or
formatting issues with this document to \url{gavin(at)brokentrain.net}\\
Updated version available at: \url{http://gavin.brokentrain.net/projects/vimtips/vimtips.pdf}

\subsubsection{Source}
The source of this document is available at:
\url{http://github.com/gaving/vimtips/}.

\subsection{Links}
\begin{longtable}{l|l}
    \url{http://www.vim.org/} & Official site\\
    \url{http://chronos.cs.msu.su/vim/newsgroup.html} & Newsgroup and Usenet\\
    \url{http://groups.yahoo.com/group/vim} & Specific newsgroup\\
    \url{http://u.webring.com/hub?ring=vim} & VIM Webring\\
    \url{http://www.truth.sk/vim/vimbook-OPL.pdf} & Vim Book\\
    \url{http://vimdoc.sourceforge.net/} & Searchable VIM Doc\\
    \url{http://www.faqs.org/faqs/editor-faq/vim/} & VIM FAQ\\
\end{longtable}

\end{document}
