I wanted vim to increase its &columns automatically when I do a :vsplit, so I implemented the advice found here: https://groups.google.com/forum/#!topic/vim_use/R4gg-YdPHJM

Basically, you add:

autocmd WinEnter * if winwidth(0) < 80 | set columns+=80 | wincmd = | endif

to .vimrc.

However, in terminal vim, I opened 3 windows side-by-side, and the &columns increased beyond the size of my terminal.

I tried to do this:

if &columns + 79 > $COLUMNS | set columns=$COLUMNS | else | set columns+=79 | endif

but it didn't work, saying, "Number required after =: columns=$COLUMNS".

How can I get &columns to increase but only up to a maximum of the $COLUMNS environment variable?


COLUMNS is a shell variable, not an environmental variable.

You could do something like this:

This exports COLUMNS before invoking vim

alias vim='export COLUMNS;vim $*'

However, the value of the exported COLUMNS variable won't be updated if you resize your terminal window while you are running vim.


Gets the terminal's columns directly without using an environmental variable

let cols = system('tput cols')
if &columns + 79 > cols | set columns=cols | else | set columns+=79 | endif
  • 1
    Thanks! I didn't know about shell variables vs. environment variables until now. set columns=cols didn't work. It needs to be execute "set columns=".cols Also, just in case someone else is also using tmux like me, instead of tput cols I need to do tmux display-message -p "#{pane_width}" – Flurrywinde May 9 '17 at 21:44

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.