问题
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?
回答1:
:[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
回答2:
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.
回答3:
I do it like this, FYR~
Once you've selected partial of your content by "Visual mode", press Ctrl-C to trigger it
vmap <C-c> y:new ~/.vimbuf<CR>VGp:x<CR>
来源:https://stackoverflow.com/questions/12282573/save-selected-text-partial-line-from-vim