skip to Main Content

I want to save my current cursor position, move to some other line (in the same file), do some editing and jump back to the original cursor position to continue coding.

Lets say i am in line 50 and realise i need to add a header/library file at line 5. I want save my position here so that i can jump back after making my changes at line 5. How can i do this?

I have looked into cursor navigation shortcuts but they all move cursor by tracing every position cursor was in, and its time consuming and confusing, instead i want to jump back to saved position in one shot.

3

Answers


  1. Chosen as BEST ANSWER

    Thanks @scottfrazer for the suggesting selection anchor.

    Thanks @alefragnani for suggesting extensions.

    Based on these answers, i wrote a keybinding using multi command extension,

    After installing the multi command extension, Add the following snippets in the keybindings.json,

    [
        {
            "key": "ctrl+alt+`",
            "command": "extension.multiCommand.execute",
            "when": "!selectionAnchorSet",
            "args": { 
                "sequence": [
                    "editor.action.setSelectionAnchor",
                ]
            }
        },
        {
            "key": "ctrl+alt+`",
            "command": "extension.multiCommand.execute",
            "when": "selectionAnchorSet",
            "args": { 
                "sequence": [
                    "editor.action.goToSelectionAnchor",
                    "editor.action.cancelSelectionAnchor",
                ]
            }
        },
    ]
    

    Above snippet will create a key binding for "ctrl+alt+`" (Ctrl + Alt + BackTick).

    When pressed,

    1. Creates a selection anchor at cursor position
    2. If an anchor already exists, Move to that cursor position and deletes that anchor at that position.

  2. There is no built-in command for that. The Go Back / Go Forward navigation commands doesn’t allows you to save specific locations.

    Instead, you should rely on extensions, like my Bookmarks extensions.

    There is a bunch of other similar extensions, as you can see on this search https://marketplace.visualstudio.com/search?term=bookmarks&target=VSCode&category=All%20categories&sortBy=Relevance. You should take a look at some, and try out the one that better fit our needs.

    Hope this helps

    Login or Signup to reply.
  3. You could use the selection anchor. There is no default keybinding for "Go to Selection Anchor" so you’d have to add one yourself.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search