merlin2011
merlin2011

Reputation: 75639

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.

Upvotes: 86

Views: 34546

Answers (7)

Brux
Brux

Reputation: 31

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.

Upvotes: 1

qeatzy
qeatzy

Reputation: 1561

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 (Save and run at the same time in Vim), 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 Execute current line in Bash from Vim.

Upvotes: 10

cforbish
cforbish

Reputation: 8829

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('.')

Upvotes: 47

Kent
Kent

Reputation: 195269

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

!!bash<cr>

Upvotes: 24

Daan Bakker
Daan Bakker

Reputation: 6352

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.

Upvotes: 154

Rohan Ghige
Rohan Ghige

Reputation: 535

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.

Upvotes: 5

Rohan Ghige
Rohan Ghige

Reputation: 535

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

Upvotes: 0

Related Questions