Tuesday, July 18, 2006

vim :: tags keyboard shortcuts

Keyboard shortcuts used to work with tags. There are really few of them. All of them work in control/normal mode.

  • :set tags? needless to remind that VIM must be told which tag files to use. It can be done automatically as for example shown here.
  • :tag word is main one. Opens file and goes to line with first tag entry matching word.
  • ^] same as above, but uses word under cursor.
  • :tag /word is similar to :tag word. Opens file and goes to line with first tag entry matching regular expression in word.
  • :tselect word is what I use most often. The function - tag select - displays list of matching tags allowing you to select one to go to. Without word, it will display list of matching tags for word used last time.
  • g^] same as above, but uses word under cursor. Also, omits displaying list if only single match is found.
  • :tnext goes to next matching tag entry.
  • :tprev goes to previous matching tag entry.
  • :tlast goes to last matching tag entry.
  • :tfirst goes to first matching tag entry.
  • ^T Every time you do :tag word, the word with current file position is put on stack and vim goes to new file/position as it is told by tag file. To restore the previous file position you use ^T. The shortcut removes tag stack top element and moves to the file position saved there. So if you wandered off far away with ^] - to return back where you started you need to press ^T until vim would complain that it is at bottom of tag stack.
That's about it. Both :tag word and :tselect word support wildcard key (Tab/^D) to show list of tags matching partially the word. That will work with regilar expressions too. But be warned that often tag files can host millions of entries: displaying list of thousands items can take some time. Use wildcard key carefully.

Friday, July 14, 2006

syntax highlight for non-standard file types

Following line once put into vimrc will tell vim which syntax highlighting to use:

au BufReadPost SCons* set syntax=python
au BufReadPost Cons* set syntax=perl
au BufReadPost *.mke set syntax=make
au BufReadPost make*.inc set syntax=make
au BufReadPost *.fcc set syntax=cpp
au BufReadPost *.fhh set syntax=cpp


This is selfexplanatory. e.g. SCons files are in fact python scripts - so tell vim to use python syntax highlighting on everything what matches 'SCons*'. Same goes for Construct - but done in perl.

:au BufReadPost <mask> tells vim to execute something right after file read into buffer. In that particular case we use that to set buffer local variable 'syntax' to name of one of standard syntax highlightings (perl, make, cpp, etc). :help :au for more info.

P.S. One can check what syntax module vim has loaded by looking at 'syntax' variable value: do :set syntax? after opening file recognized by vim.

P.P.S. Of course, :syntax on has to be put somewhere in the vimrc for syntax highlighting to work automatically. VIM 6.x doesn't have problem of previous versions (I experienced that in VIM 5.x), where loading of syntax from vimrc was slowing launch time, even if file had no syntax highlighting assigned. Hm.. or probably my PC now fast enough for me not to notice that launch time. Who cares. It works.