1

When using { and } to skip to the next paragraph, the behavior is unpredictable since I can't see white spaces. I have tried to show line endings to see which lines have spaces, but I don't like the cluttered look. I would like { and } to treat lines with white spaces as empty lines. How can I achieve this?

6
  • Maybe you could create a mapping that searches for the next blank line: nnoremap <LEADER>} /^\s*&/<CR>
    – mattb
    Commented Mar 21, 2022 at 11:58
  • I think I understand how this is supposed to work, but it doesn't. I'm "new" to Vim, can I just type this into command mode character by character, or do I have to do some ctrl+v magic or similar? Also, is the & supposed to be a $?
    – nixlarfs
    Commented Mar 21, 2022 at 12:17
  • @nixlarfs yes, you ought to be able to type :nnoremap <leader>} /^\s*$/<cr> and press enter (or put the same line in a vimrc or other config file), and then type \} (unless you have changed mapleader, see :help <Leader>). You might however instead prefer to eliminate such trailing spaces: :%substitute/\s*$//
    – D. Ben Knoble
    Commented Mar 21, 2022 at 12:38
  • Now it kind of works, but this <leader> thing doesn't seem to work in visual mode. Also, I'd prefer not to have to use <leader> if that's possible. I'm divided on the elimination of white spaces in case I share code with people, but maybe that's the best solution.
    – nixlarfs
    Commented Mar 21, 2022 at 12:52
  • For visual mode, add a xnoremap!
    – Biggybi
    Commented Mar 21, 2022 at 22:06

1 Answer 1

2

As per the comments, you can search for the next blank line with /^\s*$. However, this will clobber the search register (@/) and interfere with the search history. It will also highlight all blank lines if the 'hlsearch' setting is on.

I'd recommend something similar, search(). Like /, it searches for a pattern, but without the quirks mentioned above. Specifically,

  • search('^\s*$', 'W') will search forward for the first blank line (without wrapping around the end of the buffer), and
  • search('^\s*$', 'bW') will search backwards.

Finally, override the default behavior of {,} with the mappings

nnoremap } <cmd>call search('^\s*$', 'W')<cr>
nnoremap { <cmd>call search('^\s*$', 'bW')<cr>

For visual mode, use xnoremap in place of nnoremap.

EDIT: Note that the use of <cmd>...<cr> requires Vim 8.2 (specifically, 8.2.2026) or Neovim.

5
  • Typing nnoremap } :call search('^\s*$', 'W')<cr> in command mode seems to do just the thing, but I can't figure out what to put in the .vimrc. Also, if I do xnoremap } :call search('^\s*$', 'W')<cr>, I'm kicked out of visual mode when I type }.
    – nixlarfs
    Commented Mar 21, 2022 at 15:04
  • You should be able to put this anywhere in your vimrc.
    – Biggybi
    Commented Mar 21, 2022 at 22:10
  • @Biggybi I copied these lines exactly but it didn't work. Swapping <cmd> for : works in command mode, but having to input this for every session doesn't beat the auto trim trailing white spaces solution.
    – nixlarfs
    Commented Mar 22, 2022 at 8:54
  • 1
    It works as expected for me (both in normal and visual mode). <cmd> was introduced in a fairly recent version though, so you might need to update your vim. Else, use : as you have, and try to add gv for visual maps (to go back to visual selection). Put these lines in your vimrc, they will be read when vim starts and you won't need to input them anymore.
    – Biggybi
    Commented Mar 22, 2022 at 12:04
  • 2
    Upgrading from 8.1 to 8.2 solved both the problem with putting the lines in the .vimrc, and the problem of being kicked out of visual mode with xnoremap.
    – nixlarfs
    Commented Mar 28, 2022 at 11:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.