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 1:

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.

Solution 2:

I do this sort of thing all the time with:

:exec '!'.getline('.')

You can even create a mapping in your .vimrc:

nmap <F6> :exec '!'.getline('.')

Solution 3:

Move the cursor to that line, and in normal mode press:

!!bash<cr>