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

后端 未结 8 2131
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-23 18: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


        
相关标签:
8条回答
  • 2020-12-23 19:30

    In addition to selected answer above,

    • when using the mouse to select (1),

    • and the problem of only copying whole lines mentioned by the comment of @dronus to it, when just wanted to partly copy lines (2):

    (1) At my Debian based DietPi (Raspberry PI)system, vim acts a little different like in the preferred solution above when using the mouse to enter and select 'VISUAL MODE' on my Ubuntu 16.04 work station. Then

    • y to yank it

    but if I type ':' for command, it will not show up with the

    '<,'>
    

    where I can just simply add my

    w new.txt
    

    after it. So I just typed it by myself and it did work:

    '<,'>w new.txt
    

    and it copies the whole line(s) yanked content to my file 'new.txt', whereas '<,' seem to mean 'copy selected lines and '>' redirect it to the write command's referenced file.

    (2) And to the problem of not pasting part of the line(s), like in @dronus comment mentioned, this solution (the selected one, first alternative) worked for me:

    Edit the first file, yanking the text you want. Then open your second file from within vi (:e /path/to/other/file) and paste it (by typing p). Save it (like above ':w new.txt'.

    It will then just copy the part of the lines marked up by mouse or 'y' with the cursors.

    [EDIT] On my Ubuntu system: Sometimes selecting by mouse does NOT enter 'VISUAL MODE' in vim. Then the normal copy/paste can be selected using the context menu... I have not found the reason why Ubuntu changed it behaviour from a 'client acting behaviour' to a 'host' one (withUbuntu hosting the ssh bash window to my 'Client')...

    0 讨论(0)
  • 2020-12-23 19:32

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

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

    0 讨论(0)
提交回复
热议问题