<?xml version='1.0'?>
<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.4//EN"
  "/usr/share/xml/docbook/schema/dtd/4.3/docbookx.dtd">

<book>

<bookinfo>
<title>Vim Cheat Sheet</title>
</bookinfo>

<chapter>
<title>Movement actions</title>

<para>

Moving the display, as opposed to moving the cursor, allows you to choose the part of the file that
displays in the current window.  For instance, the paragraph that you are reading is at the bottom
of the current display and you would like to bring it somewhere near the middle of the screen.  You
could issue a CTRL+D to page down half a screen or if the cursor is located somewhere in the
paragraph you might also issue a "zz" or "z." to bring the line pointed to by the cursor to the
middle of your display.

</para>

<table pgwide="1">
<title>Moving the display</title>
<tgroup cols="3">

<thead>
<row>
<entry>Actions</entry>
<entry>Results</entry>
<entry>Notes</entry>
</row>
</thead>

<tbody>
<row>
<entry>CTRL-D</entry>                                 
<entry>Down one half-screen</entry>
</row>
<row>
<entry>CTRL-E</entry>                                 
<entry>Down one line</entry>
</row>
<row>
<entry>CTRL-F</entry>                                 
<entry>Down one screen</entry>
</row>
<row>
<entry>CTRL-U</entry>                                 
<entry>Up one half-screen</entry>
</row>
<row>
<entry>CTRL-Y</entry>                                
<entry>Up one line</entry>
</row>
<row>
<entry>CTRL-B</entry>                               
<entry>Up one screen</entry>
</row>
<row>
<entry>z+enter</entry>                                
<entry>current line at top of the screen</entry>
</row>
<row>
<entry>88z+enter</entry>                              
<entry>line 88 at top of the screen</entry>
</row>
<row>
<entry>zt</entry>                                     
<entry>current line at top of the screen</entry>
</row>
<row>
<entry>zb</entry>                                     
<entry>current line at bottom of the screen</entry>
</row>
<row>
<entry>zz</entry>                                     
<entry>current line at center of the screen</entry>
</row>
<row>
<entry>z.</entry>                                     
<entry>current line at center of the screen</entry>
</row>
<row>
<entry>CTRL-G</entry>                                 
<entry>info about current location</entry>
<entry>Under gnu/screen escape the screen hotkey via "CTRL-G g"</entry>
</row>
<row>
<entry>1CTRL-G</entry>                                
<entry>more info</entry>
<entry>Under gnu/screen escape the screen hotkey via "1 CTRL-G g". This provides
       the full pathname to the file among other things.
</entry>
</row>
<row>
<entry>2CTRL-G</entry>                                
<entry>etc.</entry>
<entry>Under gnu/screen escape the screen hotkey via "2 CTRL-G g"
</entry>
</row>
<row>
<entry>gCTRL-G</entry>                                
<entry>more fun with current location</entry>
<entry>Since I normally run vim under gnu/screen and my screen "command key" is 
       CTRL-G I need to issue a "g CTRL-G g" to achieve the same result.
</entry>
</row>
<row>
<entry>&lt;</entry>                                    
<entry>shift left (use with visual select for block)</entry>
<entry>granted that these are not actions that affect the display since they 
       indent a line or a block of text by the shiftwidth specified.
</entry>
</row>
<row>
<entry>&gt;</entry>            
<entry>shift right (use with visual select for block)</entry>
<entry>to move a block seven positions to the left, it is first necessary to specify a 
       "shiftwidth" of seven (:set sw=7) .. move the cusor to the first line of the block,
       issue a CTRL+v to switch to visual line mode, issue a cursor movement command to position
       the cursor at the end of the block such as "12j", "}", ")" .. "G" etc. depending on the 
       circumstances and then hit the ">" right indent command.  It is also possible to set "sw"
       to one (":set sw=1") and issue a "7>" command to obtain the same result.
</entry>
</row>
</tbody>

</tgroup>
</table>

</chapter>

<chapter>
<title>Cursor movement actions</title>

<para>

Moving the cursor around is the number one skill when it comes to efficient editing.  These actions
should be mastered before anything else since vim offers and wide variety of methods to move the
cursor and a nimble cursor will dramatically increase your productivity.

