VIM Tips


Contents:

Editing C programs

There are quite a few features in Vim to help you edit C program files. Here is an overview with tags to jump to:

Editing local HTML files (WWW)

Vim has some features which can help simplify the creation and maintenance of HTML files, provided that the files you are editing are available on the local file system. The "]f", "gf" and "CTRL-W f" commands can be used to jump to the file whose name appears under the cursor, thus not only checking that the link is valid (at least the file name part of the URL) or providing a quick and easy way to edit many related HTML pages at once.

Editing paragraphs without a line break

If you are typing text in Vim that will later go to a word processor, it is useful to keep a paragraph as a single line. To make this more easy use:

  • set 'wrap' on, to make lines wrap
  • set 'linebreak' on, to make wrapping happen at a blank
  • use "gk" and "gj" to move one screen line up or down

Switching screens in an xterm

(From comp.editors, by Juergen Weigert, in reply to a question)

>> Another question is that after exiting vim, the
>> screen is left as it was, i.e. the contents of
>> the file I was viewing (editting) was left on
>> the screen. The output from my previous like "ls"
>> were lost, ie. no longer in the scrolling buffer.
>> I know that there is a way to restore the screen
>> after exiting vim or other vi like editors, I
>> just don't know how. Helps are appreciated. Thanks.

>I imagine someone else can answer this. I assume though
>that vim and vi do the same thing as each other for a
>given xterm setup.

They not necessarily do the same thing, as this may be a termcap vs. terminfo problem. You should be aware that there are two databases for describing attributes of a particular type of terminal: termcap and terminfo. This can cause differences when the entries differ *and* when of the programs in question one uses terminfo and the other uses termcap.

In your particular problem, you are looking for the control sequences ^[[?47h and ^[[?47l. These switch between xterms alternate and main screen buffer. As a quick workaround a command sequence like

echo -n "^[[?47h"; vim ... ; echo -n "^[[?47l"
may do what you want. (My notation ^[ means the ESC character, further down you'll that the databases use \E instead).

On startup, vim echoes the value of the termcap variable ti (terminfo: smcup) to the terminal. When exiting, it echoes te (terminfo: rmcup). Thus these two variables are the correct place where the above mentioned control sequences should go.

Compare your xterm termcap entry (found in /etc/termcap) with your xterm terminfo entry (retrieved with /usr/5bin/infocmp -C xterm). Both should contain entries similar to:

:te=\E[2J\E[?47l\E8:ti=\E7\E[?47h:

PS: If you find any difference, someone (your sysadmin?) should better check the complete termcap and terminfo database for consistency.

Scrolling in Insert mode

If you are in insert mode and you want to see something that is just off the screen, you can use CTRL-X_CTRL-E and CTRL-X CTRL-Y to scroll the screen.

To make this more easy you could use these mappings:

:inoremap ^E ^X^E
:inoremap ^Y ^X^Y

(Use CTRL-V CTRL-E to enter ^E, CTRL-V CTRL-X to enter ^X, etc.)

Also consider setting 'scrolloff' to a larger value, so you can always see some context around the cursor. If 'scrolloff' is bigger than half the window height, the cursor will always be in the middle, the text is scrolled when the cursor is moved up/down.

Correcting common typing mistakes

When there are a few words that you keep on typing in the wrong way, make a few abbreviations that correct them:

:ab teh the
:ab fro for

Renaming files

Say I have a directory with the following files in them (directory picked at random :-):

addcr.c
alloc.c
amiga.c
...

and I want to rename *.c *.bla. I'd do it like this:

$ vim
:r! ls *.c
:%s/\(.*\).c/mv & \1.bla
:w !sh
:q!

Speeding up external commands

On some situations execution of an external command can be very slow. This can also slowdown wildcard expansion on Unix. Here are a few suggestions to increase the speed.

If your .cshrc (or other file, depending on the shell used) is very long, you should separate it into a section for interactive use and a section for non-intercative use (often called secondary shells). When you execute a command from Vim like ":!ls" you do not need the interactive things, for example setting the prompt. Put the stuff that is not needed after these lines:

	if ($?prompt == 0) then
		exit 0
	endif
Another way is to include the "-f" flag in the 'shell' option, e.g.:

:set shell=csh\ -f

(the backspace is needed to include the space in the option). This will make csh completely skip the use of the .cshrc file.

Useful mappings

Here are a few mappings that some people like to use.

:map ' `
Make the single quote work like a backtick. Puts the cursor on the column, instead of going to the first non-blank character in the line.

For emacs-style editing on the command line:
:cnoremap ^A start of line
:cnoremap ^B back one character
:cnoremap ^D delete character under cursor
:cnoremap ^E end of line
:cnoremap ^F forward one character
:cnoremap ^N recall newer command line
:cnoremap ^P recall previous (older) command line
:cnoremap ^B back one word
:cnoremap ^F forward one word

Compressing the help files

For those that are really short on disk space, you can compress the help files and still be able to view them with Vim.

(1) Compress all the help files: "gzip doc/*.txt".

(2) Edit "doc/vim_tags" and do ":%s=\\.txt\<Tab\>/=.txt.gz\<Tab\>/= (backslash notation: \\ is a single backslash, \<Tab\> is a Tab)

(3) Add these lines to your .vimrc:

set helpfile=<dirname>/vim_help.txt.gz
autocmd BufReadPre *.txt.gz set bin
autocmd BufReadPost *.txt.gz '[,']!gunzip
autocmd BufReadPost *.txt.gz set nobin
set cmdheight=2

Where <dirname> is the directory where the help files are. If you already have included autocommands to edit ".gz" files you should omit the three "autocmd" lines. Setting the command height to two is to avoid having to hit return, it is not mandatory.


Send feedback on this page to Rajesh Kallingal
For Vim version 3.24. Last modification: 1996 Apr 25