Executing text as a shell command

0 views
Skip to first unread message

Dave Tapley

unread,
Jan 13, 2008, 1:27:24 PM1/13/08
to vim...@googlegroups.com
Hi there,

No luck with Google for this one, not even sure if it is a feature but
I've come across the need for it a few times:

I would like to be able to take a line (or buffer) in Vim and send it
to the shell in the fashion that :! can be used to execute shell
commands.

So for example if I have a readme.txt with the line:
sudo apt-get install lots of packages

I execute that line without resorting to yank / pasting.

Cheers,
Dave

Tony Mechelynck

unread,
Jan 13, 2008, 2:12:57 PM1/13/08
to vim...@googlegroups.com

See ":help filter".

(untested)
On Unix:
(single line)
:.!bash
:undo
(Visual: Vim inserts '<,'> when you hit the colon key)
:'<,'>!bash
:undo
(whole buffer)
:%!bash
:undo

On Windows, use cmd instead of bash

The line(s) will be replaced by the output of the external command, hence the
undo...


However, it's sometimes simpler to yank & paste, since then you don't have to
undo:

yank with
yy
paste on the Vim command-line:
:!<Ctrl-R>"<BS><Enter>

where <Ctrl-R> means "hit Ctrl-R" etc.
The <BS> (optional) is there to remove the Ctrl-M representing the end-of-line.


Or: yank in Vim, to the clipboard

"+yy
or (10 lines)
"+10yy
or (in linewise-visual)
"+y

then paste in a separate shell, using whatever (such as "Edit -> Paste")
applies to the terminal where the shell is running.


Best regards,
Tony.
--
Pittsburgh Driver's Test

(8) Pedestrians are

(a) irrelevant.
(b) communists.
(c) a nuisance.
(d) difficult to clean off the front grille.

The correct answer is (a). Pedestrians are not in cars, so they are
totally irrelevant to driving; you should ignore them completely.

Kazuo Teramoto

unread,
Jan 13, 2008, 3:00:32 PM1/13/08
to vim...@googlegroups.com
On 1/13/08, Tony Mechelynck <antoine.m...@gmail.com> wrote:
> See ":help filter".
>
> (untested)
> On Unix:
> (single line)
> :.!bash
> :undo
> (Visual: Vim inserts '<,'> when you hit the colon key)
> :'<,'>!bash
> :undo
> (whole buffer)
> :%!bash
> :undo
>
> On Windows, use cmd instead of bash
>
> The line(s) will be replaced by the output of the external command, hence the
> undo...

On linux (I don't have other system to test) you can use

:.w !bash
:'<,'>w !bash
:w !bash

if it you don't need the undo.

--
«Dans la vie, rien n'est à craindre, tout est à comprendre»
Marie Sklodowska Curie

Kazuo Teramoto

unread,
Jan 13, 2008, 3:03:39 PM1/13/08
to vim...@googlegroups.com
On 1/13/08, Kazuo Teramoto <kaz...@gmail.com> wrote:
> On linux (I don't have other system to test) you can use
>
> :.w !bash
> :'<,'>w !bash
> :w !bash

the help topic is:
:help :write_c

--
«Dans la vie, rien n'est à craindre, tout est à comprendre»

Marie Sklodowska Curie.

Charles E. Campbell, Jr.

unread,
Jan 13, 2008, 6:27:52 PM1/13/08
to vim...@googlegroups.com
Dave Tapley wrote:

You could use RunView for this (see
http://mysite.verizon.net/astronaut/vim/index.html#RUNVIEW).

:[range]RunView[!] command (such as sh)
(visual-select)\rv (uses the string in g:runview_filtcmd (default:
"ksh") for the filter-command)

Regards,
Chip Campbell

Jürgen Krämer

unread,
Jan 14, 2008, 4:02:30 AM1/14/08
to vim...@googlegroups.com

Hi,

:call system(getline('.'))

jkr

--
Sometimes I think the surest sign that intelligent life exists elsewhere
in the universe is that none of it has tried to contact us. (Calvin)

Reply all
Reply to author
Forward
0 new messages