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.
Here are alternatives to DJ's mappings that play well with hlsearch
:
jump to next non-empty line
nnoremap <key> :<C-u>call search('^.\+')<CR>
jump to previous non-empty line
nnoremap <otherkey> :<C-u>call search('^.\+', 'b')<CR>
extend visual selection to next non-empty line
xnoremap <key> :<C-u>k`\|call search('^.\+')\|normal! <C-r>=visualmode()<CR>``o<CR>
extend visual selection to previous non-empty line
xnoremap <otherkey> :<C-u>k`\|call search('^.\+', 'b')\|normal! <C-r>=visualmode()<CR>``o<CR>
operate to next non-empty line
omap <key> :<C-u>normal! v<key><CR>
operate to previous non-empty line
omap <otherkey> :<C-u>normal! v<otherkey><CR>
Explanation…
With hlsearch
enabled, /anything
will 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 bypass hlsearch
and 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.