I like to fold code in the VS Code editor, but even when I fold some code blocks, they are opened when I move over them with my cursor using j / k
using the VS Code Vim extension. How can I protect my code folds from automatically opening like this?
Asked
Active
Viewed 1.0k times
27

Mihai Chelaru
- 7,614
- 14
- 45
- 51

JongHyeon Yeo
- 879
- 1
- 10
- 18
-
1Are you using a [Vim extension by any chance](https://github.com/VSCodeVim/Vim/issues/1004)? Or is this happening in regular VSCode? – Mihai Chelaru Jun 16 '18 at 14:06
-
I'm using visual studio code vim extension. – JongHyeon Yeo Jun 17 '18 at 00:34
2 Answers
75
It looks like this is an issue many people have had for a while, and the solution is to do the following (original source):
- Open up your user settings. On windows the shortcut is CTRL + ,
- Search for
vim.foldfix
and check the checkbox so the setting is set totrue
.
Alternatively, open your settings.json
file by opening the command palette (CTRL + SHIFT + P), select Preferences: Open Settings (JSON), then add the following line: "vim.foldfix": true
Now the folds should no longer automatically expand when you scroll past them with j or k.
Be aware that this is a hack because of various problems with VS Code itself that make fixing this difficult.

Mihai Chelaru
- 7,614
- 14
- 45
- 51
-
same problem happened to me. Automatically my cursor up and down when I following upper way. – JongHyeon Yeo Jun 17 '18 at 01:10
-
Did this solve your problem, or are you saying it's still not working? I'm not sure I understand your comment. – Mihai Chelaru Jun 17 '18 at 01:18
-
It had been worked in some manner, but the side effect generated. The side effect is overwhelming so I just toggled the option to false. My original problem was folded codes are automatically opened when I cursor crossing down to these block, But when I toggled the option as true, my cursor cannot work properly when I push 'J' or 'K'. – JongHyeon Yeo Jun 17 '18 at 01:41
-
I see. Hopefully they will add a proper fix soon, but it might be a while. – Mihai Chelaru Jun 17 '18 at 01:51
-
9Only works with "J" "K", folds still open with using
, and other movements. – Logan Sep 09 '20 at 02:45 -
Is one of the side effects the ctrl-d, ctrl-u, ctrl-f, ctrl-b will not work if the cursor were to land in one of the collapsed folds? – BrainPermafrost Nov 28 '21 at 02:39
-
6
For people who want to prevent this without implementing the hacking way suggested by the best answer, you can simply use gj
& gk
to navigate.
Furthermore, if gj
& gk
is not convenient for you, a little mapping might help.

louielyl
- 745
- 7
- 9
-
Really helpful answer. Also thanks for linking to other issue for more context – RobStallion Feb 22 '23 at 16:56