bashvim

Execute current line in Bash from Vim


This question is similar to Vim: execute current file?, but instead of executing the current file I want to execute only the current line.

Is this possible?

Ideally, I am looking for solutions which can have side effects in the outer shell.

For example, suppose I have the following line:

alias foo=bar

After running the command in Vim, if I start a shell with :sh, the alias foo is available, but if I quit vim using :q, then the alias is no longer available.


Solution

  • Sure thing, you can 'write' any content of the current file into the standard input of another program:

    :.w !bash
    

    Here . (the part before w) refers to the range of lines you are writing, and . is only the current line. Then you use !bash to write those lines to Bash.