27

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?

Mihai Chelaru
  • 7,614
  • 14
  • 45
  • 51
JongHyeon Yeo
  • 879
  • 1
  • 10
  • 18

2 Answers2

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):

  1. Open up your user settings. On windows the shortcut is CTRL + ,
  2. Search for vim.foldfix and check the checkbox so the setting is set to true.

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
  • 9
    Only 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
  • It works perfectly. – ilkyu tony lee Dec 22 '21 at 06:54
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