Peter's Vi Q&A

  1. How can I reformat my text document?
  2. How can I spellcheck my text document?
  3. How do I perform word completion when entering text?
  4. Can you give an example of visual block editing?
  5. How do I reduce a range of blank lines to a single blank line?
  6. How do I cut and paste between different vim sessions?
  7. Peter, what does your .vimrc file look like?
  8. What are text markers and how do I use them?
  9. How do I turn a setting on/off for any file ending in .c?

  1. How can I reformat my text document?
    Put
    map <F1> !}fmt<CR>
    in your .vimrc to use F1 to reformat a paragraph. You can specify command line arguments to fmt by:
    map <F1> !}fmt --option1 --option2<CR>
    Instead, if you want to reformat the entire document (good if you want to reformat mail from Windows users) try:
    map <F1> 1G!Gfmt --option1 --option2<CR>
    1G moves you to the top of the file !G says pipe from here to the end of the doc through the following command.



  2. How can I spellcheck my text document?
    Checking a single word

    For non-interactive spell checkers (like writing a script that greps /usr/dict/words), put this in your .vimrc:

    set keywordprg=/usr/local/interactivespellchecker
    after that, by pressing K, vim will hand the word under the cursor to the spell checker.

    using vim's highlighting to check a whole file

    Download engspchk.vim (http://www.ee.cua.edu/~cec/engspchk.vim.gz) if you don't already have it. Then in your .vimrc file, put:

    map <F3> :so /usr/share/vim/syntax/engspchk.vim<CR>
    make sure syntax highlighting is on by placing
    syntax on
    in your .vimrc file. Unrecognized words are highlighted in red. It's clear how to add words to this file if you ever need to. If you have Vim 5.4 or higher, you can also use \en and \ep for the next and preceeding error. Using an interactive spell checker (from the vim faq)

    Put this in your .vimrc:

    noremap ;ispell :%! ispell-filter<CR><C-L>
    where ispell-filter is:
    	#!/bin/sh
    	# This script runs ispell on stdin, returning the result through stdout.
    	# It can be used from VI like this (or map it to a key sequence):
    	# :%! ~/bin/ispell-filter
    	#
    	cat > /tmp/tmp.$$
    	ispell $* /tmp/tmp.$$ < /dev/tty > /dev/tty
    	cat /tmp/tmp.$$
    	rm -f /tmp/tmp.$$
    	
    Using an interactive spellchecker like ispell:

    Put the following in your .vimrc

    map #fi :w<CR>:!ispell %<CR<:e %<CR>
    Actually, I like this one the best because I can see exactly what it's doing.
    map #fimaps the string #fi in command mode (not : or text insert mode)
    :w<CR> writes the file.
    !ispell %<CR> runs ispell on your file. in ex, % expands to the file you're currently editing
    :e %<CR>ispell writes to your file, but you still have the old copy with spellign erors. So you want to reload the file.
    This will work for stock old, musty vi.



  3. How do I perform word completion when entering text?
    It's similar to bash's tab for filename completion, but it uses control-p and control-n instead.
    control-n will complete the current string using text from lines below the current position (next).
    control-p will complete the current string using text from above (previous).
    



  4. Can you give an example of visual block editing?
    Suppose you have a file with contents:
    de
    fi
    uk
    Germany
    Finland
    England
    and you'd like to put the country names after their 2 letter codes.

    1. Place the cursor over the d in de.
    2. Move the cursor over the G in "Germany".
    3. Press control-v for "block editing".
    4. Move the cursor to d in "England". The 3 country names should now be highlighted.
    5. Press y to yank them into memory. Move the cursor to the e in "de".
    6. Paste the block in. This should produce what we want, almost:
    deGermany
    fiFinland
    ukEngland
    1. Move the cursor to G in German. Press control-v again.
    2. Move the cursor to E in England. This should produce a 1 character vertical highlighted bar.
    3. Tap I and press the space bar. Press ESC and you'll see:
    de Germany
    fi Finland
    uk England



  5. How do I reduce a range of blank lines to a single blank line?
    In ex mode:
    v/./.,/./-1join



  6. How do I cut and paste between different vim sessions?
    Place this in your .vimrc file:
    " _Y: Yank the highlighted block of text (or a single line) to a tmp file.
    " _P: Put the text yanked with \_Y (possibly in another invocation of Vim).
    nmap _Y :.w! ~/.vi_tmp<CR>
    vmap _Y :w! ~/.vi_tmp<CR>
    nmap _P :r ~/.vi_tmp<CR>
    Now you just highlight the area you want to copy with V commands and yank it with _Y. Then you can paste it in another vim with _P.



  7. Peter, what does your .vimrc file look like?
    map #fi :w<CR>:!ispell %<:CR>:e %<CR>
    "map #fi :w^M:!ispell %^M:e %^M
    "set autoindent    " lines following an indented line will have the same
                       "  indentation. ^d in text mode backs over the indentation
    set beautify
    set nomesg         " don't bug me (set mesg)
    set modeline
    "set number        " puts line numbers on the LHS of the screen (set nonumber)
    set shiftwidth=3   " similar to tabstop, but tabstop takes precedence
    set showmode       " shows what mode we're in
    set tabstop=3      " sets the number of spaces to a tab
    set textwidth=76         " textwidth wins over wrapmargin
    set warn           " warns if you modified a file but haven't saved it yet.
                       "   doesn't seem to work
    set wrapmargin=6 
    set nobackup     
    set noerrorbells
    map! <F1>  \begin{equation}
    map! <F2> \end{equation}
    map! <F3> \langle
    map! <F4> \rangle
    
    au BufNewFile,BufRead *.html map! <F1> <TT>
    au BufNewFile,BufRead *.html map! <F2> </TT>
    au BufNewFile,BufRead *.html map! <F3> <BLOCKQUOTE>
    au BufNewFile,BufRead *.html map! <F4> </BLOCKQUOTE>
    au BufNewFile,BufRead *.f90,*.for,*.c,,*.html,*.pl,*.tex set tw=0 wm=0
    "syntax on
    map <F3> :so /usr/share/vim/syntax/engspchk.vim<CR>
    



  8. What are text markers and how do I use them?
    Vim has 52 text markers named a-z and A-Z. Surprisingly, they mark text.

    To mark a text location:

    1. Place the cursor over the location you want to mark.
    2. Type m
    3. Type the name of the marker you want to use (A-Z or a-z).
    Now you've marked a location in your document. If you want to work examples along with my explanation, go ahead and edit a document. Place 3 text markers within your document -- use a, B and c.

    To go back to the mark you just made:

    1. Go somewhere (anywhere) else in the document to make sure this works.
    2. To go to the beginning of the line with the text marker a, type 'a. That's a forward quote.
    3. To go to the exactlocation of the text marker B, type `B. That's a back quote.
    This is pretty useful when you're going back and forth between locations in your document, but we can do more with them. Here are some examples. In ex mode,
    `a,`Bs/emacs/vim/gBetween text markers a and B, change all occurances of emacs to vim.
    `a,`cd Delete all text between text markers a and c.
    'j,'kyy Yank text between the beginning of the line marked by j and the beginning of the line marked by k.
    "cy`bPlace the text between the current cursor position and marker b into the named buffer c.
    In general, a text region can often be defined by a cursor movement command. The forward and backward quote commands are cursor movement commands.



  9. How do I turn a setting on/off for any file ending in .c?
    Suppose you wanted to turn off wrap margins for any C source code or html file. Put the following in your .vimrc file:
    au BufNewFile,BufRead *.c *.html *.h *.cc set tw=0 wm=0