Gvim: passing visually selected text to the command line

I use gvim to store recipes for the commands that I will execute, depending on the output. Currently, I select the text in gvim and paste the commands into the terminal console, but I am sure that I can pass the visually selected range on the command line for execution.

+4
source share
2 answers

Assuming you mean the Vim command line:

(if you mean the OS command line, see below).

For parts of strings (i.e. end of line character) you can do something like this:

" Visually select lines, then: y:<CR>"<ENTER> 

where <CR> means pressing Ctrl + R. y 'yanks' selected text : goes into command mode, <CR>" extracts the contents of the register " (the last yanked text) on the command line and <ENTER> (obviously) executes the command.


If you want to do linear things, it's a little more complicated (since the command line doesn't like ^M ). I would recommend something like this in your vimrc:

 function! RunCommands() exe getline('.') endfunction command -range RunCommands <line1>,<line2>call RunCommands() vmap ,r :RunCommands<CR> 

Select the lines (after restarting vim) and press ,r .


Another way that may be useful is to copy the necessary lines, press q: to open a command prompt window and paste the necessary lines there, and then move the cursor over the desired line and press ENTER . This has the advantage that you can edit the command before pressing ENTER . He will only run one command at a time.

If you mean (for example) the Windows or Linux command line:

Use the function above, but instead:

 exe getline('.') 

using

 call system(getline('.')) 

or, if you want to see the result:

 echo system(getline('.')) 

or

 echomsg system(getline('.')) 

For more information:

 :help :echo :help :echomsg :help :messages :help :vmap :help :command-range :help :command :help :function :help c_CTRL-R :help :exe :help getline() :help system() 
+16
source

If you use the vim GUI, you can do set guioptions+=a . Thus, any selected text inside gvim is pasted into the clipboard in visual mode.

+2
source

Source: https://habr.com/ru/post/1336004/


All Articles