</para>

<table pgwide="1">
<title>Moving the cursor</title>
<tgroup cols="3">

<thead>
<row>
<entry>Actions</entry>
<entry>Results</entry>
<entry>Notes</entry>
</row>
</thead>

<tbody>
<row>
<entry>W/w</entry>   
<entry>move forward one "word"</entry>
</row>
<row>
<entry>E/e</entry>		   
<entry>move forward one word</entry>
</row>
<row>
<entry>I</entry>		   
<entry>move cursor to beginning of line and switch to insert mode</entry>
</row>
<row>
<entry>A</entry>		   
<entry>move cursor to end of line and switch to insert mode</entry>
</row>
<row>
<entry>B</entry>		   
<entry>move back one "word"</entry>
</row>
<row>
<entry>gE</entry>		   
<entry>move back to end of previous word</entry>
</row>
<row>
<entry>0</entry>		   
<entry>move to column 0</entry>
</row>
<row>
<entry>^</entry>		   
<entry>move to beginning of line</entry>
</row>
<row>
<entry>$</entry>		   
<entry>move to end of line</entry>
</row>
<row>
<entry>-</entry>		   
<entry>move to beginning of previous line</entry>
</row>
<row>
<entry>fc</entry>		   
<entry>find character 'c' forward</entry>
</row>
<row>
<entry>Fc</entry>		   
<entry>find character 'c' backward</entry>
</row>
<row>
<entry>tc</entry>		   
<entry>idem fc, but position cursor before 'c'</entry>
</row>
<row>
<entry>Tc</entry>		   
<entry>idem Fc, but position cursor before 'c'</entry>
</row>
<row>
<entry>;</entry>		   
<entry>repeat latest fFtT command</entry>
</row>
<row>
<entry>'</entry>		   
<entry>repeat latest fFtT command (opposite direction)</entry>
</row>
<row>
<entry>G</entry>		   
<entry>move to last line</entry>
</row>
<row>
<entry>gg</entry>		   
<entry>move to first line</entry>
</row>
<row>
<entry>44G</entry>		   
<entry>move to line 44</entry>
</row>
<row>
<entry>44gg</entry>		   
<entry>move to line 44</entry>
</row>
<row>
<entry>:44 &lt;enter&gt;</entry>	   
<entry>ibid.</entry>
</row>
<row>
<entry>5000go</entry>	   
<entry>move to 5000th byte in the file</entry>
</row>
<row>
<entry>33%</entry> 		   
<entry>move to ~33% mark in the file</entry> 
</row>
<row>
<entry>CTRL-O</entry>	   
<entry>jump to previous location</entry>
</row>
<row>
<entry>&lt;TAB&gt;</entry>	   
<entry>jump to next location</entry>
</row>
<row>
<entry>''</entry>		   
<entry>jump to previous location</entry>
</row>
<row>
<entry>CTRL-D</entry>	   
<entry>remove one level of indentation when in insert mode</entry>
</row>
<row>
<entry>CTRL-T</entry>	   
<entry>add one level of indentation when in insert mode</entry>
</row>
<row>
<entry>^CTRL-D</entry>	   
<entry>go to column 0 for this line only - ie. for an #include</entry>
</row>
<row>
<entry>0CTRL-D</entry>         
<entry>go to column 0 for good - abandon indentation</entry>
</row>
<row>
<entry>H, M, L</entry>	   
<entry>move cursor to top (H).. middle (M) .. or bottom (L) of screen</entry>
</row>
<row>
<entry>CTRL-o</entry>	   
<entry>in insert mode - switch to command mode for next action</entry>
</row>
<row>
<entry>CTRL-o o/O</entry>	   
<entry>insert new line (below/above) and return ton insert mode</entry>
</row>
<row>
<entry>CTRL-o 0/$</entry>	   
<entry>move cursor to beginning/end of line</entry>
</row>
<row>
<entry>:ju</entry>		   
<entry>list jumplist - ~ history in a web browser</entry>
</row>
</tbody>

</tgroup>
</table>

</chapter>

<chapter>
<title>Using windows and buffers</title>

<para>

