Skip to content

Instantly share code, notes, and snippets.

@tweekmonster
Created April 27, 2016 06:09
Show Gist options
  • Save tweekmonster/bf7a5c5eda0720da3c6f96defd919a4a to your computer and use it in GitHub Desktop.
Save tweekmonster/bf7a5c5eda0720da3c6f96defd919a4a to your computer and use it in GitHub Desktop.
Vim script to quickly edit the top portion of Python scripts using NrrwRgn
" A little annoying becuase NrrwRgn doesn't seem to be well suited for this.
" But, it gets the job done.
" Uses <leader>i in normal mode to open the split.
function! s:nrrw_head() abort
let saved = winsaveview()
keepjumps normal! 1G
let start = 1
let end = search('^\%(def\|class\)\s', 'ncW')
if !end
call winrestview(saved)
return
endif
let end = prevnonblank(end - 1)
let restore_options = 'let &l:wfw='.&l:wfw.' | let &l:wfh='.&l:wfh.' | let &l:ea='.&l:ea
let split_option = get(g:, 'nrrw_topbot_leftright', '')
let g:nrrw_topbot_leftright = 'aboveleft'
setlocal noequalalways winfixwidth winfixheight
execute start.','.end.'NarrowRegion'
wincmd p
let win = winnr()
call winrestview(saved)
wincmd p
execute 'autocmd! BufWinLeave <buffer> :'.win.' wincmd w | '.restore_options
if !empty(split_option)
let g:nrrw_topbot_leftright = split_option
else
unlet g:nrrw_topbot_leftright
endif
endfunction
autocmd FileType python nnoremap <buffer><silent> <leader>i :<c-u>call <sid>nrrw_head()<cr>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment