Get "usable" window width in vim script
My ingo-library plugin has a ingo#window#dimensions#NetWindowWidth()
function for that.
I think, you should be able to get that width using:
:set virtualedit=all
:norm! g$
:echo virtcol('.')
Alternatively, you could check, whether a signcolumn is present (e.g. using redir
)
:redir =>a |exe "sil sign place buffer=".bufnr('')|redir end
:let signlist=split(a, '\n')
:let width=winwidth(0) - ((&number||&relativenumber) ? &numberwidth : 0) - &foldcolumn - (len(signlist) > 1 ? 2 : 0)
Answering because I can't comment yet:
Christian's answer gives the wrong result in the case that the actual number of lines in the file exceeds &numberwidth
(because &numberwidth
is just a minimum, as kshenoy pointed out). The fix is pretty simple, though, just take the max()
of &numberwidth
and the number of digits in the last line in the buffer (plus one to account for the padding vim adds):
redir =>a | exe "silent sign place buffer=".bufnr('') | redir end
let signlist = split(a, '\n')
let lineno_cols = max([&numberwidth, strlen(line('$')) + 1])
return winwidth(0)
\ - &foldcolumn
\ - ((&number || &relativenumber) ? lineno_cols : 0)
\ - (len(signlist) > 2 ? 2 : 0)
Kale's answer corrected one corner case where the number of lines is exceeding what &numberwidth
can display. Here I fix another corner case where the signcolumn
option is not set to auto
function! BufWidth()
let width = winwidth(0)
let numberwidth = max([&numberwidth, strlen(line('$'))+1])
let numwidth = (&number || &relativenumber)? numberwidth : 0
let foldwidth = &foldcolumn
if &signcolumn == 'yes'
let signwidth = 2
elseif &signcolumn == 'auto'
let signs = execute(printf('sign place buffer=%d', bufnr('')))
let signs = split(signs, "\n")
let signwidth = len(signs)>2? 2: 0
else
let signwidth = 0
endif
return width - numwidth - foldwidth - signwidth
endfunction