VIm - smart tips

Insert TAB instead spaces


Many times in VIm you have turn on tab extension - that means if you press TAB button into text will be inserted predefined space instead TAB. But sometimes you need to insert TAB char instead spaces.

You can use <CTRL-V><Tab> in insert mode. In insert mode <CTRL-V> inserts a literal copy of your next character. On windows you may need to press <CTRL-Q> rather than <CTRL-V>.

Multistring search

First you need to define two highlight groups to set up the colors you want. Let's say, one with green background and the other one with blue background :

:highlight MyGroup1 ctermbg=green guibg=green
:highlight MyGroup2 ctermbg=blue guibg=blue

Next you need to define your two matches with the matchadd() function. For example, you could apply the green background defined in the highlight group MyGroup1 to the pattern foo and the blue background defined in MyGroup2 to the pattern bar :

:call matchadd("MyGroup1", "foo")
:call matchadd("MyGroup2", "bar")

You can access all the information (highlight group, pattern, priority, id) about your matches with the getmatches() function:

:echo getmatches()

The id key is useful because its value allows you to delete a specific match with the matchdelete() function: :call matchdelete({id})

Besides, if you store the output of getmatches() inside a variable (let's say m) :

:let m = getmatches()

… then you can clear your matches with the clearmatches() function:

:call clearmatches()

… and restore them with the setmatches() function afterwards:

:call setmatches(m)

Finally, if you have to define those matches regularly, you can gain some time by storing the commands that define them in a separate file and source it when needed.

For example, you could store the following code in mymatches.vim:

highlight MyGroup1 ctermbg=green guibg=green
highlight MyGroup2 ctermbg=blue guibg=blue

call matchadd("MyGroup1", "foo")
call matchadd("MyGroup2", "bar")
And source it with : :so mymatches.vim

Note that if one of your pattern is a double quote , you should escape it “\”” or put single quotes around '”'.