Take the 2-minute tour ×

Say, I want a specific vim mapping to be applicable only if the file name (better still, path) contains specific characters --- irrespective of filetype.

Can this be done? How? Or am I asking for too much?

share|improve this question
it can be done, create script that checks your file name and contains a if else that changes the .vimrc based on the result from the file name check, then the script starts up vim with your file. last alias vim to your new script. –  Darkmage Nov 21 '12 at 14:42
add comment

1 Answer

Yes, you can do that with vim's autocmd command, by putting something like the following in your .vimrc file:

autocmd BufRead,BufNewFile /path/* map <buffer> <c-J> gqip

That will cause the map command to be executed whenever you start to edit a new (because of the BufNewFile event) or existing (because of the BufRead event) file that matches the pattern. Since the pattern contains a / it will be matched against the entire path of the file, if it didn't it would be matched against only the base name of the file. Using the <buffer> argument to the map command will apply the mapping only to the current buffer.

You can use :help autocmd to get additional information on defining autocmds, including the available events and details of the patterns that are supported.

share|improve this answer
add comment

Your Answer


