I wrote the following script that allows you to close a fold while "moving" left when you are at the beginning of a line and to open a closed fold by trying to move right when on the fold line.
When trying to move right opens the fold, the cursor does not actually move but stays on the same column.
That way, one can move up and down in the leftmost column and use "moves" right/left to quickly open/close folds which makes browsing folded code efficient!
(Disclaimer: not tested on documents using hard tabs at the start of lines)
function! MoveAndFoldLeft()
let line = getpos('.')[1]
let col = getpos('.')[2]
if l:col ==# 1 && foldlevel(l:line)
execute "foldclose"
else
execute "normal! h"
endif
endfunction
function! MoveAndFoldRight()
let line = getpos('.')[1]
if foldlevel(line) && foldclosed(line) != -1
execute "foldopen"
else
execute "normal! l"
endif
endfunction
nnoremap <silent> <Left> :call MoveAndFoldLeft()<cr>
nnoremap <silent> h :call MoveAndFoldLeft()<cr>
nnoremap <silent> <Right> :call MoveAndFoldRight()<cr>
nnoremap <silent> l :call MoveAndFoldRight()<cr>
h
to try to fold current section if cursor is at line start. But, IMHO, it is a little inconvenient to do so, because you have to have (or move) the cursor there in the first place. While the default key maps (zc
, orzC
) don't have that shortcoming.