9

I wonder if there is any command similar to :TOhtml, but for plain text only and representing the whole Vim display.

For example, given the display:

enter image description here

It would create the following text file:

  1 B                         1 a                     
~                           ~                         
~                           ~                         
~                           ~                         
~                           ~                         
~                           ~                         
~                           ~                         
~                           ~                         
 N  <me] [+]      100% 1:1  ~                         
  1 a                       ~                         
~                           ~                         
~                           ~                         
~                           ~                         
~                           ~                         
~                           ~                         
~                           ~                         
 N  <me] [+]      100% 1:1   N  <e] [+]      100% 1:1 

(this has been done via copy/paste from the terminal and manually formatting the text)

I did not found any option to do so, I think there is none.

How, if possible, can Vim achieve this?

3
  • 1
    Hmm. I don't think there is any way for Vim to "see" its own window (that is, your screenshot above, as opposed to the contents of the buffers). How curious. Aug 20, 2016 at 12:01
  • To my mind that is not a problem for vi, but for the terminal -- which admittedly is not very different from using the mouse! gnome-terminal can "select all", but I haven't checked of you need to reformat after pasting. On another tack, I would love something that dumped the whole (scrollable) window to a file!
    – Law29
    Aug 20, 2016 at 23:12
  • You can use your mouse to select the entire screen, including the tab names at the top as well as the status bar at the bottom and right click outside to paste the copied screen. This worked for me. I used 7.2 vim in Linux.
    – SibiCoder
    Aug 21, 2016 at 7:02

1 Answer 1

5

Yes, there is a way to do this! You can use the screenchar() function. From :help screenchar()

screenchar(row, col)                        *screenchar()*
        The result is a Number, which is the character at position
        [row, col] on the screen.  This works for every possible
        screen position, also status lines, window separators and the
        command line.  The top left position is row one, column one
        The character excludes composing characters.  For double-byte
        encodings it may only be the first byte.
        This is mainly to be used for testing.
        Returns -1 when row or col is out of range.

To use this in a script, you can do the following:

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()

This lovely piece of vimscript was written by Christian Brabandt, so if you find it useful go give him an upvote.

Here's an example of it in action. When I ran this function on my vim window:

enter image description here

I got this text:

  1                                    |  1                                     
~                                      |~                                       
~                                      |~                                       
~                                      |~                                       
~                                      |~                                       
~                                      |~                                       
~                                      |~                                       
~                                      |~                                       
~                                      |~                                       
~                                      |~                                       
~                                      |~                                       
[No Name]            0,0-1          All|~                                       
  1                                    |~                                       
~                                      |~                                       
~                                      |~                                       
~                                      |~                                       
~                                      |~                                       
~                                      |~                                       
~                                      |~                                       
~                                      |~                                       
~                                      |~                                       
~                                      |~                                       
~                                      |~                                       
[No Name]            0,0-1          All [No Name]             0,0-1          All
:call ScreenCapture()                                                           

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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