Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I insert the current visual selection phrase into the command-line?

Tags:

vim

When entering an argument on the command, I know I can type <C-R><C-W> to insert the word under the cursor and <C-R><C-A> to insert the WORD under the cursor.

Is there a way to insert the current visual-selection?

Thanks!

like image 689
Jeet Avatar asked Sep 01 '10 15:09

Jeet


People also ask

How do I enable visual mode in Vim?

To get into the Vim Visual Line mode, press “Shift+V” while you are in Vim's Normal mode.

How do I select a line of code in Vim?

Place your cursor anywhere on the first or last line of the text you want to manipulate. Press Shift+V to enter line mode. The words VISUAL LINE will appear at the bottom of the screen. Use navigation commands, such as the Arrow keys, to highlight multiple lines of text.


2 Answers

You can use the contents of any register on the ex or search command-lines with <C-R> followed by the register's name. By yanking your visual selection, it is put into the 0 register, so <C-R>0 will add your yanked selection to the current command-line.

That is the only way I know of, but I still use it on a daily basis.

like image 139
Walter Avatar answered Sep 19 '22 11:09

Walter


There are a few ways your question can be construed. The easiest is if you mean you want to give the selected text as a range argument to an ex command. I doubt this is your question, since this happens automatically if you type : with a visual selection, but the ex syntax for this

:'<,'> 

The second way I think your question could be construed is that you want to insert the visual selected text itself as an argument to an ex command; I don't think this can be done. If you read the vim manual section 40.2, where range arguments are described, the only things that a command is allowed to grab from a range argument is the number of the first line and the last line (using the tags <line1> and <line2>).

Finally, if you want to run the selected text on the shell command line, all you need to do is select it and type

:!sh 

(The '<,'> part should get inserted for you between the : and the !. You can replace 'sh' with the command to start your favorite shell).

like image 43
Daisy Sophia Hollman Avatar answered Sep 19 '22 11:09

Daisy Sophia Hollman