Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with cursor position using visual line movement (gj and gk) #1323

Open
kevinjalbert opened this issue Feb 22, 2017 · 19 comments · May be fixed by #5080
Open

Issue with cursor position using visual line movement (gj and gk) #1323

kevinjalbert opened this issue Feb 22, 2017 · 19 comments · May be fixed by #5080

Comments

@kevinjalbert
Copy link

Please thumbs-up 👍 this issue if it personally affects you! You can do this by clicking on the emoji-face on the top right of this post. Issues with more thumbs-up will be prioritized.


What did you do?

I've set the following as I like to work with word wrap enabled:

    "vim.otherModesKeyBindingsNonRecursive": [
        {
            "before": ["j"],
            "after": ["g", "j"]
        },
        {
            "before": ["k"],
            "after": ["g", "k"]
        }
    ]

I noticed a discrepancy in behaviour for lines without wrapping. As you navigate between lines of varying length the cursor position doesn't keep its horizontal position.

The following GIFs just show motion using j and k.

I would expect both GIFs to show the same thing regardless if the gj and gk mappings are applied.

What did you expect to happen?

Expected behaviour (the keybinding mappings not applied):
normal vim movement

What happened instead?

Current behaviour (the keybinding mappings applied):
normal vim movement

Technical details:

  • VSCode Version: 1.9.1
  • VsCodeVim Version: 0.5.3
  • OS: MacOS 10.12.3
@xconverge
Copy link
Member

copying comment from dupe ticket linked:

This is a tough one

I couldn't figure out a way to get the start and end column positions of the wrapped line.

I tried this:

const posLine = vimState.editor.selection.active.line;

await vscode.commands.executeCommand("cursorMove", {
	to: "wrappedLineStart"
});

const startCol = vimState.editor.selection.active.character;

await vscode.commands.executeCommand("cursorMove", {
	to: "wrappedLineEnd"
});

const endCol = vimState.editor.selection.active.character;
const lineLength = endCol - startCol;

vimState.cursorPosition =
	new Position(posLine, Math.min(lineLength + startCol, vimState.desiredColumn + startCol));

but moving the cursor 3 times for every 1 time we want to move it makes no sense from a performance standpoint.

I think that we need to get wrappedLine info via the vscode API but I could not find it currently...

Then we could skip using cursor move and do the same sort of moves we do as j and k, however we would use the wrappedLine start and end column instead of 0 and end of line

@xconverge
Copy link
Member

related microsoft/vscode#23045

@jessejanderson
Copy link

New user here having this issue. I had trouble following this issue since it was spread across a several of PRs/Issues so I might just be misunderstanding, but as far as I can tell it looks like it was supposed to have been fixed in May with #1552

I'm not intending to do anything with folding, but trying to get my j => gj/k => gk shortcuts working I've enabled "vim.foldfix": true which that PR suggested.

I'm still seeing the initial behavior where cursor position isn't retained (as seen in the gifs above).

Am I missing something?

@wegry
Copy link

wegry commented May 2, 2018

@jessejanderson "vim.foldfix": true makes my cursor skip (as in touch a wrapped line) and then continue with the previous erroneous behavior. I was hoping that was what I was missing.

@kevinjalbert
Copy link
Author

This still seems like an issue, If anyone has figured out a configuration which works I'm all 👂.

VSCode - 1.24.1
VSCodeVim - 0.13.0

@Chaoclypse
Copy link

I also have this issue. Would love to see a solution, or any form of workaround!

@hhu94
Copy link
Contributor

hhu94 commented Jul 19, 2018

I don't know if it's related, but when moving a number of lines using j or k as in 15j or 15k, the initial column position isn't remembered either.

@rightaway
Copy link

For me it's an issue when moving to/from a line that spans more than row.

@fabianschilling
Copy link

This remains an isse even with "vim.foldfix": true, VSCode 1.29.1, and VSCode Vim 0.16.14. Does someone have another workaround?

@Chaoclypse
Copy link

Still an issue for me, as well.

@xarthurx
Copy link

Still an issue.

@rightaway
Copy link

@J-Fields what vscode version will we see your fix in?

@J-Fields J-Fields reopened this Nov 16, 2019
@J-Fields
Copy link
Member

@rightaway Please see #4127

@J-Fields J-Fields changed the title Issue with cursor position using visual line movement (gj and gk) Issue with cursor position using visual line movement (gj and gk) Oct 15, 2021
@Adnios
Copy link

Adnios commented May 23, 2022

Still met this problem 😢

@hpurmann
Copy link

I think this has worked for a while but I can't get this to work anymore. @J-Fields did your fix somehow get removed?

@aurangzaib-danial
Copy link

aurangzaib-danial commented Nov 21, 2023

