2

Vim can often show you text differing from the actual input, such as with non-printing characters, concealed text, etc. There are plugins which prettify text (for example, using a proper lambda character instead of \ in Haskell). How can I copy the text so displayed?

I could use:

  • the mouse
  • screen/tmux

But I would prefer a within-Vim solution.

To clarify, I only care about the text, not colours or highlighting. For example, if it shows ^@ for NUL, or ^H for backspace, or λ for \, then I want my copied text to use ^@, ^H and λ respectively.

  • 3
    Does :TOhtml work as expected? If it does, you could try borrowing whatever method that uses. – Martin Tournoij Nov 18 '15 at 21:28
  • @Carpetsmoker yes, it does show the displayed characters. – muru Nov 18 '15 at 22:47
  • The source for that is at /usr/share/vim/vim74/autoload/tohtml.vim. Taking a quick peek at it, it seems to use getline() to get the data. So a mapping that does let @+ = getline('.') should work. For more lines you need a slightly more complex function... – Martin Tournoij Nov 19 '15 at 11:49
8

For the terminal, I think, the simplest solution is to let the terminal handle the mouse selection by pressing shift when visually selecting with the mouse.

Another solution would be to script something using the screenchar() function. But I don't know of a ready plugin for that. Update But you can script something like this:

fu! ScreenCapture()
    let array=[]
    for i in range(1,&lines)
        let row=''
        for j in range(1,&columns)
            let row.=nr2char(screenchar(i,j))
        endfor
        call add(array, row)
    endfor
    tabnew
    call setline(1,array)
endfu
com! ScreenCapture :call ScreenCapture()

Both solutions won't handle syntax highlighting. An alternative that can handle even syntax highlighting and signs would be to use the distributed 2html vim plugin. This converts the current buffer to a html page including syntax highlighting (or diff mode) that you can open with the browser. For me, this is the prefered solution to pretty print something from within Vim.

  • Sorry, I wasn't clear. I don't want to copy colours, but only the displayed characters. For example, if it shows ^@ for NUL, or ^H for backspace, or λ for \, then I want my copied text to use ^@, ^H and λ respectively. Indeed, for selecting with the mouse, column-wise selection is better than using just Shift. – muru Nov 18 '15 at 21:08
  • @muru What I do, when I want this is: I usually hold the shift key and select (linewise) with the mouse. Further post-processing (like column wise selection is done in another instance of Vim – Christian Brabandt Nov 18 '15 at 21:12
  • Yes, but that adds nothing to the discussion - I have already mentioned using the mouse. – muru Nov 18 '15 at 21:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.