I'm trying to get vim to execute the script I'm editing with a key mapping, say ctrl + x, so I used this in vimrc
:map <C-x> :!exec_file %<CR>
To pass the file name to a script I wrote which parses the extension and calls the appropriate interpreter, this works great so far.
However, I was wondering if I can call bash
directly with the script name and have it execute it since most scripts I edit have a shebang line, so I tried this mapping instead:
:map <C-x> :!bash %<CR>
But it doesn't work, is it possible to execute a script by passing its name to bash ?
Best Answer
Vim already uses a shell (see
:set shell?
) to execute the external command; scripts with shebang lines should work just fine. The canonical way to execute the current buffer is(Prepending
./
to deal with the current directory not being part ofPATH
. This assumes that the script is already executable (:!chmod +x %
, maybe done in a mapping / ftplugin.)You can re-execute with just
:!!
. Also, there are fancier solutions for executing (partial, unsaved, etc.) buffer contents in external interpreters; check the plugins section of vim.org.