(Read more interesting articles on Hacking Vim 7.2 here.)
Work area personalization
In this article, we introduce a list of smaller, good-to-know modifications for the editor area in Vim. The idea with these recipes is that they all give you some sort of help or optimization when you use Vim for editing text or code.
Adding a more visual cursor
Sometimes, you have a lot of syntax coloring in the file you are editing. This can make the task of tracking the cursor really hard. If you could just mark the line the cursor is currently in, then it would be easier to track it.
Many have tried to fix this with Vim scripts, but the results have been near useless (mainly due to slowness, which prevented scrolling longer texts at an acceptable speed). Not until version 7 did Vim have a solution for this. But then it came up with not just one, but two possible solutions for cursor tracking.
The first one is the cursorline command , which basically marks the entire line with, for example, another background color without breaking the syntax coloring. To turn it on, use the following command:
The color it uses is the one defined in the CursorLine color group. You can change this to any color or styling you like, for example:
:highlight CursorLine guibg=lightblue ctermbg=lightgray
If you are working with a lot of aligned file content (such as tab-separated data), the next solution for cursor tracking comes in handy:
This command marks the current column (here the cursor is placed) by coloring the entire column through the entire file, for example.
As with the cursor line, you can change the settings for how the cursor column should be marked. The color group to change is named cursorcolumn.
Adding both the cursor line and column marking makes the cursor look like a crosshair, thus making it impossible to miss.
Even though the cursorline and cursorcolumn functionalities are implemented natively in Vim, it can still give quite a slowdown when scrolling through the file.
Adding line numbers
Often when compiling and debugging code, you will get error messages stating that the error is in some line. One could, of course, start counting lines from the top to find the line, but Vim has a solution to go directly to some line number. Just execute :XXX where XXX is the line number, and you will be taken to the XXX line.
Alternatively, you can go into normal mode (press the Esc key) and then simply use XXXgg or XXXG (again XXX is the line number). Sometimes, however, it is nice to have an indication of the line number right there in the editor, and that’s where the following command comes in handy:
Now, you get line numbers to the left of each line in the file. By default, the numbers take up four columns of space, three for numbers, and one for spacing. This meansthat the width of the numbers will be the same until you have more than 999 lines. If you get above this number of lines, an extra column will be added and the content will be moved to the right.
Of course, you can change the default number of columns used for the line numbers. This can be achieved by changing the following property:
Replace XXX with the number of columns that you want.
Even though it would be nice to make the number of columns higher in order to get more spacing between code and line numbers, this is not achievable with the numberwidth property. This is because the line numbers will be right-aligned within the columns.
In the following figure, you can see how line numbers are shown as right-aligne when a higher number of columns are set in numberwidth:
You can change the styling of the line numbers and the columns they are in by making changes to the LineNr color group.
Spell checking your language
We all know it! Even if we are really good spellers, it still happens from time to time that we misspell a word or hit the wrong keys. In the past, you had to run your texts (that you had written in Vim) through some sort of spell checker such as Aspell or Ispell . This was a tiresome process that could only be performed as a final task, unless you wanted to do it over and over again.
With version 7 of Vim, this troublesome way of spell checking is over. Now, Vim has got a built-in spell checker with support for more than 50 languages from around the world.
The new spell checker marks the wrongly written words as you type them in, so you know right away that there is an error.
The command to execute to turn on this helpful spell checker feature is:
This turns on the spell checker with the default language (English). If you don’t use English much and would prefer to use another language in the spell checker, then there is no problem changing this. Just add the code of the language you would like to use to the spelllang property . For example:
Here, the language is set to German (Deutsch) as the spell checker language of choice. The language name can be written in several different formats. American English, for example, can be written as:
Names can even be an industry-related name such as medical. If Vim does not recognize the language name, Vim will highlight it when you execute the property-setting command.
If you change the spelllang setting to a language not already installed, then Vim will ask you if it should try to automatically retrieve it from the Vim homepage.
Personally, I tend to work in several different languages in Vim, and I really don’t want to tell Vim all the time which language I am using right now.
Vim has a solution for this. By appending more language codes to the spelllang property (separated by commas), you can tell Vim to check the spelling in more than one language.
Vim will then take the languages from the start to the end, and check if the words match any word in one of these languages. If they do, then they are not marked as a spelling error. Of course, this means that you can have a word spelled wrong in the language you are using but spelled correctly in another language, thereby introducing a hidden spelling error.
You can find language packages for a lot of languages at the Vim FTP site: ftp://ftp.vim.org/pub/vim/runtime/spell.
Spelling errors are marked differently in Vim and Gvim.
In regular Vim, the misspelled word is marked with the SpellBad color group (normally, white on red).
In Gvim, the misspelled word is marked with a red curvy line underneath the word. This can, of course, be changed by changing the settings of the color group.
Whenever you encounter a misspelled word, you can ask Vim to suggest better ways to spell the word. This is simply done by placing the cursor over the word, going into the normal mode (press Esc), and then pressing Z + =.
If possible, Vim will give you a list of good guesses for the word you were actually trying to write. In front of each suggestion is a number. Press the number you find in front of the right spelling (of the word you wanted) or press Enter if the word is not there.
Often Vim gives you a long list of alternatives for your misspelled word, but unless you have spelled the word completely wrong, chances are that the correct word is within the top five of the alternatives. If this is the case, and you don’t want to look through the entire list of alternatives, then you can limit the output with the following command:
Set X to the number of alternative ways of spelling you want Vim to suggest.
Adding helpful tool tips
In the Modifying tabs recipe, we learned about how to use tool tips to store more information using less space in the tabs in Gvim. To build on top of that same idea with this recipe, we move on and use tool tips in other places in the editor.
The editing area is the largest part of Vim. So, why not try to add some extra information to the contents of this area by using tool tips?
In Vim, tool tips for the editing area are called balloons and they are only shown when the cursor is hovering over one of the characters. The commands you will need to know in order to use the balloons are:
- The first command is the one you will use to actually turn on this functionality in Vim.
- The second command tells Vim how long it should wait before showing the tool tip/balloon (the delay is in milliseconds and as a default is set to 600).
- The last command is the one that actually sets the string that Vim will show in the balloon.
This can either be a static text string or the return of some function.
In order to have access to information about the place where you are hovering over a character in the editor, Vim provides access to a list of variables holding such information:
||Will not expand as the abbreviation is part of a word|
||Will not expand because there are letters after the abbreviation|
|abc||Will not expand until another special letter is pressed|
So with these variables in hand, let’s look at some examples.
The first example is based on one from the Vim help system. It shows how to make a simple function that will show the information from all the available variables.
function! SimpleBalloon() return 'Cursor is at line/column: ' . v:beval_lnum . '/' . v:beval_col . ' in file ' . bufname(v:beval_bufnr) . '. Word under cursor is: "' . v:beval_text . '"' endfunction set balloonexpr=SimpleBalloon() set balloonevalcode 59
The result will look similar to the following screenshot:
Let’s look at a more advanced example that explores the use of balloons for specific areas in editing. In this example, we will put together a function that gives us great information balloons for two areas at the same time:
- Misspelled words: The balloon gives ideas for alternative words
- Folded text: The balloon gives a preview of what’s in the fold
So, let’s take a look at what the function should look for, to detect if the cursor is hovering over either a misspelled word or a fold line (a single line representing multiple lines folded behind it).
In order to detect if a word is misspelled, the spell check would need to be turned on:
If it is on, then calling the built-in spell checker function—spellsuggest()—would return alternative words if the hovered word was misspelled. So, to see if a word is misspelled, just check if the spellsuggest() returns anything. There is, however, a small catch. spellsuggest() also returns alternative, similar words if the word is not misspelled. To get around this, another function has to be used on the input word before putting it into the spellsuggest() function . This extra function is the spellbadword(). This basically moves the cursor to the first misspelled word in the sentence that it gets as input, and then returns the word. We just input a single word and if it is not misspelled, then the function cannot return any words. Putting no word into spellsuggest() results in getting nothing back, so we can now check if a word is misspelled or not.
It is even simpler to check if a word is in a line, in a fold. You simply have to call the foldclosed()function on the line number of the line over which the cursor is hovering (remember v:beval_lnum ?), and it will return the number of the first line in the current fold; if not in a fold, then it returns -1. In other words, if foldclosed(v:beval_lnum) returns anything but -1 and 0, we are in a fold.
Putting all of this detection together and adding functionality to construct the balloon text ends up as the following function:
function! FoldSpellBalloon() let foldStart = foldclosed(v:beval_lnum ) let foldEnd = foldclosedend(v:beval_lnum) let lines =  " Detect if we are in a fold if foldStart 31 ) let lines = getline( foldStart, foldStart + 14 ) let lines += [ '-- Snipped ' . ( numLines - 30 ) . ' lines --' ] let lines += getline( foldEnd - 14, foldEnd ) else "less than 30 lines, lets show all of them let lines = getline( foldStart, foldEnd ) endif endif " return result return join( lines, has( "balloon_multiline" ) ? "n" : " " ) endfunction set balloonexpr=FoldSpellBalloon() set ballooneval
The result is some really helpful balloons in the editing area of Vim that can improve your work cycle tremendously. The following screenshot shows how the information balloon could look when it is used to preview a folded range of lines from a file:
Instead, if the balloon is used on a misspelled word, it will look like the following screenshot:
In Production Boosters, you can learn more about how to use folding of lines to boost productivity in Vim.