Vim: Solution for VSCodeVim code folding issue (solves #1004)

Created on 27 Jan 2020  路  8Comments  路  Source: VSCodeVim/Vim

Solves->

1004

Introduction->
First of all, thank you for this amazing plugin.
Without a vim plugin, I wouldn't be using vscode.

Thank you very much for your hard work.

Problem ->
vscode vim folding has been an issue for a lot of time and, AFAIK vscode devs havn't fixed the issue and it seems they don't plan to provide the required API to solve these issues.

This has been probably the most requested feature ( #1004 ) for VSCodeVim but due to the negligence of support from core vscode team, I was going to leave vscode behind, out of frustration.

Discussion ->
But I didn't want to leave vscode.

So I came up with a solution myself.

I don't know if it is the "best" solution. It works for me. And I plan to continue using it.

I'm sharing here in hope that if someone else is facing the same problem, they are able to solve the issue and enjoy using vscode.

I'm not noticing any performance degradation due to using this fix, but it's a dirty fix nonetheless.

Use->

  • Setup the macros as shown in solution section.
  • Use alt+j to move down.
  • Use alt+k to move up.

Solution ->
I used macros to setup alternative ways to move up and down which didn't have the code folding issue.

I downloaded this macro plugin : <<< LINK >>>

I included the following setting into settings.json

"macros": {
    "vimAltDown" : [
        "toggleVim",
        "cursorDown",
        "toggleVim"
    ],
    "vimAltUp" : [
        "toggleVim",
        "cursorUp",
        "toggleVim"
    ]
}

I added the following to keybindings.json

{
    "command": "macros.vimAltDown",
    "key": "alt+j"
},
{
    "command": "macros.vimAltUp",
    "key": "alt+k"
}

How it works ->
I used the macro library to setup proper macros that would toggle out of vim mode, move the cursor down or up and toggle vim on again.

When vim mode is toggled off, the cursor can move up and down without expanding the code folds.

Request->
If this is suitable for your needs, could you please include the solution in a more official way?

Thanks

Most helpful comment

That's exactly what I meant - the whole point of the macros is to do more than one command by calling just one custom command. A macro that calls only one command is pointless. Instead, you could do:

"vim.normalModeKeybindingsNonRecursive": [
    {
        "before": "j",
        "commands": "cursorDown"
    },
    {
        "before": "k",
        "commands": "cursorUp"
    }
]

All 8 comments

BUG

Using this method, the cursor goes through the code, but for some reason now, the cursor resets back to line 1 or 2 of the file.

Maybe someone who knows the system well could look into it?

Thanks.

BUG

Using this method, the cursor goes through the code, but for some reason now, the cursor resets back to line 1 or 2 of the file.

Maybe someone who knows the system well could look into it?

Thanks.

Found a temporary fix.

After using alt+j or alt+k to scroll through the code, press escape once.

Then go on using j and k to scroll through your code.

Pressing escape somehow ensures that the cursor remains at the desired position and doesn't jump to line 1 or 2 of the code.

UPDATE -> GOOD NEWS !

I went over the code and wasn't happy with the rapid vim toggles.
I removed them and kept the move cursor commands.

It is still working.

And this doesn't have the bug where the cursor jumps to line 2 or 1.

Now settings.json just needs :

"macros": {
    "vimAltDown" : [
        "cursorDown"
    ],
    "vimAltUp" : [
        "cursorUp"
    ]
}

Which means you don't need the macros!

Which means you don't need the macros!

No.
The macros are still required in this implementation.

What is different in this macro is that it seems to work without rapidly toggling vim on and off.

cursorUp and cursorDown are only used.

But I get where you are coming from. It might be possible to just remap the k and j keys to cursorUp and cursorDown commands.

That's exactly what I meant - the whole point of the macros is to do more than one command by calling just one custom command. A macro that calls only one command is pointless. Instead, you could do:

"vim.normalModeKeybindingsNonRecursive": [
    {
        "before": "j",
        "commands": "cursorDown"
    },
    {
        "before": "k",
        "commands": "cursorUp"
    }
]

That's exactly what I meant - the whole point of the macros is to do more than one command by calling just one custom command. A macro that calls only one command is pointless. Instead, you could do:

"vim.normalModeKeybindingsNonRecursive": [
    {
        "before": "j",
        "commands": "cursorDown"
    },
    {
        "before": "k",
        "commands": "cursorUp"
    }
]

Love this.
Did you see the preferred results on your side?
I will test if it works out on my end.

Yes. I have been using it for a while. It also solves the lag that I had been experiencing. The one caveat is that it breaks counts like 10j for j and k. I don't use counts for j and k though, so it worked for me.

Was this page helpful?
0 / 5 - 0 ratings