Make { and } ignore lines containing only whitespace

前端 未结 5 1693
一生所求
一生所求 2021-02-05 09:07

When you navigate by paragraph in vim using { and } it skips lines that contain nothing but whitespace though they are otherwise \'blank\'.

How can I convince vim to tre

5条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-05 09:55

    This is something that's bothered me for a long time. Probably the "right" solution would be to submit a patch to vim itself that would allow you to customize paragraph boundaries with a regex (like :set paragraphs, but actually useful).

    In the meantime, I've made a function and a couple of mappings that almost do the right thing:

    function! ParagraphMove(delta, visual)
        normal m'
        normal |
        if a:visual
            normal gv
        endif
    
        if a:delta > 0
            " first whitespace-only line following a non-whitespace character
            let pos1 = search("\\S", "W")
            let pos2 = search("^\\s*$", "W")
            if pos1 == 0 || pos2 == 0
                let pos = search("\\%$", "W")
            endif
        elseif a:delta < 0
            " first whitespace-only line preceding a non-whitespace character
            let pos1 = search("\\S", "bW")
            let pos2 = search("^\\s*$", "bW")
            if pos1 == 0 || pos2 == 0
                let pos = search("\\%^", "bW")
            endif
        endif
        normal |
    endfunction
    
    nnoremap  } :call ParagraphMove( 1, 0)
    onoremap  } :call ParagraphMove( 1, 0)
    " vnoremap  } :call ParagraphMove( 1, 1)
    nnoremap  { :call ParagraphMove(-1, 0)
    onoremap  { :call ParagraphMove(-1, 0)
    " vnoremap  { :call ParagraphMove(-1, 1)
    

    This doesn't correctly handle counts like '4}' or visual mode correctly (uncomment the vnoremap lines at your peril), but seems ok for things like not clobbering the current search pattern and not flickering. Also, 'd}', 'y}', etc. seem to work ok. If anyone has ideas for making counts work or fixing visual mode, please let me know.

提交回复
热议问题