Skip to content

Instantly share code, notes, and snippets.

@kshenoy
Last active March 13, 2021 13:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kshenoy/00497c02590444fda6f5c85a43c3f8cb to your computer and use it in GitHub Desktop.
Save kshenoy/00497c02590444fda6f5c85a43c3f8cb to your computer and use it in GitHub Desktop.
Paragraph Jumper to land on non-blank lines
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>
@gryftir
Copy link

gryftir commented Feb 18, 2017

Can you stick a MIT or other license on this, I'd love to stick it in my personal vimrc

@kshenoy
Copy link
Author

kshenoy commented Feb 22, 2017

Sure. Bundled it as a very simple plugin: vim-parjumper

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment