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

后端 未结 8 2130
爱一瞬间的悲伤
爱一瞬间的悲伤 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:08

    Vim get the visual selection and save to a file:

    function! Get_visual_selection()
        #Get the position of left start visual selection
        let [line_start, column_start] = getpos("'<")[1:2]
        #Get the position of right end visual selection
        let [line_end, column_end] = getpos("'>")[1:2]
        #gotta catch them all.
        let lines = getline(line_start, line_end)
        if len(lines) == 0
            return ''
        endif
        #edge cases and cleanup.
        let lines[-1] = lines[-1][: column_end - 2]
        let lines[0] = lines[0][column_start - 1:]
        return join(lines, "\n")
    endfunction
    
    function Save_visually_selected_text_to_file()
        let selected_text = Get_visual_selection()
        call writefile(split(selected_text, "\n"), "/tmp/something.txt")
    endfunction
    
    #the c-u does a union of all lines in visual selection.
    #this goes in the vimrc
    vnoremap <F10> :<c-u>call Save_visually_selected_text_to_file()<cr>
    
    0 讨论(0)
  • 2020-12-23 19:12

    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
    

    ...and press enter.

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

    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.

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

    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.

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

    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:

    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.

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

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

    vmap <C-c> y:new ~/.vimbuf<CR>VGp:x<CR>:!pbcopy < ~/.vimbuf<CR><CR>
    
    0 讨论(0)
提交回复
热议问题