Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run shell command on visual selection

Tags:

shell

vim

I'm trying to run a shell command on a visual selection but I can't figure out how to run it on the selection and not the selected lines, eg:

    Selection start here
    v
 hello
 world
   ^ Ends here

Running the following command will send hello\nworld to my_command

:'<,'>!my_command

How can i send lo\nwor to my_command. Where \n is a newline

Upon reading the :help '< page I got the impression what '< should be simulate to `<:

 '<  `<     To the first line or character of the last selected
            Visual area in the current buffer.  For block mode it
            may also be the last character in the first line (to
            be able to define the block).  {not in Vi}.
like image 439
Andreas Louv Avatar asked Oct 30 '22 04:10

Andreas Louv


1 Answers

You can achieve something similar using a key map.

vnoremap <C-s> y:! <C-r>0<Home><right>

This will map Ctrl-S in visual mode. It copies selected text, invokes command line and pastes the selected text and then moves the cursor to enter a command at !.

like image 148
ronakg Avatar answered Dec 13 '22 05:12

ronakg