# $Id: vi_cmds.txt 1185 2008-07-17 19:40:48Z tjn $ +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ [CScope] * Install cscope, e.g., sudo apt-get install cscope * Add a mapping file to the VIM dir ("$HOME/.vim/"), e.g., Downloaded (18apr2008) from: http://cscope.sourceforge.net/cscope_vim_tutorial.html $HOME/.vim/cscope_maps.vim * Add this to VIM config file (I use old style "$HOME/.exrc") " CScope for C/C++ cross-referencing! " Note, must install cscope, have compiled support in VIM, and source " following file. " See also: http://cscope.sourceforge.net/cscope_vim_tutorial.html if has("cscope") && filereadable("/usr/bin/cscope") source $HOME/.vim/cscope_maps.vim endif +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ [Split screens] # Split current file into two views :split # To close a split view (keeping file open) :q # Make current window only one on screen :only OR :on # only show window of current file # Move cursor to window below/right of current one -W w # move to below/right window (split screen) -W -W # move to below/right window (split screen) # Move cursor to below window - j # move to below window (split screen) -W -J # move to below window (split screen) # Move cursor to above window - k # move to above window (split screen) -W -K # move to above window (split screen) See also: * VIM documentation ":help windows.txt" * VIM Tip#173 'Switch between splits very fast (for multi-file editing)' http://vim.sourceforge.net/tips/tip.php?tip_id=173 * VIM Tip#305 "Best of VIM Tips (VIM's best Features)" http://vim.sourceforge.net/tips/tip.php?tip_id=305 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ [Editing Multiple Buffersfiles] # list of currently opened buffers (files) :ls OR :files # open another buffer :o FILE.txt # Switch to next/previous buffer (file) :bnext OR :bn # next buffer (file) :bprev OR :bp # prev buffer (file) # Switch to next/prev file (seems to only work if opened like vi f1.txt f2.txt) :next OR :n :prev # Jump to a given buffer (file) number, several different ways 1) :buffer # this will iterage through open buffers (files) 2) + -^ # Note, i've noticed is file is closed (:bdelete), # this method will reopen the file!?! :) 3) :sbuffer # will open split screen # Closing a given buffer (file) -- not entire VIM session! :bdelete OR :bd # "delete"/close a buffer & del from buf list # Closing current buffer (file) -- not entire VIM session! :bdelete OR :bd # "delete"/close cur buffer & del from buffer list See also: * VIM documentation ":help windows.txt" * VIM Tip#135 'Vim buffer FAQ' http://www.vim.org/tips/tip.php?tip_id=135 * VIM Tip#305 "Best of VIM Tips (VIM's best Features)" http://vim.sourceforge.net/tips/tip.php?tip_id=305 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ [Spell Checking] # Starting with vim 7.x there's a spell check module. # Enable spell checking :set spell # Get suggestion list (with cursor on highlighted misspelling) z= +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ [Consolidating vi notes into one file] # Id: vi.txt,v 1.2 2002/06/07 01:40:16 tjn Exp # Substitute an "F" for all digits on a line # notice the wonderful notation for character classes! :s/\[[0-9\]]/F/g # Remember the word "Remember" and insert "F"'s around it # notice the wonderful notation for memory parentheses! :s/\(Remember\)/ F \1 F/g +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Line Wrapping The 'wrapmargin' ('wm') is supposed to wrap N spaces from the right margin, except when 'textwidth' is set. I've been having some issues with the width selected for things that *must* be under say 75 chars wide. In that case I'm using the "textwidth=75" and it seems to work. You can see what they are all set to by typing 'set VAR', e.g. :set textwidth see also: (from within vim) :help wrapmargin see also: (from within vim) :help textwidth +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ [source code from others, etc.] Embedding Vim setting in the source file's comments Example to set the tabstop, expandtab, & shiftwidth, (notice the space between the first item "vim:") NOTE: Make sure 'modeline' is enabled for this to work! (see VIM help info, e.g., ":help modeline") LaTeX comment % vim:tabstop=4:shiftwidth=4:expandtab:textwidth=76:filetype=plaintex Perl comment # vim:tabstop=8:expandtab:shiftwidth=8 C comment /* vim:tabstop=8:expandtab:shiftwidth=8 */ +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ [Oualline] "VI Improved", Steve Oualline * Text Formatting [Oualline, p115] gq -- format paragraph gqMOTION Ex. gq4j (fmt cur & next 4 lines -- 5 ttl lines) gq} -- begin at top of paragraph (or only applies to end of para) [note the '}' is the forward paragraph cmd] gqip -- "inner paragraph" (format para, w/o having to be on first line) (from anywhere in paragraph) gqgq -- format a line gqq -- " (shorthand of 'gqgq') * Text Centering, etc. [Oualline, p115] (note, using line numbering (':set nu') may be helpful in this area) (additionally, the example line numbers may have shifted slightly.) :range center [width] ^^^^^-- defaults to 'textwidth' Example, :1,5 center 30 -- centers first - 5th line at 30 char width The Third LCI International Conference on Linux Clusters :37,40 center 30 The Third LCI International Conference on Linux Clusters :1,5 right 30 -- right justify first - 5th line at 30 chars :46,49 right 30 The Third LCI International Conference on Linux Clusters :range left margin Example, :1 left 5 -- left justify line 1, at margin=5 :57 left 5 The Third LCI International Conference on Linux Clusters Also, there's a macro package for Vim to justify text :source $VIMRUNTIME/macros/justify.vim With this macro pkg you can use '_j' to do global justifications. * Multiple Files [Oulline, p.42] To edit multiple files, without using CTRL-Z ;) $ vim one.c two.c three.c ^^^^^ opened first :next -- switch to next file ("two.c") You will be prompted for a Save, you can either '!' the command or use 'wnext' to write & move to the next. [Note, possibly use ':set autowrite' to avoid prompt,] [ unset this with ':set noautowrite'.] :previous -- switch to prev file ("one.c") You will be prompted for a Save, you can either '!' the command or use 'wprevious' to write & move to the prev. [Note, possibly use ':set autowrite' to avoid prompt,] [ unset this with ':set noautowrite'.] :2next -- jumps to 2 forward :rewind -- jumps to 1st file [logically should be "first" :^) ] :last -- jumps to Nth file CTRL-^ -- switch between two files 1CTRL-^ -- move to specified file 2CTRL-^ -- move to specified file +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ gg=G (jump to top of file, indent to BOTTOM) gg>G (indent entire file) :'a,'b> (indent one tabstop between marks) Tip #83: how to indent (useful for source code) see also: http://vim.sourceforge.net/tip_view.php?tip_id=83 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ All this information was extracted form the Vi tutorial at the following URL: http://www.linuxbox.com/~taylor/4ltrwrd/section3_4.html ----------------------------------------------------------------- Moving Around h move the cursor one character to the left j move the cursor one character down k move the cursor one character up l move the cursor one character to the right 0 move to the beginning of a line $ move to the end of a line G move to the end of a file 1G move to the first line of a file move down one screen move up one screen [from: http://www.linuxbox.com/~taylor/4ltrwrd/VBmove.html ] ----------------------------------------------------------------- Deleting Text x delete the character under the cursor dd delete a line [from: http://www.linuxbox.com/~taylor/4ltrwrd/VBdelete.html ] ----------------------------------------------------------------- Saving & Quitting :w write to disk ZZ write to disk and exit :q! exit without writing to disk [from: http://www.linuxbox.com/~taylor/4ltrwrd/VBsave.html ] ----------------------------------------------------------------- Editing dd deletes the current line of text and places it in a buffer yy copies the current line of text to a buffer (leaving the orig alone) "Marking" mq ........ ............ ............ d'q <== This deletes all between original mark "q" and putss it into buffer. You can also do the same with 'yank' so as to not alter orig y'q <== text as noted above. As shown here (more detailed description of this is quoted here) "marking the beginning of the text block with the name q, i.e., mq will give the current cursor location the name q. Then we move to the end of the portion of text we wish to move and type d'q. This command deletes everything from the marked position to the cursor position and places it in a buffer. Text in the buffer is retrieved using the "pull" command already described. The "yank" command allows you to copy the text to the buffer instead of deleting it. Typing y'q instead of d'q will place a copy of the text in the buffer and leave the original text unaltered." [from: http://www.linuxbox.com/~taylor/4ltrwrd/VBedit.html ] ----------------------------------------------------------------- Searching Use RegExp for searching Can also use many of the options that are familiar from say Perl experience. Example: Search for words with "an" that are not preceeded by a "c" /[^c]an Search/Replace Example: Search for "search_string" and replace it with "replacement_string" the "g" applies for muliple matches on same line (global). s/search_string/replacement_string/g Example, if I wanted to replace guy with gal on lines 32 through 56 I would type :32,56s/guy/gal/g [from: http://www.linuxbox.com/~taylor/4ltrwrd/VBsr.html ] ----------------------------------------------------------------- Display Commands ^G Give file name, status, current line number and relative position. ^L Refresh the screen (sometimes `^P' or `^R'). [from: http://www.linuxbox.com/~taylor/4ltrwrd/VRdisplay.html ] -----------------------------------------------------------------