Vim sign column toggle

If you're willing to wait for Bram to get to it on his TODO list, or are willing to patch/compile Vim yourself, a patch was recently submitted to allow this with a new 'signcolumn' option. https://groups.google.com/d/topic/vim_dev/CrBId6DRbvo/discussion


Update Since patch 7.4.2201 you can make use of the 'signcolumn' option to disable displaying the signs. Have a look at the documentation :h 'signcolumn'

Well, you need to unplace all signs for the current buffer to have them not displayed. With recent Vims (e.g. newer thane 7.3.596) you can simply use :sign unplace *.

You can take my plugin https://github.com/chrisbra/SaveSigns.vim to save those signs to a temporary file (which will in fact create a Vim script, to be able to replace all the signs. Using that plugin you can write a custom function to toggle displaying the signs.

Something like this might work for you:

fu! MySignsToggle()
    if !has("signs") || empty(bufname(''))
        return
    endif
    if !exists("s:signfile")
        let s:signfile = tempname().'_'
    endif
    redir =>a|exe "sil sign place buffer=".bufnr('')|redir end
    let signs = split(a, "\n")[1:]
    if !empty(signs)
        let bufnr = bufnr('')
        exe ":sil SaveSigns!" s:signfile.bufnr('')
        if bufnr('') != bufnr
            exe "noa wq"
        endif
        sign unplace *
    elseif filereadable(s:signfile.bufnr(''))
        exe "so" s:signfile.bufnr('')
        call delete(s:signfile.bufnr(''))
    endif
endfu

Based on @elliottcable 's answer (Many thanks for that!), I wrote a simple toggle function and mapped it to <Leader>2

nnoremap <Leader>2 :call ToggleSignColumn()<CR>

" Toggle signcolumn. Works on vim>=8.1 or NeoVim
function! ToggleSignColumn()
    if !exists("b:signcolumn_on") || b:signcolumn_on
        set signcolumn=no
        let b:signcolumn_on=0
    else
        set signcolumn=number
        let b:signcolumn_on=1
    endif
endfunction

Or if you want to toggle line number as well, simply modify the mapping line:

nnoremap <Leader>2 :set number!<CR>:call ToggleSignColumn()<CR>

Hope it is helpful :)


If you are using Vim 8.0 or newer (or NeoVim), this is now a simple setting:

$ vim "+help signcolumn" "+only"

For instance,

:set scl=no   " force the signcolumn to disappear
:set scl=yes  " force the signcolumn to appear
:set scl=auto " return the signcolumn to the default behaviour

Tags:

Vim