Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In vim, how can I map a key to toggle folding over the whole buffer?

I'd like to map a key to toggle between foldmethod=indent and no folding. How can I do that?

like image 611
static_rtti Avatar asked Dec 22 '22 16:12

static_rtti


1 Answers

I'd say zi (toggle foldenable) does the job. No mapping required. (see also :he folding)

(You could also look at zM and zR)


Since you want to map it to a single key, proceed as follows:

:nnoremap <F10> zi

To force the foldmode to indent each time (not really recommended for me), you'd need a function: Add the function to your vimrc[2]:

function ForceFoldmethodIndent()
    if &foldenable
        se foldmethod=indent
    endif
endfunction

nnoremap <F10> :normal zi^M|call ForceFoldmethodIndent()^M
inoremap <F10> ^O:normal zi^M|call ForceFoldmethodIndent()^M

Let me know if that works for you. I appreciate if you accept this answer if it does :)

Cheers

[1] with behave mswin [2] To enter the special keys (e.g. ^O) in commandline or insertmode use e.g.

  • Ctrl-VCtrl-O or
  • on windows[1] Ctrl-QCtrl-O
like image 78
sehe Avatar answered Jan 15 '23 09:01

sehe