Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to perform File Extension based Actions in VIM?

Tags:

vim

I want to perform rubyf action in VIM when I press F5 if the file is of .rb extension. Right now I just have

map <F5> :rubyf % <CR>.

But I also want to interpret scheme files if its .scm or compile tex etc using the same F5. How do I check the file extension and perform the correct binding? How do you guys compile different files in G/VIM?

like image 418
unj2 Avatar asked May 24 '09 04:05

unj2


1 Answers

You could create a different autocmd for each file extension. eg:

au BufEnter,BufNew *.rb map <F5> :rubyf % <CR>.

See :help autocmd for info about autocmds.

A better approach for your specific problem would be to map <F5> to always invoke :make % and then have a autocmd that set the makeprg option for each file type (use setlocal when you do this for best results). This wouldn't be for loading ruby into Vim (as you seem to be doing) but instead for invoking an external compiler/interpreter/linter. This is essentially what I do. The nice thing about doing it this way is that Vim can interpret the errors and warnings and automatically jump to the problems in your code. You can also bring up a list of the errors/warnings. See :help quickfix for info about this, as well as the help topics for 'makeprg', :make, :copen and 'errorformat'.

A slight variation on this would be to not use autocmds at all, but instead to have an external script that when given a source filename figures out what to run (ruby, your scheme compiler, pychecker, your C compiler, whatever). Then just set makeprg to always run that script.

like image 200
Laurence Gonsalves Avatar answered Jan 04 '23 10:01

Laurence Gonsalves