Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
311 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 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...