Save selected text (partial line) from Vim

你。 提交于 2019-12-08 17:25:21

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!