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.
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.
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.
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.
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.
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!
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
Even more useful is the fact that you can return to locations where changes occurred, even those from previous editing sessions.
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!
.+1,$ (all lines after current line) and
'<,'> (last visually selected lines) are very useful.
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.
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).
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!
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.