如何将可视模式下的文本块保存到Vim中的文件?

时间:2022-06-11 19:19:54

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. 我在可视模式下选择BLOCK1
  3. I yank it by pressing y
  4. 我按下y来猛拉它
  5. How can I save the yanked BLOCK1 to some other file?
  6. 如何将拉出的BLOCK1保存到其他文件中?

6 个解决方案

#1


120  

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)即可

:'<,'>w new.txt

...and press enter.

...然后按Enter键。

#2


16  

With the block is selected, you can :'<,'>w other-file, which will write only the selected block to other-file. Hitting : in visual mode should place '<,'> into the command line for you already, so you really only have to type :w other-file.

选择块后,您可以:'<,'> w其他文件,它只会将所选块写入其他文件。点击:在可视模式下,应该将'<,'>放入命令行,所以你真的只需输入:w other-file。

#3


4  

There's probably a simpler way to do this, but what I would do is create a new buffer (or tab) and then paste it in with p. You can create a new buffer with :new or a new tab with :tabnew. You can write the buffer/tab to a file as normal with :w filename.

可能有一种更简单的方法,但我要做的是创建一个新的缓冲区(或标签),然后用p粘贴它。您可以使用以下命令创建新缓冲区:new或新选项卡:tabnew。您可以使用:w filename将缓冲区/选项卡正常写入文件。

#4


2  

Like @dronus mentioned in the comments, the :w !pbcopy suggestions does not copy correctly because it copies the entire line. If I want to copy only the url in a line, I will not be able to. Here's a line that you can add to your .vimrc file so that everytime you hit CTRL-C, the selected line in your vim will be copied to clipboard:

与评论中提到的@dronus一样,:w!pbcopy建议无法正确复制,因为它会复制整行。如果我只想复制一行中的网址,我将无法做到。这是您可以添加到.vimrc文件的行,这样每次按CTRL-C时,vim中的选定行都将被复制到剪贴板:

map <C-c> y:e ~/clipsongzboard<CR>P:w !pbcopy<CR><CR>:bdelete!<CR>

If you'd like to read details about what this does, you can read about this on my blog

如果您想阅读有关此功能的详细信息,可以在我的博客上阅读

Its the same implementation as what @rmeador suggested.

其实现与@rmeador建议的相同。

#5


1  

Similar to @songz's solution, I prefer do it like this using ":new"

与@ songz的解决方案类似,我更喜欢使用“:new”这样做

vmap <C-c> y:new ~/.vimbuf<CR>VGp:x<CR>:!pbcopy < ~/.vimbuf<CR><CR>

#6


0  

Basing on @chenkaie's variant, works well for me:

基于@ chenkaie的变体,适合我:

let mapleader = "," let g:mapleader = "," vmap <leader>y y:new ~/.vbuf<CR>VGp:x<CR> nmap <leader>p :r ~/.vbuf<CR>

let mapleader =“,”让g:mapleader =“,”vmap y y:new~ / .vbuf VGp:x nmap p:r~ / .vbuf

#1


120  

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)即可

:'<,'>w new.txt

...and press enter.

...然后按Enter键。

#2


16  

With the block is selected, you can :'<,'>w other-file, which will write only the selected block to other-file. Hitting : in visual mode should place '<,'> into the command line for you already, so you really only have to type :w other-file.

选择块后,您可以:'<,'> w其他文件,它只会将所选块写入其他文件。点击:在可视模式下,应该将'<,'>放入命令行,所以你真的只需输入:w other-file。

#3


4  

There's probably a simpler way to do this, but what I would do is create a new buffer (or tab) and then paste it in with p. You can create a new buffer with :new or a new tab with :tabnew. You can write the buffer/tab to a file as normal with :w filename.

可能有一种更简单的方法,但我要做的是创建一个新的缓冲区(或标签),然后用p粘贴它。您可以使用以下命令创建新缓冲区:new或新选项卡:tabnew。您可以使用:w filename将缓冲区/选项卡正常写入文件。

#4


2  

Like @dronus mentioned in the comments, the :w !pbcopy suggestions does not copy correctly because it copies the entire line. If I want to copy only the url in a line, I will not be able to. Here's a line that you can add to your .vimrc file so that everytime you hit CTRL-C, the selected line in your vim will be copied to clipboard:

与评论中提到的@dronus一样,:w!pbcopy建议无法正确复制,因为它会复制整行。如果我只想复制一行中的网址,我将无法做到。这是您可以添加到.vimrc文件的行,这样每次按CTRL-C时,vim中的选定行都将被复制到剪贴板:

map <C-c> y:e ~/clipsongzboard<CR>P:w !pbcopy<CR><CR>:bdelete!<CR>

If you'd like to read details about what this does, you can read about this on my blog

如果您想阅读有关此功能的详细信息,可以在我的博客上阅读

Its the same implementation as what @rmeador suggested.

其实现与@rmeador建议的相同。

#5


1  

Similar to @songz's solution, I prefer do it like this using ":new"

与@ songz的解决方案类似,我更喜欢使用“:new”这样做

vmap <C-c> y:new ~/.vimbuf<CR>VGp:x<CR>:!pbcopy < ~/.vimbuf<CR><CR>

#6


0  

Basing on @chenkaie's variant, works well for me:

基于@ chenkaie的变体,适合我:

let mapleader = "," let g:mapleader = "," vmap <leader>y y:new ~/.vbuf<CR>VGp:x<CR> nmap <leader>p :r ~/.vbuf<CR>

let mapleader =“,”让g:mapleader =“,”vmap y y:new~ / .vbuf VGp:x nmap p:r~ / .vbuf