Vim Tips Wiki:Featured Tip/2008

Contents

When in an editing session, another process (perhaps from a shell command) may modify or delete a file in one of Vim's buffers. The FileChangedShell autocommand event can be used to control Vim's response.

A search like /\%>80v.\+ with search highlighting (:set hlsearch) will highlight any text after column 80.

A simpler pattern such as \%81v.* fails to highlight text past the limit if there is no character in virtual column 81, and can give an erroneous highlight of column 81 on lines of exactly 80 characters.

Programmers, and other careful editors, often work hard to maintain consistent use of whitespace characters (space and tab). Perhaps you want to avoid using spaces before tabs or at the end of a line. Or, you may want to see where tabs occur, or see only the tabs that are not used for indenting.

Good news! There are several ways to highlight unwanted whitespace. Try a simple search, or match or syntax highlighting. There's also the 'list' option, and your filetype may support showing syntax space errors.

It's easy to work with CSV files (comma-separated values), if you have the right tools! You can highlight all text in any column, navigate from cell to cell (up/down and left/right), search for text in a specific column, and more.

All you need is Vim and this month's hot tip! Be sure to check it out if you need to sort csv data by column, or if your brain needs some regex exercise. Marvel at the magic of matchadd() (Vim 7.1.040 and later), with fallback to :2match for earlier versions.

Searching is simple: starting in normal mode, press / then type what you want to find, and press Enter. But in Vim there's always more to learn!

This month's featured tip improves your searching skills, starting with use of * to search for the current word, and continuing with more suggestions for successful searching. Remember to check the "see also" for more search tips.

Folding is frequently used to provide an easy overview of a program or document. A folded section is displayed as a single line that can easily be expanded. When supported by a syntax file, it is usually best to use setlocal foldmethod=syntax.

This month's featured tip explains how to enhance syntax folding of Vim scripts by using syn region commands in an after/syntax/vim.vim file. The tip provides valuable information for advanced users who may want to add to, or override, syntax rules for any file type.

Many text editors launch new files in tabs, rather than showing a separate window for each file. Here's how to make Vim behave that way under Microsoft Windows.

At command prompt, use ftype to define the open command to run gvim with the --remote-tab-silent option. Use assoc to define which file extensions will be associated with the ftype open command. You can specify the --servername option so files with one type open in one instance of Vim, while files of another type open in another. See tip 1440 Launch files in new tabs under Windows for details.

We know how to use /pattern to search the current file for the regular expression pattern.

You can also use a command like :vimgrep pattern *.txt to search all .txt files in the current directory for pattern, or :vimgrep pattern **/*.txt to search in the current directory and all its subdirectories. Using forward slashes for the file path works on Unix-based and Windows systems.

Visual selection is useful for operating on a selected area: v for characters, V for lines, Ctrl-V (Ctrl-Q on Windows) for a block. You can extend the selection with normal commands such as j to move down, or w to move by a word, or / to search. Marks are automatically defined that specify the start and end of the visual selection.

It's easy to search and replace within a visual selection – just type the normal :s/old/new/g command while in visual mode.