JongHyeon Yeo
JongHyeon Yeo

Reputation: 1009

How to prevent code-folds from automatically opening when cursor moves over them? (VSCode, vim mode)

I like to fold code in the VS Code editor, but 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?

Upvotes: 39

Views: 14508

Answers (3)

Ying n Yang
Ying n Yang

Reputation: 109

Note that the hacky way using vim.foldfix breaks whatever the name is for "column saving?"

In other words, if you hit a newline, the cursor will be reset to the start of the line for every future line. Instead of the desired default behavior of keeping the column position.

Use gj and gk like louielyl mentioned to avoid this issue

Upvotes: 0

louielyl
louielyl

Reputation: 1042

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.

Upvotes: 9

Mihai Chelaru
Mihai Chelaru

Reputation: 8187

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.

Upvotes: 102

Related Questions