问题
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?
Update: 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.
回答1:
Sure thing, you can 'write' any content of the current file into the stdin 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.
回答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('.')
Daan's answer also works, I gave him an upvote.
回答3:
move cursor to that line, and in normal mode press:
!!bash<cr>
回答4:
This could be a comment if I can comment.
Concerning redirect/pipe lines of current buffer in vim to external command, inspire by Daan Bakker's great answer, I wrote I answer here (https://stackoverflow.com/a/40002312/3625404), on an question concerning run python script (current buffer).
Beside run whole buffer, how to run a range of line via 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 whole buffer to python
:%w !python
suppose your current buffer contain 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, eg, some keymaps.
nnoremap <F8> :.w !python<CR>
vnoremap <F8> :w !python<CR>
first one run current line, second one run visual selection, via python interpreter.
#!! be careful, in vim ':w!python' and ':.w !python' are very different, the
first write (create or overwrite) a file named 'python' with contents of
current buffer, the second redirect the selected cmdline range (here dot .,
which mean current line) to external command (here 'python').
for cmdline range, see
:h cmdline-ranges
not below one, which concerning normal command, not cmdline one.
:h command-range
inspired by https://stackoverflow.com/a/19883963/3625404
回答5:
Add this mapping to your .vimrc file,
nmap <leader>E yyp:.!csh<CR>
Explanation:
yyYank current linepPaste yanked line below (and the cursor goes to this next row):.!csh<CR>Execute (usingcsh) 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.
回答6:
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.
回答7:
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
来源:https://stackoverflow.com/questions/19883917/execute-current-line-in-bash-from-vim