Search code examples
vimviml

VimL: VSPLIT with width specified as a function of total width


I want to write a vim script that opens a new window with vsplit, where the width of the new window is equal to the total number of columns minus 90.

The result would be, the current window would be 90 columns wide (to view 80 cols of code + gutter) and the new vsplit would occupy whatever part of the screen is left over.

If I understand vsplit correctly, :vsplit 90 specifies the window being created should be 90 columns. Is there a way to get the current number of columns in a window into a variable?

let cur_cols = [insert magic here]
let win_width = cur_cols - 90
execute "vsplit ". win_width

Solution

  • Use winwidth to find width of window. winwidth returns width as the no. of characters window can accommodate. In your case, use

    let cur_cols = winwidth(0)
    

    Here the parameter 0 refers to current widow. For more information,

    :help winwidth
    :help :vsplilt