Vim has a built-in windowing mode that lets you split the screen both horizontally and
vertically.  This allows you make the the most of your screen's real estate.  Typically you could
display the contents of two or three different files as well as a help window and possibly the
"preview window".  Windows can be resized, giving you total control over your windowed display.
Combining these features gives you the flexibility that you would expect of a tiling window
manager.

</para>

<table pgwide="1">
<title>Windows and buffers.</title>
<tgroup cols="3">

<thead>
<row>
<entry>Actions</entry>
<entry>Results</entry>
<entry>Notes</entry>
</row>
</thead>

<tbody>
<row>
<entry>CTRL-w s</entry>
<entry>open new window (horizontally)</entry>
</row>
<row>
<entry>CTRL-w v</entry>
<entry>open new window (vertically)</entry>

<entry>With high resolution screens and a smallish font , splitting the display once vertically
       and twice horizontally can let you display up to six files or pages of text concurrently.

</entry>
</row>
<row>
<entry>CTRL-w n</entry>		      
<entry>edit new file in new window</entry>
</row>
<row>
<entry>CTRL-w c</entry>		      
<entry>close window</entry>
</row>
<row>
<entry>CTRL-w q</entry>   		    
<entry>close window</entry>
</row>
<row>
<entry>CTRL-w p</entry>		      
<entry>go to previous window</entry>
</row>
<row>
<entry>CTRL-w w</entry>		      
<entry>go to next window</entry>
</row>
<row>
<entry>CTRL-w t</entry>		      
<entry>go to top window</entry>
</row>
<row>
<entry>CTRL-w b</entry>		      
<entry>go to bottom window</entry>
</row>
<row>
<entry>CTRL-w h    (j,k,l)</entry>	      
<entry>go to one window to the left - ibid. j, k, l..</entry>
</row>
<row>
<entry>CTRL-w h    (J,K,L)</entry>	      
<entry>move current window to far left - ibid. j, k, l..</entry>
<entry>This can be particularly useful when you want want to switch from horizontal to 
       vertical split (and vice-versa).  If for instance, you have two windows in horizontal 
       split mode and your cursor is positioned in the top window, a [CTRL-w L] action
       will result in a switch to vertical split mode with the window where the cursor is located 
       positioned to the far right.
</entry>

</row>
<row>
<entry>CTRL-w r</entry>		      
<entry>rotate / switch windows</entry>
</row>
<row>
<entry>CTRL-w CTRL-o / CTRL-w o</entry>      
<entry>hides other windows - see :vert unhide below</entry>
</row>
<row>
<entry>CTRL-w =</entry>		      
<entry>makes all windows the same width/height</entry>
</row>
<row>
<entry>CTRL-w &lt;</entry>		      
<entry>decrease window width by one</entry>
</row>
<row>
<entry>CTRL-w &gt;</entry>		      
<entry>increase window width by one</entry>
</row>
<row>
<entry>CTRL-w 10&gt;</entry>		      
<entry>increase window width by ten</entry>
</row>
<row>
<entry>CTRL-w 33 }</entry>		      
<entry>open preview window for tag under cursor (window height = 33)</entry>
</row>
<row>
<entry>CTRL-w z</entry>		      
<entry>close the preview window</entry>
</row>
<row>
<entry>:vne</entry>			      
<entry>open empty vertical window</entry>
</row>
<row>
<entry>:vert h ruler</entry>		      
<entry>open vertical window for help on 'ruler'</entry>
</row>
<row>
<entry>:vert unhide</entry>		      
<entry>reveal hidden buffers in vertical windows</entry>
</row>
<row>
<entry>:vert unhide 3</entry>		      
<entry>limit windows to 3</entry>
</row>
<row>
<entry>:ls</entry> 			      
<entry>list buffers</entry>
</row>
<row>
<entry>:ls!</entry>			      
<entry>list all buffers including help etc.</entry>
</row>
<row>
<entry>:b n</entry>			      
<entry>show buffer #n</entry>
</row>
<row>
<entry>:bd n</entry>			      
<entry>delete buffer #n</entry>
</row>
<row>
<entry>$ vim -O lilo.conf{,.sav}</entry>    	      
<entry>edit lilo.conf &amp; lilo.conf.sav in two vsplit windows</entry>
</row>
<row>
<entry>$ vimdiff -O lilo.conf{,.sav}</entry>	      
<entry>vimdiff lilo.conf &amp; lilo.conf.sav in two vsplit windows</entry>
</row>
<row>
<entry>$ vim main.c</entry>			      
<entry>start vim on main.c</entry>		       
</row>
<row>
<entry>:vne	function.c</entry>		      
<entry>open source file with functions</entry>	       
</row>
<row>
<entry>CTRL-W r / CTRL-W w</entry>		      
<entry>etc..</entry> 	 			       
</row>
</tbody>

