Michal Krejčí
Michal Krejčí

Reputation: 75

Get columns in terminal in Vim

I'm trying to get terminal size from within Vim, but calling :!tput cols always returns 80 no matter actual size of the terminal. Running the command from terminal returns correct value.

Is there a way to get number of columns in terminal from within Vim? Why is the command above yielding incorrect value? I guess it has something to do with how Vim runs external command.

There's also $COLUMNS shell variable but it looks these are not exported to child process (vim) as stated here: LINES and COLUMNS environmental variables lost in a script

Upvotes: 5

Views: 3685

Answers (2)

Kusalananda
Kusalananda

Reputation: 15633

I find :set co returns the number of columns.

'columns' 'co'          number  (default 80 or terminal width)
                        global
                        {not in Vi}
        Number of columns of the screen.  Normally this is set by the terminal
        initialization and does not have to be set by hand.  Also see
        |posix-screen-size|.
        When Vim is running in the GUI or in a resizable window, setting this
        option will cause the window size to be changed.  When you only want
        to use the size for the GUI, put the command in your |gvimrc| file.
        When you set this option and Vim is unable to change the physical
        number of columns of the display, the display may be messed up.  For
        the GUI it is always possible and Vim limits the number of columns to
        what fits on the screen.  You can use this command to get the widest
        window possible: >
                :set columns=9999
<       Minimum value is 12, maximum value is 10000.

Upvotes: 7

yayg
yayg

Reputation: 319

You can do :echo winwidth('%') to get the current width of your window/term.

Upvotes: 0

Related Questions