VIM settings (.vimrc) and VIM usage tips & tricks

Line wrapping and text formatting

We commonly use the VIM text editor for composing e-mail messages in Mutt.

To have lines auto-wrap at a reasonable length (to allow for quoting them a few times, yet fit an 80-character wide terminal window), put this in ~/.vimrc:

set tw=72

This setting can be overridden from within VIM, e.g. for copy-pasting some pre-formatted text, by typing:

:set tw=0

To re-format a paragraph in VIM, type gqap in command mode (see :help gqap), or concatenate the lines with multiple presses of Shift-J, then append a character by pressing Shift-A and typing. Note that the gqap command might not be available in small builds of VIM. With the VIM package on Owl, this command is available when VIM is invoked as vim, but not when it is invoked as vi.

Making small changes to large source trees

Quite often, we need to make a few small changes to a large source tree, e.g. of the Linux kernel, and then generate a patch. Rather than create a copy of the source tree, a “hard links tree” may be created with:

cp -al program program.orig

Then files in the program tree may be edited, but only as long as the editor uses a rename operation to replace the files. To make VIM do it, add:

set bkc=no

to ~/.vimrc of the account that you use for this task. Without this setting, VIM rewrites files in-place (by copying a “backup” file over the original) in order to preserve the inode number, which in this case is precisely what we don't need.

Another program that is known to work as desired on a “hard link tree” is patch, and you do not need any non-default setting for this one. This allows to apply an older revision of a patch, then use VIM (with the above setting) to make additional changes, and finally to generate an updated patch with diff.

This approach has several advantages: the “hard links tree” is created much quicker than a copy, it does not occupy much disk space, it is much quicker to create a diff between two “hard link trees” than between two copies (unchanged files don't even need to be read since it is immediately seen that their inodes match), and such a diff does not purge other data from memory cache. However, this approach should be used with great care as it is very easy to inadvertently edit a file in both trees and thus not have the changes appear in the resulting patch.

A similar effect can be achieved without creating a second tree at all, by using the gendiff script distributed along with RPM. The rpm package on Owl includes a remake of gendiff.

internal/vimrc.txt · Last modified: 2008/05/23 06:21 by solar
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate to DokuWiki Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki Powered by OpenVZ Powered by Openwall GNU/*/Linux