Execute current line in Bash from Vim

BashVim

Bash Problem Overview


This question is similar to https://stackoverflow.com/questions/15449591/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.

Bash Solutions


Solution 1 - Bash

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 - Bash

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 - Bash

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

!!bash<cr>

Solution 4 - Bash

This could be a comment if I can comment.

Concerning redirect/pipe lines of current buffer in Vim to external command, inspired by Daan Bakker's great answer, I wrote I answer here (https://stackoverflow.com/questions/601039/vim-save-and-run-at-the-same-time/40002312#40002312), on an question concerning running a Python script (current buffer).

Beside running the whole buffer, how to run a range of line via an external command is demonstrated.

To save time, I just copy it below.

#####################

In Vim, you could simply redirect any range of your current buffer to an external command (be it 'bash', 'python', or you own Python script).

# Redirect the whole buffer to 'python'
:%w !python

Suppose your current buffer contains the two lines as below,

import numpy as np
print np.arange(12).reshape(3,4)

then :%w !python will run it, be it saved or not. And print something like below on your terminal,

[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]

Of course, you could make something persistent, for example, some keymaps.

nnoremap <F8> :.w !python<CR>
vnoremap <F8> :w !python<CR>

The first one runs the current line. The second one runs the visual selection, via the Python interpreter.

#!! Be careful, in Vim ':w!python' and ':.w !python' are very different, the
first writes (creates or overwrites) a file named 'python' with thew contents of
the current buffer. The second redirects the selected cmdline range (here dot .,
which mean current line) to an external command (here 'python').

For cmdline range, see

:h cmdline-ranges

Not the below one, which concerning normal command, not cmdline one.

:h command-range

This was inspired by https://stackoverflow.com/questions/19883917/execute-current-line-in-bash-from-vim/19883963#19883963.

Solution 5 - Bash

Add this mapping to your .vimrc file,

nmap <leader>E yyp:.!csh<CR>

Explanation:

  1. yy Yank current line
  2. p Paste yanked line below (and the cursor goes to this next row)
  3. :.!csh<CR> Execute (using csh) this newly pasted line in place. The output of this line replaces this current line, thus before executing the line was yanked and pasted below.

Solution 6 - Bash

If I have a command on a line of text within vi/Vim like this"

ls -la

Position the cursor anywhere on the line and do ":.!!" and press return.

That is: colon dot bang bang return

That will execute the text in that line in the current shell from within vi/Vim and have the output inserted within the text file.

I'm thinking that is what you were asking? It's like magic.

Solution 7 - Bash

Consider the following command run on terminal,

seq 0 10 | xargs printf "%02x "

Expected output is,

00 01 02 03 04 05 06 07 08 09 0a

Consider you have this above command written in a file. To execute this command get this respective output back in that file, you can add this mapping in yours .vimrc,

nmap <leader>E :exec 'r!'.getline('.')<CR>

Note that, to execute above mentioned line, you need to write it with adding escape char for '%' as follows,

seq 0 10 | xargs printf "\%02x "

Go this line in your file and press <leader>E. In my case, <leader> is mapped to , hence I will press ,E

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionmerlin2011View Question on Stackoverflow
Solution 1 - BashDaan BakkerView Answer on Stackoverflow
Solution 2 - BashcforbishView Answer on Stackoverflow
Solution 3 - BashKentView Answer on Stackoverflow
Solution 4 - BashqeatzyView Answer on Stackoverflow
Solution 5 - BashRohan GhigeView Answer on Stackoverflow
Solution 6 - BashBruxView Answer on Stackoverflow
Solution 7 - BashRohan GhigeView Answer on Stackoverflow