Skip to content

Instantly share code, notes, and snippets.

@romainl
Created September 23, 2017 13:50
Show Gist options
  • Save romainl/ac63e108c3d11084be62b3c04156c263 to your computer and use it in GitHub Desktop.
Save romainl/ac63e108c3d11084be62b3c04156c263 to your computer and use it in GitHub Desktop.
Markdown : jump to next heading
" markdown : jump to next heading
function! s:JumpToNextHeading(direction, count)
let col = col(".")
silent execute a:direction == "up" ? '?^#' : '/^#'
if a:count > 1
silent execute "normal! " . repeat("n", a:direction == "up" && col != 1 ? a:count : a:count - 1)
endif
silent execute "normal! " . col . "|"
unlet col
endfunction
nnoremap <buffer> <silent> ]] :<C-u>call <SID>JumpToNextHeading("down", v:count1)<CR>
nnoremap <buffer> <silent> [[ :<C-u>call <SID>JumpToNextHeading("up", v:count1)<CR>
@adigitoleo
Copy link

adigitoleo commented Feb 18, 2021

Pretty nice, you can avoid the repeat call by using the :global command :) (nope, I'm being silly, but it's a similar idea)

        silent execute a:direction ==? "up"
                    \ ? '?^#?normal! N' .. a:count .. 'n'
                    \ : '/^#/normal! N' .. a:count .. 'n'

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