</tgroup>
</table>
</chapter>

<chapter>
<title>Editing files.</title>

<para>

Vim has a slew of simple commands that make entering new data or making changes
to existing files an absolute breeze.. Once you have learned them to the point
where you only have to think about what you want to do and no longer how to
do it, you're in business.

</para>

<table pgwide="1">
<title>Editing.</title>
<tgroup cols="3">

<thead>
<row>
<entry>Actions</entry>
<entry>Results</entry>
<entry>Notes</entry>
</row>
</thead>

<tbody>
<row>
<entry>.</entry>				  
<entry>repeat previous action</entry>

<entry>Vim remembers the last action that you performed on the buffer, such as
pasting, deleting, etc. and issuing the minimal "." command, possibly after
moving your cursor to a different location, asks Vim to repeat this command. 

</entry>
</row>
<row>
<entry>i</entry>				  
<entry>insert at cursor position</entry>
</row>
<row>
<entry>a</entry>				  
<entry>insert after cursor</entry>
</row>
<row>
<entry>r</entry>				  
<entry>replace character under cursor</entry>
</row>
<row>
<entry>s</entry>				  
<entry>delete character under cursor / switch to insert mode</entry>
</row>
<row>
<entry>x</entry>				  
<entry>delete character under cursor / stay in normal mode</entry>
</row>
<row>
<entry>o</entry>				  
<entry>insert new line below cursor</entry>
</row>
<row>
<entry>O</entry>				  
<entry>insert new line above cursor</entry>
</row>
<row>
<entry>A</entry>				  
<entry>jump to end of line and switch to insert mode</entry>
</row>
<row>
<entry>C</entry>				  
<entry>delete to end of line / switch to insert mode</entry>
</row>
<row>
<entry>D</entry>				  
<entry>delete to eol</entry>
</row>
<row>
<entry>I</entry>				  
<entry>jump to beginning of line and switch to insert mode</entry>
</row>
<row>
<entry>R</entry>				  
<entry>switch to overstrike (replace) mode</entry>
</row>
<row>
<entry>cw</entry>				  
<entry>change word</entry>
</row>
<row>
<entry>dw</entry>				  
<entry>delete word</entry>
</row>
<row>
<entry>u</entry>				  
<entry>undo last edit</entry>
</row>
<row>
<entry>CTRL-r</entry>			  
<entry>redo last (undone) editing</entry>
</row>
<row>
<entry>CTRL-p</entry>			  
<entry>word completion</entry>
</row>
<row>
<entry>~</entry>				  
<entry>change case</entry>
</row>
<row>
<entry>ve~</entry>				  
<entry>highlight to end of word and (un)capitalize word</entry>
</row>
<row>
<entry>CTRL-[</entry>			  
<entry>ESC - back to command mode</entry>
</row>
<row>
<entry>gq}</entry>				  
<entry>rewrap to end of paragraph using (cf. textwidth)</entry>
</row>
<row>
<entry>gqap</entry>				  
<entry>rewrap current paragraph</entry>
</row>
<row>
<entry>gqip</entry>				  
<entry>rewrap current paragraph</entry>
</row>
<row>
<entry>V.. CTRL-V.. etc. + gq</entry>	  
<entry>select &amp; rewrap highlighted text</entry> 
</row>
<row>
<entry>gg.. V.. G.. :!par 72gqrj</entry>	  
<entry>select beginning->end and justify w/ linewidth=72</entry> 
</row>
<row>
<entry>:g/^#/d</entry>			  
<entry>delete every line that has a # in col. 1 (comments usually)</entry>
</row>
<row>
<entry>:v/^#/d</entry>			  

