Sunday, September 14, 2008

Defining a user command: easy make invocation

This little thing:

command MM :exec ":make ".substitute( substitute(expand('%:p'), '\.[^\.]*$', '', ''), '^'.getcwd().'/', '', '')

defines a new VIM command ":MM" which does invoke ":make" with name of current file without extension. E.g. if you edit file "helloworld.c" and then type command ":MM" that would expand to command ":make helloworld"

Just in case I have also added stripping of $PWD (getcwd() in VIM universe) so that if you edit file from another directory, make would be called with relative path and Makefile from current directory would be used. That will not improve much make behavior, but if by chance you are using "cons", then that would work very well.

Edit1. Forgot to mention. VIM mandates that all user commands start with capital letter (all standard commands start with lower case letters). That's why I named the custom command "MM"