Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
321 views
in Technique[技术] by (71.8m points)

Execute shell command without filtering from Vim

I want to select a block of text (for example, V%) and use the text as input to a shell command (for example, wc or pbcopy) - but I don't want to alter the current buffer - I just want to see the output of the command (if any) then continue editing without any changes.

Typing V%!wc translates to :'<,'>!wc and switches the block of text for the output of the wc command.

How do you pipe a chunk of text to an arbitrary shell command without affecting the current buffer?

question from:https://stackoverflow.com/questions/1237780/execute-shell-command-without-filtering-from-vim

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Select your block of text, then type these keys :w !sh

The whole thing should look like:

:'<,'>w !sh

That's it. Only took me 8 years to learn that one : )

note: typing : after selecting text produces :'<,'> a range indicating selection start and end.

Update 2016: This is really just one use of the generic:

'<,'>w !cli_command

Which basically lets you "send" arbitrary parts of your file to external commands and see the results in a temporary vi window without altering your buffer. Other useful examples would be:

'<,'>w !wc
'<,'>w !to_file my_file

I honestly find it more useful to alter the current buffer. This variety is simply:

'<,'>!wc
'<,'>!to_file my_file

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...