Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent opening fold under the cursor when moving the cursor to left or right in Vim

Tags:

vim

fold

move

Whenever I move the cursor left or right using h or l, the fold under the cursor opens automatically. By the way, moving top or down does not have this problem. Is there any way to prevent automatically opening fold when moving horizontally?

like image 413
mhf Avatar asked Sep 20 '25 14:09

mhf


2 Answers

You can create the auto command:

autocmd! CursorMoved * if foldclosed('.') != -1 |
                     \   nnoremap h <nop>|
                     \   nnoremap l <nop>|
                     \ else |
                     \   silent! unmap h|
                     \   silent! unmap l|
                     \ endif

Here foldclosed('.') returns -1 if current line is unfolded. Instead of using this auto command just avoid pressing h or l over folds.

like image 50
builder-7000 Avatar answered Sep 23 '25 06:09

builder-7000


The default value of :help 'foldopen' has hor in it; this causes horizontal movements (like l, h) to open folds.

To disable this, simply add this to your ~/.vimrc:

set foldopen-=hor
like image 43
Ingo Karkat Avatar answered Sep 23 '25 06:09

Ingo Karkat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!