4

As far as I know, all "good" scripts/programs start off with a shebang line as the first line:

#!/bin/bash
#!/usr/bin/env python
#!/usr/bin/perl

etc. Is it possible to pass that line to Vim in command-mode to generically determine the program to use when executing the current file.

For example, perl file needs

:! perl%

Python,

:! python%

If the shebang is already there in the file, is it possible to replace the specific program before the % character (current file) with the shebang line/argument?

Of course, if the file is executable already :! ./% works


The idea is to map a key so that, as you write code, simply hitting a shortcut key the file is executed; mimicking an IDE.

Once again, assuming the file is not (yet) executable, since you just started writing it in Vim.

2
  • So far, this Q/A fits into this category well.
    – ILMostro_7
    Commented May 14, 2016 at 3:42
  • I suppose the easiest thing to do here is to give execute permissions to the file; e.g. from within vim itself: :! chmod +x %. Then, the rest is taken care of with the available shebang line already in place.
    – ILMostro_7
    Commented Dec 31, 2018 at 1:23

3 Answers 3

3

I'm not sure this is what you want but you could try this mapping using the <F7> key:

nnoremap <F7> :<C-U>sil! exe '!' . matchstr(getline(1), '#!\zs.*') . ' ' . shellescape(expand('%:p'), 1) <Bar> redraw!<CR>
  • matchstr(getline(1), '#!\zs.*') extract the text after the shebang
  • shellescape(expand('%:p'), 1) expand the full path to the current file and protect characters that may have a special meaning for the shell; the second non-nul argument is useful to escape special items such as !, %, # which could be expanded by Vim on the command-line

If you want to see the output in the shell, you could remove :sil! and :redraw!:

nnoremap <F7> :<C-U>exe '!' matchstr(getline(1), '^#!\zs.*') shellescape(expand('%:p'), 1)<CR>
5

You could use the fact that your shell understands the Shebang:

:!./%

Please be sure to make your file executable (chmod +x filename). You can easily create a mapping for this, so that you can execute your current file with one keypress.

1
  • Yea, I should've clarified that part of the question; if the file is executable, doing :! % works
    – ILMostro_7
    Commented Nov 29, 2015 at 20:45
2

If for some reason @cbaumhardt's answer doesn't work for you, you can actually just use perl. As a handy helper for systems that don't handle shebang lines correctly, perl will actually honor the shebang line itself. See this question on SO for more info about that.

4
  • That's an awesome tidbit of information! Unfortunately, it does not apply here. The idea is to map a key so that, as you write code, simply hitting a shortcut key the file is executed.
    – ILMostro_7
    Commented May 14, 2016 at 3:27
  • Once again, assuming the file is not (yet) executable, since you just started writing it in Vim.
    – ILMostro_7
    Commented May 14, 2016 at 3:28
  • 1
    @ILMostro_7 so this clearly doesn't have the keybinding, but running :! perl % doesn't require the file to be executable, but it does honor the shebang line without having to parse it yourself, though user9433424's solution looks like just what you want Commented May 16, 2016 at 13:58
  • Perl is looking more and more interesting and powerful the older it gets.
    – ILMostro_7
    Commented May 19, 2016 at 5:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.