Search code examples
vimlatexkeyboard-shortcutsvim-plugin

vimtex: force } and { not to skip empty lines


When I use } and {, vimtex+vim jumps somewhat randomly in the document, skipping several empty lines. See below.

How to restore the default vim behaviour not to skip the empty lines?

enter image description here


Solution

  • Short answer

    The fact that the skipping happens pretty randomly indicates that the empty lines are not really empty. They contain a whitespace or other special characters.

    Move the cursor to these 'empty' lines and press $ to see if they are really empty.


    How to avoid such problems:

    (Yes, others had your problem too.)

    Make vim show whitespace characters
    Vim has a way to show these characters. Use set list and define listchars. From the vim help for listchars:

        Strings to use in 'list' mode and for the :list command.  It is a
        comma separated list of string settings. 
        [...]
                                                        lcs-space
          space:c       Character to show for a space.  When omitted, spaces
                        are left blank.
                                                        lcs-trail
          trail:c       Character to show for trailing spaces.  When omitted,
                        trailing spaces are blank.  Overrides the "space"
                        setting for trailing spaces.
    

    See :help :list and :help listchars for more information.


    Highlight trailing whitespace
    I find it quite annoying to always have a character displayed for any space and my eyes are too bad to see a little dot at the end of a line (for trailing spaces). Therefore I use a highlight group to show all trailing whitespace characters:

    " Show trailing whitespace
        highlight ExtraWhitespace ctermbg=red       " define highlight group                      
        match ExtraWhitespace /\s\+$/               " define pattern
        autocmd BufWinEnter * match ExtraWhitespace /\s\+$/         " apply match to all buffers
        autocmd InsertEnter * match ExtraWhitespace /\s\+\%#\@<!$/  " don't match while in insert 
        autocmd InsertLeave * match ExtraWhitespace /\s\+$/
        autocmd BufWinLeave * call clearmatches()                   " It's good for the RAM
    

    Auto-remove trailing whitespace
    There is also a way to automagically delete those characters when writing the buffer -- but there are some caveats (think of markdown's double trailing whitespace for line breaks).

    Vim wiki has a good explanation.

    The simplest (but maybe not the best) solution is to add

     autocmd BufWritePre * %s/\s\+$//e
    

    to your .vimrc or to the corresponding ftplugin files. I personally have a function in my vimrc and disable it for file types, where I don't want/need it.


    You may also be interested in Make { and } ignore lines containing only whitespace


    Disclaimer
    There might be characters that are not whitespace, but are also not shown by vim by default. I never had this problem, but what I said under 'Short Answer' still does apply.