Vim: Remap key to go to the next non-empty line (and vice versa)

I am looking for a way to reassign a key to move the cursor to the next line, skipping any lines that contain only \nand a way to do the same, only to the next line.

Essentially, I want to do the opposite of {and} the movements.

+4
source share
2 answers

The following are alternatives to DJ mappings that go well with hlsearch:

  • go to the next non-empty line

    nnoremap <key> :<C-u>call search('^.\+')<CR>
    
  • go to previous non-empty line

    nnoremap <otherkey> :<C-u>call search('^.\+', 'b')<CR>
    
  • expand visual selection to the next non-empty line

    xnoremap <key> :<C-u>k`\|call search('^.\+')\|normal! <C-r>=visualmode()<CR>``o<CR>
    
  • expand visual selection to previous nonempty row

    xnoremap <otherkey> :<C-u>k`\|call search('^.\+', 'b')\|normal! <C-r>=visualmode()<CR>``o<CR>
    
  • act on the next non-empty line

    omap <key> :<C-u>normal! v<key><CR>
    
  • omap <otherkey> :<C-u>normal! v<otherkey><CR>
    

...

hlsearch, /anything . , , .

:help search(), hlsearch , , .

<C-u> .

:

  • " " :help :k,
  • ,
  • :help :normal,
  • :help i_ctrl-r, :help "= :help visualmode(),
  • " " :help '',
  • , , :help v_o.

, , .

+4

, , { }. ?

nnoremap } /^\S<cr>
nnoremap { ?^\S<cr>

.

/           " Search forward
 ^          " For the start of a line
  \S        " Followed by a non-whitespace character
    <cr>    " Enter

? , , .

,

nnoremap } /^\S<cr>
xnoremap } /^\S<cr>
onoremap } /^\S<cr>
nnoremap { ?^\S<cr>
xnoremap { ?^\S<cr>
onoremap { ?^\S<cr>

(, d{) .

+1

Source: https://habr.com/ru/post/1660241/


All Articles