<entry>delete every line that does NOT have a # in col. 1 :g/:v execute the
command ('d' - delete above) for all lines that match / don't match
</entry>
</row>

<row>
<entry>:%s/^..//</entry>			  

<entry>destructive shift of contents of buffer - 2 cols to the left any two
characters (..) at the beginning of the line (^) are replaced with 'nothing'
(//).  a different approach: gg^VGld (visual block mode)
</entry>
</row>

<row>
<entry>:set shiftwidth=4</entry>		  
<entry>override default indentation of 8</entry> 
</row>
<row>
<entry>:set textwidth=80</entry>		  
<entry>set width of text = 80 columns</entry>
</row>
<row>
<entry>:set wrap/nowrap</entry>		  
<entry>toggle wrapping</entry>
</row>
<row>
<entry>:set virtualedit+=block</entry>	  

<entry>visual block mode: allows selection of rectangles beyond the end of the
logical line - so the blocks are rectangles
</entry>
</row>
</tbody>

</tgroup>
</table>
</chapter>

<chapter>
<title>Searching and optionally replacing content.</title>

<para>

Vim supports both trivial search for a string and complex regular expressions.

</para>

<table pgwide="1">
<title>Searching and replacing.</title>
<tgroup cols="3">

<thead>
<row>
<entry>Actions</entry>
<entry>Results</entry>
<entry>Notes</entry>
</row>
</thead>

<tbody>
<row>
<entry>*</entry>
<entry>search forward for word under cursor</entry>
</row>
<row>
<entry>#</entry>		 
<entry>search backward for word under cursor'</entry>
</row>
<row>
<entry>g*</entry>		 
<entry>ibid * - but not limited to a 'word' - eg. not/nothing</entry>
</row>
<row>
<entry>g#</entry>		 
<entry>ibid # - but not limited to a 'word' - eg. not/nothing</entry>
</row>
<row>
<entry>/string</entry>		 
<entry>search forward for "string"</entry> 
</row>
<row>
<entry>/string\c</entry>		 
<entry>search forward for "string" - ignore case</entry>
</row>
<row>
<entry>?string</entry>		 
<entry>search forward for "string"</entry> 
</row>
<row>
<entry>n/N</entry>			 
<entry>search for next/previous occurrence</entry>
</row>
<row>
<entry>/\&lt;I\&gt;</entry>		 
<entry>search for occurrences of the *word* 'I' - '&lt;' and '&gt;' must be escaped</entry>
</row>
<row>
<entry>:noh</entry>
<entry>"nohighlight" - remove those often annoying reverse video highlights</entry>
</row>
<row>
<entry>:%s/oldstring/newstring/g</entry>	 
<entry>change all occurrencs of 'oldstring' to 'newstring'</entry>
</row>
<row>
<entry>:%s/\(.*\).jpg/mv &amp; \1-pic.png/ </entry>
<entry>w/ regex - lets you change all 'xxx.jpg' to 'mv xxx.jpg
       xxx-pic.jpg' the resulting file can be sourced to rename files en-masse.</entry> 
<entry>note: when reversing the above I found awk a lot easier:
       $ awk '{print $1, $3, $2}' /tmp/fndc0 > /tmp/fndc1</entry>
</row>
<row>
<entry>:helpgrep &lt;regex&gt;</entry>	   
<entry>search for all occurrences of &lt;regex&gt; in help files</entry>
</row>
<row>
<entry>:cope</entry>			   
<entry>open window with all matches</entry>
<entry>this does not seem to work with search matches .. only with "errors".  In that case 
       this command and the following only concern the "quickfix" window should be moved 
       to a separate chapter: The "edit-compile-edit" cycle.</entry>
</row>
<row>
<entry>:ccl</entry>				   
<entry>close the window -- AKA the quickfix window</entry>
</row>
<row>
<entry>**tip**</entry>			   
<entry>[CTRL-W K] switches to a nice vertically split view with the list
       of matches to the right and the current article to the left.</entry>
</row>

</tbody>
</tgroup>
</table>

</chapter>

</book>
