I try to save some selected text (part of a line) from Vim. Here is the line:
THIS TEST STRING - SELECTED_TARGET_WORLD
where the bold represents the select text. I do this:
:'<,'> w! test/selected_text
but in the file selected_text
I find the string:
THIS TEST STRING - SELECTED_TARGET_WORLD
How do I make it save only the selected part of the line?
If you want to select the entire line in a file, press V. Now when you press k or j to go up and down, vim will select the entire line above and below your cursor. Finally, you can select text in columns by pressing ctrl+v and moving up or down the block.
Press v to begin character-based visual selection, or V to select whole lines, or Ctrl-v or Ctrl-q to select a block. Move the cursor to the end of the text to be cut/copied. While selecting text, you can perform searches and other advanced movement.
You can copy to the end of the line with y$ and paste with p . To copy/paste between different instances, you can use the system clipboard by selecting the * register, so the commands become "*y$ for copying and "*p for pasting.
:[range]w filename
only works with lines so… you have to put the selected text on its own line.
An alternative using :help :redir
:
:'<,'>"ay
:redir filename
:echo @a
:redir END
That case isn't documented in the help (:h :w
) but :w
handles only line ranges (you would have seen that on a example of multiple lines).
To do what you want, you will have to first paste your selection to a temporary buffer (or on its own line and then put it back in place) and then save that buffer. That can easily be automatized if it's something you're gonna do often.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With