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?
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
.
1. Disable x
You can completely disable x
(or any other key/combination) by:
nmap x <nop>
(x
, by the way, is the same as dl
not dd
)
A closed fold is always included as a whole when using operators, thus (afaik) it is not possible to disable x
just for fold deletion.
2. Automatically open the fold
A possible workaround is to have the fold automatically open when you are "on" it, so x
would delete only one character (as normal):
set foldopen=all
but this makes it more cumbersome to navigate through the code.
3. Remap x
If you don't mind disabling some of x
's functionality (eg delete into register), this will also do the job:
function Foldx()
if foldclosed(".") == line(".")
echo "Watch it!"
else
call feedkeys("dl")
echo "x"
endif
endfunction
nmap x :call Foldx()<cr>
精彩评论