See the foldopen parameter. It controls which groups of commands will lead to opening a crease if the cursor moves to a closed crease.
Please note that vertical movements do not open a closed crease. Moreover, the foldopen parameter does not exist for this. If hor set the hor element in the hor option to open the fold, you can use h , l or other horizontal movement commands. In the event that it is important to automatically open by resetting any cursor movement that touches it, you can approach this problem by reassigning a subset of the vertical movement commands, as shown below.
nnoremap <silent> j :<cu>call MoveUpDown('j', +1, 1)<cr> nnoremap <silent> k :<cu>call MoveUpDown('k', -1, 1)<cr> nnoremap <silent> gj :<cu>call MoveUpDown('gj', +1, 1)<cr> nnoremap <silent> gk :<cu>call MoveUpDown('gk', -1, 1)<cr> nnoremap <silent> <cd> :<cu>call MoveUpDown("\<lt>cd>", +1, '&l:scroll')<cr> nnoremap <silent> <cu> :<cu>call MoveUpDown("\<lt>cu>", -1, '&l:scroll')<cr> nnoremap <silent> <cf> :<cu>call MoveUpDown("\<lt>cf>", +1, 'winheight("%")')<cr> nnoremap <silent> <cb> :<cu>call MoveUpDown("\<lt>cb>", -1, 'winheight("%")')<cr> function! MoveUpDown(cmd, dir, ndef) let n = v:count == 0 ? eval(a:ndef) : v:count let l = line('.') + a:dir * n silent! execute l . 'foldopen!' execute 'norm! ' . n . a:cmd endfunction
A lower, but slightly more economical solution is to open the fold at each cursor movement.
autocmd CursorMoved,CursorMovedI * silent! foldopen
Unfortunately, this solution is not general. After folding under the cursor opens, the cursor is located on the first line of this fold. If this behavior is undesirable, you can follow the vertical direction of movement and place the cursor on the last line of the fold when the cursor moves from bottom to top.
autocmd CursorMoved,CursorMovedI * call OnCursorMove() function! OnCursorMove() let l = line('.') silent! foldopen if exists('b:last_line') && l < b:last_line norm! ]z endif let b:last_line = l endfunction
However, the fold does not open if the movement jumps over the crease. For example, 2j on the line just above the fold will place the cursor on the line immediately after this fold, and not the second line in it.