tags:

views:

147

answers:

2

I have the following commands in a README file:

./Setup ...
./Setup ...
./Setup ...

I want to run them by selecting the codes visually and then running them.

I run unsuccessfully

: '<,'> !

Current code after Luc's comments in his answer

My code in .vimrc which I have not managed to get to work:

vmap <silent> <leader>v y:exe '!'.join(split(@", "\n"),';')<cr>

I am trying to make make a keyboard combination for

v yy

How can you get the above command work, such that you can run file's commands directly in Vim?

+5  A: 
  • First select your text,
  • then copy it with y,
  • and finally, you can execute:

    :exe '!'.join(split(@", "\n"),';')

Luc Hermitte
Your command works! Thank you. I wonder if there are any shorter way to do the same. It is difficult for me to remember the command.
Masi
You can define a mapping: -> :vnoremap whatevercombo y:exe '!'.join(split(@", "\n"),';')<cr>
Luc Hermitte
@Luc: Could you give an example of a combo. I am new in making them. I put unsuccessfully <tab-a> as a combo, that is press TAB and A to get the last part.
Masi
It will be <tab>a. We put two (or more) keys within the angle brackets only with keys like *c*trl, *s*hift, and *a*lt (/*m*eta) to denote the keys are pressed together (-> <c-a>, <s-e>, <c-s-z>, <a-del>, etc.)
Luc Hermitte
I made the following unsuccessful code % vmap <silent> <leader>v :exe '!'.join(split(@", "\n"),';')<cr> % I get "no range allowed", although I have only % echo masi % in my the selection of visual mode.
Masi
You forgot to yank the current selection.
Luc Hermitte
@Luc: I have now % vmap <silent> <leader>v y:exe '!'.join(split(@", "\n"),';')<cr> % I get "no range allowed", although I have only %. I am unable to get the code to work. How can you get the code to work?
Masi
As I said: :vnoremap <leader>v y:exe '!'.join(split(@", "\n"),';')<cr>
Luc Hermitte
@Luc: I have tested the combinations now ...<leader>v XXXX:exe ... where XXXX has been a, <a> and <c-a>. I go to the visual mode and press a or C-a: nothing happens. --- Do you know why?
Masi
This mapping has only one purpose : execute all lines in an external shell. What do you want to accomplish ?
Luc Hermitte
@Luc: I want to execute all selected lines of the file in Visual mode.
Masi
Then XXX is y and nothing else.
Luc Hermitte
+1  A: 

This might be oversimplifying things, but why not just do:

:e README
:%!bash

This filters the current file through bash, executing each line as a command. The current buffer is replaced by the output of running all of the commands in the file.

It might be helpful to do a :w RESULTS to save it as another file first, so you don't accidentally overwrite the original:

:e README
:w RESULTS
:%!bash

You had said you wanted to do this with a visual selection, which would work just as well After you select what you want to execute, type :. '<,'> will automatically be prepended to the current command. '< is the mark of the beginning of the current selection, whereas '> is the mark at the end of the current selection. You can just run just those commands you have selected just like above:

:'<,'>!bash

This will replace just the selected commands with the output of executing those commands.

Conspicuous Compiler
@Compiler: Thank you for your answer!
Masi