I was having this issue as well. I solved it by following the instructions given on #3623 for setting up keybindings in VSCode by opening keyboard shortcuts JSON file from the command palette. Just need to add following keybindings in the file:

{
  "key": "up",
  "command": "cursorUp",
  "when": "editorTextFocus && vim.active && !inDebugRepl && !suggestWidgetMultipleSuggestions && !suggestWidgetVisible"
},
{
  "key": "down",
  "command": "cursorDown",
  "when": "editorTextFocus && vim.active && !inDebugRepl && !suggestWidgetMultipleSuggestions && !suggestWidgetVisible"
},
{
  "key": "k",
  "command": "cursorUp",
  "when": "editorTextFocus && vim.active && !inDebugRepl && vim.mode == 'Normal' && !suggestWidgetMultipleSuggestions && !suggestWidgetVisible"
},
{
  "key": "j",
  "command": "cursorDown",
  "when": "editorTextFocus && vim.active && !inDebugRepl && vim.mode == 'Normal' && !suggestWidgetMultipleSuggestions && !suggestWidgetVisible"
}

This will allow moving between visual wrapped lines and also the cursor position for different lines will not reset.

Also, by following above instructions, you can remove following from your settings if you are using it as these don't work:

    "vim.otherModesKeyBindingsNonRecursive": [
        {
            "before": ["j"],
            "after": ["g", "j"]
        },
        {
            "before": ["k"],
            "after": ["g", "k"]
        }
    ]

I think by using this solution, this issue can be closed. Hope this helps!

@4volodin
Copy link

4volodin commented Dec 27, 2023

I was having this issue as well. I solved it by following the instructions given on #3623 for setting up keybindings in VSCode by opening keyboard shortcuts JSON file from the command palette. Just need to add following keybindings in the file:

{
  "key": "up",
  "command": "cursorUp",
  "when": "editorTextFocus && vim.active && !inDebugRepl && !suggestWidgetMultipleSuggestions && !suggestWidgetVisible"
},
{
  "key": "down",
  "command": "cursorDown",
  "when": "editorTextFocus && vim.active && !inDebugRepl && !suggestWidgetMultipleSuggestions && !suggestWidgetVisible"
},
{
  "key": "k",
  "command": "cursorUp",
  "when": "editorTextFocus && vim.active && !inDebugRepl && vim.mode == 'Normal' && !suggestWidgetMultipleSuggestions && !suggestWidgetVisible"
},
{
  "key": "j",
  "command": "cursorDown",
  "when": "editorTextFocus && vim.active && !inDebugRepl && vim.mode == 'Normal' && !suggestWidgetMultipleSuggestions && !suggestWidgetVisible"
}

This will allow moving between visual wrapped lines and also the cursor position for different lines will not reset.

Also, by following above instructions, you can remove following from your settings if you are using it as these don't work:

    "vim.otherModesKeyBindingsNonRecursive": [
        {
            "before": ["j"],
            "after": ["g", "j"]
        },
        {
            "before": ["k"],
            "after": ["g", "k"]
        }
    ]

I think by using this solution, this issue can be closed. Hope this helps!

it's buggy in visual mode
and
UPD:

    {
        "key": "k",
        "command": "cursorUp",
        "when": "editorTextFocus && vim.active && !inDebugRepl && vim.mode == 'Normal' && !suggestWidgetMultipleSuggestions && !suggestWidgetVisible"
    },
    {
        "key": "j",
        "command": "cursorDown",
        "when": "editorTextFocus && vim.active && !inDebugRepl && vim.mode == 'Normal' && !suggestWidgetMultipleSuggestions && !suggestWidgetVisible"
    },

it breaks 5j or 3k for example

@4volodin
Copy link

shame on you...
it hasn't been working for 8 years

@benjaminingreens
Copy link

benjaminingreens commented Nov 4, 2024

An issue for me too, but in standard terminal Vim - not in VSCode.

I map j to gj and k to gk when navigating my markdown files.

The column position is not preserved when going through a paragraph when using j (gj)

Funnily enough, this is not an issue when using k (gk), which seamelessly glides upwards through a paragraph while preserving the column


WORKAROUND FOUND: For anyone who is having this issue with terminal vim, I found a workaround

I had the standard mapping in my .vimrc like so:

autocmd FileType markdown nnoremap j gj
autocmd FileType markdown nnoremap k gk

After adding the following lines, the issue was fixed:

autocmd FileType markdown nnoremap <expr> k v:count == 0 ? 'gk' : 'k'
autocmd FileType markdown nnoremap <expr> j v:count == 0 ? 'gj' : 'j'

I was trying to fix a separate issue involving using line jumping, and the fix for that also seemed to fix the issue with preserving columns. Odd, but it works

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.