I'm looking for a way to remap a key to move the cursor down to the next line, skipping any lines that only contain \n and a way to do the same only going up to the next line.
Essentially, I want to do the opposite of the { and } motions.
On
I'm not sure what you want to map these two, so I'll just use { and }. How about this?
nnoremap } /^\S<cr>
nnoremap { ?^\S<cr>
The explanation is pretty straightforward.
/ " Search forward
^ " For the start of a line
\S " Followed by a non-whitespace character
<cr> " Enter
The ? mapping is the same except for searching backwards instead of forwards.
Of course for completeness, you'll want to add
nnoremap } /^\S<cr>
xnoremap } /^\S<cr>
onoremap } /^\S<cr>
nnoremap { ?^\S<cr>
xnoremap { ?^\S<cr>
onoremap { ?^\S<cr>
This will make it work as an argument to an operator (e.g. d{) and in visual mode.
Here are alternatives to DJ's mappings that play well with
hlsearch:jump to next non-empty line
jump to previous non-empty line
extend visual selection to next non-empty line
extend visual selection to previous non-empty line
operate to next non-empty line
operate to previous non-empty line
Explanation…
With
hlsearchenabled,/anythingwill highlight every match. Since we are not actively searching for non-empty lines but merely moving to them, the resulting highlighting is pointlessly noisy.By using
:help search(), we bypasshlsearchand thus make the mappings a lot less noisy.<C-u>is used to remove any accidental range before calling our function.The visual mode mappings work like this:
:help :k,:help :normal,:help i_ctrl-r,:help "=, and:help visualmode(),:help '',:help v_o.The operator pending mappings simply reuse the visual mode mappings.