Search code examples
vimfolding

Prevent x from deleting fold in Vim


I really like the zx key combination in Vim (it folds everything but the active area of text). However, it seems like quite a dangerous key combination. Pressing x on a fold deletes the fold. Thus, if z is omitted, or gets captured by some other preceding key combination, it becomes quite easy to accidentally delete the text in a fold by pressing x on its own.

Given that dd can also be used to delete the text in a fold, it would be good if I could disable x as a fold deletion tool.

  • How can x be disabled as a fold deletion key?

Solution

  • You can disable x on folds only with the following simple <expr> mapping:

    nnoremap <expr> x ((foldclosed('.')==-1)?('x'):('zx'))
    

    Unlike @Eelvex function, it keeps all x functionality and will also remap x being run on folds to zx.