Vim Tips Wiki

This is an archive of the Featured tip section on the Main Page for 2009.

December 2009[]

One of Vim's great features is the ability to press J to join lines. But joining lines of comments in a program is still a hassle because you need to manually remove comment leaders.

Our featured tip shows how to map J to join comment lines. More importantly for Vimmers interested in learning scripting techniques, the tip has many explanations showing how the result is achieved while handling normal mode and visual mode.

November 2009[]

Programmers often need to convert numbers from hexadecimal to decimal, or vice versa. You can simply enter a command like :echo 0x1234 to display the decimal equivalent of hex 1234, or enter :echo printf('%x',1234) to display the hex equivalent of decimal 1234.

For more serious converting, our user-defined command :Dec2hex converts all decimal numbers in a selected area to hex, and :Hex2dec does the reverse.

Our featured tip has the details, as well as links to tips showing how to display or edit binary files in hex. A method to convert the current line to or from percent-encoded hex is also provided.

October 2009[]

Perhaps you are editing several files in different directories, and now you want to open another file in the same directory as your current file. Problem: the file is deep in the directory tree, and navigating to find the file would be frustrating.

Try our command line abbreviation to invoke expand('%:p:h') so you can easily insert the full path of the directory of the current file into the command line. Then you can press Enter to browse the directory, or use Tab for file name completion.

Our featured tip has the details, as well as an alternative to set the working directory to the current file.

September 2009[]

Have you ever used Caps Lock in insert mode, then switched back to normal mode to issue a few commands, completely forgetting that Caps Lock is still on? If entering <Esc>JJJJUUU nonsense commands sounds familiar, you need to try our featured tip.

By implementing language maps (or by using a keymap), you can take advantage of Vim's ability to translate characters after pressing Ctrl-^ while in insert mode. The "Caps Lock" provided by this technique applies only to entered text, not to normal-mode commands.

August 2009[]

A mark (aka bookmark) can be used to record your current position, so you can return to it later. You probably know that ma sets mark a, and that later you can type `a to jump to the a location. But what about file marks, and what about special marks?

There are lots of questions to ask about marks, and our featured tip has all the answers.

July 2009[]

Syntax highlighting helps show the structure of a program, and usually all you need is :syntax on – the rest is automatic.

However, what if you have some C code in a text file, or some SQL code in a C file? With a little work, you can apply different syntax highlighting rules to different regions of a file, so your C code looks like C, your text looks like text, and your SQL code looks like SQL.

Our featured tip has the details. Use it to create syntax regions in plaintext, tweak existing syntax rules, or even as a guide when developing a new syntax plugin!

June 2009[]

After another editing frenzy, you might wonder exactly where you made changes. Of course (if you have an old copy of the file) you can use vimdiff to see exactly what changes have occurred. Even with no original file, you can see where each change occurred with the :changes command.

Even more useful is the fact that you can return to locations where changes occurred, even those from previous editing sessions.

May 2009[]

It's time to go back to basics and check how line ranges work. You may know that :s/old/new/g substitutes in the current line, and :%s/old/new/g substitutes in the whole file, but it's worth learning more!

Ranges like .+1,$ (all lines after current line) and '<,'> (last visually selected lines) are very useful.

April 2009[]

What can you do with all those search hits? You can simply list them with :g/pattern/, or you can make a clickable list with :vimgrep /pattern/ %.

Our featured tip shows these, and has some techniques to copy the search results to the clipboard.

March 2009[]

You can search by typing / followed by the search pattern, or by pressing * to search for the current word.

With our featured tip, you can also select some text, then press * to search for the next occurrence of the text. The selection can be a few characters, or a few lines. Whitespace in the selection matches any whitespace when searching (searches will find words separated by any combination of spaces, tabs and line breaks).

February 2009[]

Do you ever do some edits in one place, knowing that you're going to have to repeat those edits in several other places? Naturally Vim allows complex repeats by recording keys into a register. :help complex-repeat

Now you can try the magic of a recursive repeat to automatically apply the commands you have recorded to many lines at a time. By following our technique for recording to a register, and finishing by invoking the register, you can apply a macro to every line in a file with no counts necessary!

January 2009[]

Sometimes it's useful to highlight several patterns in different colors. For example, you may be studying how three variables are used, and you would like to see each displayed with a different background color.

It's easy to highlight multiple words using our amazing highlighting script. You can press a key to highlight visually-selected text, or the current word, or you can enter a command to highlight any pattern. Searching for the next highlight is also easy.

You can have different sets of highlighting in different windows, or can copy highlighting from one window to another. If you really like your current highlight patterns, you can save them for use in the future.