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

3

38

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?

Meteoritics answered 16/6, 2018 at 13:47 Comment(2)
Are you using a Vim extension by any chance? Or is this happening in regular VSCode?Chunk
I'm using visual studio code vim extension.Meteoritics
C
95

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.

Chunk answered 17/6, 2018 at 0:49 Comment(7)
same problem happened to me. Automatically my cursor up and down when I following upper way.Meteoritics
Did this solve your problem, or are you saying it's still not working? I'm not sure I understand your comment.Chunk
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'.Meteoritics
I see. Hopefully they will add a proper fix soon, but it might be a while.Chunk
Only works with "J" "K", folds still open with using <C-d>, and other movements.Prothesis
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?Luminiferous
It works perfectly.Keener
H
8

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.

Heliport answered 28/12, 2022 at 3:33 Comment(1)
Really helpful answer. Also thanks for linking to other issue for more contextLevins
O
0

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

Oryx answered 24/5 at 12:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.