Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I save a text block in visual mode to a file in Vim?

The title is very descriptive. Just in case, I will give an example:

START BLOCK1 something END BLOCK1  START BLOCK2 something somenthing... END BLOCK2 
  1. I select the BLOCK1 in visual mode
  2. I yank it by pressing y
  3. How can I save the yanked BLOCK1 to some other file?
like image 932
Léo Léopold Hertz 준영 Avatar asked Apr 28 '09 21:04

Léo Léopold Hertz 준영


People also ask

How do I copy a visual block in Vim?

You can copy a block of text by pressing Ctrl-v (or Ctrl-q if you use Ctrl-v for paste), then moving the cursor to select, and pressing y to yank. Now you can move elsewhere and press p to paste the text after the cursor (or P to paste before).

How do I use 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 save a file in Vim?

If you didn't make any changes, type :q and press Enter / return. If you made some changes and would like to keep them, type :wq and press Enter / return.


1 Answers

Select the text you wish to save, in either line visual or block visual mode, and

:w new.txt 

That's what you type, but you won't actually see exactly what's above. When you press :, you'll go to the command line which will automatically get filled in with selection information. It'll look something like this:

:'<,'> 

Just carry on typing the rest (w new.txt) to get

:'<,'>w new.txt 

...and press enter.

like image 106
Rook Avatar answered Oct 01 '22 12:10

Rook