Make { and } ignore lines containing only whitespace

前端 未结 5 1776
一生所求
一生所求 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:56

    Here's a modified version that handles counts correctly:

    function! ParagraphMove(delta, visual, count)
        normal m'
        normal |
        if a:visual
            normal gv
        endif
    
        if a:count == 0
            let limit = 1
        else
            let limit = a:count
        endif
    
        let i = 0
        while i < limit
            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
            let i += 1
        endwhile
        normal |
    endfunction
    
    nnoremap  } :call ParagraphMove( 1, 0, v:count)
    onoremap  } :call ParagraphMove( 1, 0, v:count)
    " vnoremap  } :call ParagraphMove( 1, 1)
    nnoremap  { :call ParagraphMove(-1, 0, v:count)
    onoremap  { :call ParagraphMove(-1, 0, v:count)
    " vnoremap  { :call ParagraphMove(-1, 1)
    

提交回复
热议问题