bash 从vim执行bash中的当前行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19883917/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Execute current line in bash from vim
提问by merlin2011
This question is similar to Vim: execute current file?but instead of executing the current file I want to execute only the current line.
这个问题类似于Vim: execute current file? 但我只想执行当前行而不是执行当前文件。
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.
在 vim 中运行命令后,如果我使用 启动 shell :sh
,则别名foo
可用,但如果我退出vim
使用:q
,则别名不再可用。
回答by Daan Bakker
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.
这里.
(w之前的部分)指的是你正在写的行的范围,并且.
只是当前行。然后你!bash
用来将这些行写入 bash。
回答by cforbish
I do this sort of thing all the time with:
我一直在做这样的事情:
:exec '!'.getline('.')
You can even create a mapping in your .vimrc:
你甚至可以在你的 .vimrc 中创建一个映射:
nmap <F6> :exec '!'.getline('.')
Daan's answer also works, I gave him an upvote.
Daan 的回答也有效,我给了他一个赞。
回答by Kent
move cursor to that line, and in normal mode press:
将光标移动到该行,并在正常模式下按:
!!bash<cr>
回答by qeatzy
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).
关于将 vim 中当前缓冲区的重定向/管道线重定向到外部命令,受到 Daan Bakker 的精彩回答的启发,我在这里写了我的回答(https://stackoverflow.com/a/40002312/3625404),关于运行 python 脚本的问题(当前缓冲区)。
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).
在 vim 中,您可以简单地将当前缓冲区的任何范围重定向到外部命令(无论是 bash、python 还是您自己的 python 脚本)。
# 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,
然后:%w !python
将运行它,无论是否保存。并在您的终端上打印如下内容,
[[ 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.
第一个运行当前行,第二个运行视觉选择,通过 python 解释器。
#!! 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
对于 cmdline 范围,请参阅
:h cmdline-ranges
not below one, which concerning normal command, not cmdline one.
不低于一,这与正常命令有关,而不是 cmdline 一。
:h command-range
inspired by https://stackoverflow.com/a/19883963/3625404
回答by Rohan Ghige
Add this mapping to your .vimrc file,
将此映射添加到您的 .vimrc 文件中,
nmap <leader>E yyp:.!csh<CR>
Explanation:
解释:
yy
Yank current linep
Paste 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.
yy
扬升电流线p
粘贴下面的猛拉线(光标移到下一行):.!csh<CR>
执行(使用csh
)这个新粘贴的行。该行的输出替换了当前的这一行,因此在执行该行之前被拉出并粘贴到下面。
回答by Brux
If I have a command on a line of text within vi/vim like this"
如果我在 vi/vim 中的一行文本上有这样的命令”
ls -la
ls -la
Position the cursor anywhere on the line and do ":.!!" and press return.
将光标定位在该行的任意位置并执行“:.!!” 然后按回车。
That is: colon dot bang bang return
即:冒号 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.
这将从 vi/vim 中执行当前 shell 中该行中的文本,并将输出插入文本文件中。
I'm thinking that is what you were asking? it's like magic.
我想这就是你要问的?这就像魔术。
回答by Rohan Ghige
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,
考虑您已将上述命令写入文件。要执行此命令将相应的输出返回到该文件中,您可以在您的 .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
转到文件中的这一行,然后按<leader>E
。在我的情况下,<leader>
被映射到,
因此我会按,E