Reputation: 17405
What is the keyboard shortcut to navigate back to the last cursor position in Visual Studio Code?
Upvotes: 1364
Views: 509883
Reputation: 1166
I am on macOS (Sequoia 15.1.1), M4 Macbook Pro and I was able to achieve the cursor movements (back and forth) using Command + Option + Left / Right
by default without making any changes to my system.
Upvotes: 1
Reputation: 10107
if you want you can also define your own keybindings for navigation
{
"command": "workbench.action.navigateForward",
"key": "cmd+alt+[ArrowRight]"
},
{
"command": "workbench.action.navigateBack",
"key": "cmd+alt+[ArrowLeft]"
},
Upvotes: 4
Reputation: 2570
To set your own keyboard shortcuts (even if that particular keyboard-combo is already taken) you can go to Code > Preferences > Keyboard Shortcuts
and set them up there.
Code > Preferences > Keyboard Shortcuts
Go Forward
and Go Back
and set them to whatever you like.eg. our main tool is Android Studio on a mac which uses cmd+[ and cmd+] for forward and backward). This is especially powerful with a cmd+click for drilling into functions. (so you can follow your code as if it was an Ariadne's thread in a Minotaur labyrinth. Does help! ( so you jump around the code, diving in and out of functions, and back between files.
Upvotes: 27
Reputation: 5719
I am on Mac OS X, so I can't answer for Windows users:
I added a custom keymap entry and set it to Ctrl + ← + Ctrl + →, while the original default is Ctrl + - and Ctrl + Shift + - (which translates to Ctrl + ß and Ctrl + Shift+ß on my German keyboard).
One can simply modify it in the user keymap settings:
{ "key": "ctrl+left", "command": "workbench.action.navigateBack" },
{ "key": "ctrl+right", "command": "workbench.action.navigateForward" }
For the accepted answer I actually wonder. Alt + ← / Alt + → jumps wordwise for me (which is kind of standard in all editors). Did they really do this mapping for the Windows version?
Upvotes: 147
Reputation: 24438
The keyboard shortcut commands are Go Forward and Go Back.
On Windows:
Alt + ← ... navigate back
Alt + → ... navigate forward
On Mac:
Ctrl + - ... navigate back
Ctrl + Shift + - ... navigate forward
On Ubuntu Linux:
Ctrl + Alt + - ... navigate back
Ctrl + Shift + - ... navigate forward
Upvotes: 2400
Reputation: 995
Ctrl+k Ctrl+q
This was mentioned in sudormfbin's comment but not listed as an answer. This is what I want to do 99% of the time when I think "where was I now?". It is the easiest quickest way to get back to your last position without taking your fingers off keyboard to use the arrow keys and without installing extensions, etc.
ALT+ ← Back
ALT+→ Forward
Most of the other posted solutions refer to this - moving between "Navigation Locations", i.e., navigating between editor tabs OR within an editor (e.g., you move the cursor on one line, then click on a line 20 spaces above).
For navigating between editor tabs, instead of the above you can do Ctrl+Tab as it will sort all of the tabs in most recent order so you have option to go to others or just release and go back to the last used one. Also without taking fingers off keyboard.
Upvotes: 7
Reputation: 2183
To answer for your question, for:
You can find the current key-bindings in "Keyboard Shortcuts editor."
You can even edit the key-binding as per your preference.
Upvotes: 34
Reputation: 182511
vscode v1.65 is adding a number of new commands for navigating to the last/next edit location or the last/next cursor location.
An edit location
means there was an actual edit, whereas a cursor location
means that you visited that location but may not necessarily have made an edit there.
By default, editor navigation locations are added whenever you navigate across editors but also when navigating within editors (for example, when switching notebook cells or changing selection in text editors). If you feel that too many locations are being recorded, new commands have been added that reduce locations to either:
- Navigation locations (for example when using Go to Definition)
- Edit locations (whenever an editor is changed, for example when typing in a text editor)
Go Forward in Navigation Locations
"workbench.action.navigateForwardInNavigationLocations"
Go Back in Navigation Locations
"workbench.action.navigateBackInNavigationLocations"
Go to Last Navigation Location
// acts like a toggle between current and last cursor location
"workbench.action.navigateToLastNavigationLocation"
Go Previous in Navigation Locations
"workbench.action.navigatePreviousInNavigationLocations"
Go Forward in Edit Locations
"workbench.action.navigateForwardInEditLocations"
Go Back in Edit Locations
"workbench.action.navigateBackInEditLocations"
Go Previous in Edit Locations
"workbench.action.navigatePreviousInEditLocations"
Go to Last Edit Location
// acts like a toggle between current and last edit location
"workbench.action.navigateToLastEditLocation"
Associated context keys have been added to make assigning keybindings more powerful:
canNavigateBackInNavigationLocations
: Whether it is possible to go back in navigation locationscanNavigateForwardInNavigationLocations
: Whether it is possible to go forward in navigation locationscanNavigateToLastNavigationLocation
: Whether it is possible to go to the last navigation locationcanNavigateBackInEditLocations
: Whether it is possible to go back in edit locationscanNavigateForwardInEditLocations
: Whether it is possible to go forward in edit locationscanNavigateToLastEditLocation
: Whether it is possible to go to the last edit location
Upvotes: 13
Reputation: 3639
There are several ways to go back and options to go back in vs code.
To change any of the following things, go to command pannel by pressing ctrl
+ p
and then after typing a >
, type what config you need to change:
Then all you need to do is click on the Settings icon (as pointed by 2 in the image) and change the key binding to whatever you want.
Different options you have for going back:
(meaning: go to last cursor location)
(Opposite of above)
Upvotes: 2
Reputation: 1643
There is an extension available named Code-Navigation for all the platforms. As an alternative to the keyboard shortcuts it adds buttons to the left side of the status bar.
Upvotes: 25
Reputation: 12879
While the accepted answer is correct, anyone using the Vim extension for VS Code may be frustrated by the fact that navigating backwards takes you through every single cursor movement you've made. This prevents "Go back" from working well for use cases like:
With the Vim extension installed, you'll be stuck doing "Go Back" for every cursor movement you've made (and for me, that's enough to make it almost useless!).
Fortunately, the Vim extension implements jumplists well, so after using VS Code to jump to a new position (i.e., "Go to definition"), you can simply do CTRL+O to go back to the previous jump location.
Upvotes: 5
Reputation: 4101
This will be different for each OS, based on the information in Key Bindings for Visual Studio Code.
Go Back: workbench.action.navigateBack
Go Forward: workbench.action.navigateForward
Go Back: Ctrl+Alt+-
Go Forward: Ctrl+Shift+-
Go Back: Ctrl + -
Go Forward: Ctrl + Shift (⇧) + -
Go Back: Alt + ⬅️
Go Forward: Alt + ➡️
Upvotes: 121
Reputation: 1639
You can go to menu File → Preferences → Keyboard Shortcuts. Once you are there, you can search for navigate. Then, you will see all shortcuts set for your Visual Studio Code environment related to navigation. In my case, it was only Alt + - to get my cursor back.
Upvotes: 6
Reputation: 1328022
With Visual Studio Code 1.43 (Q1 2020), those Alt+← / Alt+→, or Ctrl+- / Ctrl+Shift+- will also... preserve selection.
See issue 89699:
Benjamin Pasero (bpasero
) adds:
going back/forward restores selections as they were.
Note that in order to get a history entry there needs to be at least 10 lines between the positions to consider the entry as new entry.
Upvotes: 3
Reputation: 119
As an alternative to the keyboard shortcuts, there is an extension named "Back and Forward buttons" that adds the forward and back buttons to the status bar.
Upvotes: 8
Reputation: 119
Mac OS (MacBook Pro):
Back: Ctrl(control) + - (hyphen)
Back forward: Ctrl + Shift + - (hyphen)
Upvotes: 10
Reputation: 702
For macOS:
⌘ + U: Undo the last cursor operation
You can also try Ctrl + -.
BTW, all the shortcuts are in Keyboard shortcuts for macOS. This is really useful!
Upvotes: 17
Reputation: 2601
Use Alt + ← / →
You can find all shortcuts in Key Bindings for Visual Studio Code.
Upvotes: 9