Last active
March 13, 2021 13:42
-
-
Save kshenoy/00497c02590444fda6f5c85a43c3f8cb to your computer and use it in GitHub Desktop.
Paragraph Jumper to land on non-blank lines
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function! ParJump(dir, ...) | |
" Description: Paragraph jumping to land on non-blank lines | |
" Arguments: | |
" dir = 1 : Search forward for the last line of the current paragraph or first line of the next one | |
" 0 : Search backward for the first line of the current paragraph or last line of the next one | |
" a:1 : Output of visualmode() | |
" TODO: | |
" * Cursor doesn't stay in the same column in Visual mode | |
let l:curr_line = line('.') | |
let l:curr_col = col('.') | |
if (a:dir) | |
if ( (len(getline(l:curr_line)) == 0) | |
\ || (len(getline(l:curr_line + 1)) == 0) | |
\ ) | |
" Current or next line is blank | |
" ==> Find next non-blank line | |
let l:targ_line = nextnonblank(l:curr_line + 1) | |
else | |
" Neither the current nor the next line is blank i.e. we're in the middle of a paragraph | |
" ==> Jump to the last non-blank line | |
let l:targ_line = search('^$', 'nW') | |
let l:targ_line = (l:targ_line > 0 ? l:targ_line - 1 : line('$')) | |
endif | |
else | |
if ( (len(getline(l:curr_line)) == 0) | |
\ || (len(getline(l:curr_line - 1)) == 0) | |
\ ) | |
let l:targ_line = prevnonblank(l:curr_line - 1) | |
else | |
let l:targ_line = search('^$', 'nWb') | |
let l:targ_line = (l:targ_line > 0 ? l:targ_line + 1 : 1) | |
endif | |
endif | |
call setpos((a:0 ? "'>" : "."), [0, l:targ_line, l:curr_col]) | |
if a:0 | |
normal! gv | |
endif | |
endfunction | |
nnoremap <silent> { :call ParJump(0)<CR> | |
nnoremap <silent> } :call ParJump(1)<CR> | |
vnoremap <silent> { :call ParJump(0, visualmode())<CR> | |
vnoremap <silent> } :call ParJump(1, visualmode())<CR> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can you stick a MIT or other license on this, I'd love to stick it in my personal vimrc