'Vim sign column toggle

When signs are defined in Vim, a column appears at the left of the screen.

From the Vim help:

When signs are defined for a file, Vim will automatically add a column of two
characters to display them in. When the last sign is unplaced the column
disappears again.

Is it possible to remove the column whilst there are still signs defined?

Ideally I would like to toggle the column on / off.

vim


Solution 1:[1]

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

Solution 2:[2]

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

Solution 3:[3]

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 :)

Solution 4:[4]

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

Solution 5:[5]

Well, you distinguish between defined (describes how a certain sign looks like) and placed signs (which are actually shown in the sign column).

Unfortunately there is no way to toggle the sign column without removing all placed signs first. Thus you'd have to use a list/dict to keep the IDs/line numbers of signs.

(Shameless plug: https://github.com/mhinz/vim-signify)

Solution 6:[6]

vimscript one-liner (vim/neovim):

exe "set signcolumn=" .. (&signcolumn == "yes" ? "no" : "yes")

lua one-liner (neovim):

vim.o.signcolumn = vim.o.signcolumn == "yes" and "no" or "yes"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3
Solution 4 Ben
Solution 5
Solution 6