This is an archive of the Featured tip section on the Main Page for 2008 (started in February 2008).
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.
It's easy for a script to mark the buffer of a deleted file as modified so you don't accidentally abandon a buffer that could be used to restore a deleted file.
FileChangedShell is used, the script has to handle each reason that the event may be invoked (file deleted, or contents changed, or other reason).
Programmers often need to know when source lines exceed a certain length. For example, your team's coding standards may require that lines never exceed 80 columns.
It's easy to search, match, or use syntax highlighting to highlight long lines.
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.
Vim's command and search history are extremely helpful. Perhaps you use history, but are frustrated from editing the command line with the primitive arrow, backspace and delete keys.
Now you can smile while editing history with all Vim's power in the command-line window. Read how to easily copy some previously-entered commands so you can apply them while editing another file.
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
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 Vim users prefer working with commands rather than complex key sequences, because commands can be remembered. A frequently-used command can be mapped to any key you like.
You can create user-defined commands, but each must start with an uppercase letter. So how do you replace a built-in command with a user-defined command? Here is a clever but simple technique to replace built-in commands, while avoiding accidental
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.
Would you like to press a key to search for the current word in all files in and under the current directory? See tip 1543 Find in files within Vim for details.
Visual selection is useful for operating on a selected area:
v for characters,
V for lines,
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.
You can also search the area that was visually selected – press Esc to exit visual mode, and start the search with
/\%V rather than
/. See tip 438 Search and replace in a visual selection for details.