Search code examples
vimsyntax-highlightingtext-editorvim-syntax-highlighting

Vim: toggle highlighting of long lines


In my .vimrc, I have:

:au BufWinEnter * let w:m1=matchadd('Search', '\%>80v.\+', -1)

to highlight lines that stray over the 80 character limit. How can I set it so that this is toggled on/off by pressing a function key?


Solution

  • Use mappings.

    To activate highlight:

    :nnoremap <leader>1 :match Search '\%>80v.\+'<CR>
    

    To deactivate it:

    :nnoremap <leader>2 :match none<CR>
    

    UPDATE to use same key/key combination to toggle highlight:

    let s:activatedh = 0 
    function! ToggleH()
        if s:activatedh == 0
            let s:activatedh = 1 
            match Search '\%>80v.\+'
        else
            let s:activatedh = 0 
            match none
        endif
    endfunction
    
    nnoremap <leader>1 :call ToggleH()<CR>