Are there bookmarks in Visual Studio Code? - visual-studio-code

How can I set bookmarks in Visual Studio Code? I can't find any keyboard shortcuts.
Or is there anything else that I can use instead?

Yes, via extensions. Try Bookmarks extension on marketplace.visualstudio.com
Hit Ctrl+Shift+P and type the install extensions and press enter, then type Bookmark and press enter.
Next you may wish to customize what keys are used to make a bookmark and move to it. For that see this question.

You need to do this via an extension as of the version 1.8.1.
Go to View → Extensions. This will open Extensions Panel.
Type bookmark to list all related extensions.
Install
I personally like "Numbered Bookmarks" - it is pretty simple and powerful.
Go to the line you need to create a bookmark.
Click Ctrl + Shift + [some number]
Ex: Ctrl + Shift + 2
Now you can jump to this line from anywhere by pressing Ctrl + number
Ex: Ctrl + 2

Visual Studio Code currently does not support bookmarks natively. Please add this as feature request to our Git Hub issue list (https://github.com/Microsoft/vscode).
In the meantime there are some ways to navigate around the code based on your navigation history. You can Ctrl+Tab to quickly jump to previously opened files. You can also navigate within your code based on cursor positions using Goto | Back and Goto | Forward.

Both VS Code extensions can be used:
'Bookmarks'
'Numbered Bookmarks'
Personally, I'm suggesting:
Numbered Bookmarks, with 'navigate through all files' option:
ctrl + Shift + P in VS Code
In newly open field, type: Open User Settings
Paste this key/value: "numberedBookmarks.navigateThroughAllFiles": "allowDuplicates" (allow duplicates of bookmarks),
Or, paste this key/value: "numberedBookmarks.navigateThroughAllFiles": "replace"
NOTE
Either way, be careful with shortcuts (Ctrl+1, Ctrl+Shift+1,..) that are already assigned.
Personally, mine were in 2 conflicts, with:
VS Code shortcuts, that already exists,
Ditto clipboard (I've got paste on each call of bookmark)

The bookmarks extension mentioned in the accepted answer conflicts with toggling breakpoints via the margin.
You could do the same with breakpoints and select the debug tab on the left to see them listed. Better yet, use File, Preferences, Keyboard Shortcuts and set (Shift+)Ctrl+F9 to navigate between them, even across files:

If you are using vscodevim extension, then you can harness the power of vim keyboard moves. When you are on a line that you would like to bookmark, in normal mode, you can type:
m {a-z A-Z} for a possible 52 bookmarks within a file. Small letter alphabets are for bookmarks within a single file. Capital letters preserve their marks across files.
To navigate to a bookmark from within any file, you then need to hit ' {a-z A-Z}. I don't think these bookmarks stay across different VSCode sessions though.
More vim shortcuts here.

No extension
As an alternative you can do Ctrl + P as "Go to file" in your workspace and type:
partial name of your file and/or extension.
type # + any name of method, property, variable, class (symbol) etc.
in currently opened file navigate between symbols with #
I found it more convenient than Bookmarks extension. Of course it depends a lot on your naming conventions and how well you know your codebase.

Under the general heading of 'editors always forget to document getting out…' to toggle go to another line and press the combination ctrl+shift+'N' to erase the current bookmark do the same on marked line…

Related

How to get keyboard shortcut `Cmd + S` to save on VS Code?

When I attempt to save my file on Visual Studio Code with the cmd+s keyboard shortcut, it does not save the code. I have to manually click File, the Save, just to be able to save my progress. I'm using macOS Catalina version 10.15.7 and VC Code version 1.55.2.
How do I fix this?
Attempts:
-Checked if ⌘ Cmd + S keyboard shortcut is tied to the Save functionality
If you do it right this solution will fix your issue.
I have never had a problem saving with VS-Code personally, but I have had issues with keybindings. IDK if you write your own keybindings, but if you do, you might want to check the keybindings that you have wrote to make sure they do not conflict with [CTRL + S]. The keybindings.json file that you create custom keybindings in, overrides the default keybindings.json file that defines the keybindings that VS-Code ships with.
To check your keybindings.json file...
Hit the F1-Key
A menu will drop open type in "Keyboard Shortcuts"
There will be two Preferences: Keyboard Shortcuts
Make sure to select Preferences: Keyboard Shortcuts and not Preferences: Default Keyboard Shortcuts
If the file is empty you are good. If you have keybindings written in the file, you need to iterate through them by hand, checking each one. Make sure that none use [CTRL + S] together. Even if the keybinding uses [CTRL + S] and other keys, you will need to disable it, so you can test if it is causing an issue.
Debugging [CTRL + S]
If your keybindings.json file is all good, then great, that's one thing to scratch off the list. The only thing left to do now is debug the Bound Key ("Key Binding"). To debug keybindings, you will use a built in tool, that VS-Code offers. To start do the following:
Hit the F1-Key
When the quick input drops open type the following into the text input:
"Toggle Keyboard Shortcuts Troubleshooting"
Select the option: Developer: Toggle Keyboard Shortcuts Troubleshooting
It should automatically open the OUTPUT panel, which is located in the same panel that your terminal is. Make sure that the OUTPUT is set to LOG(Window) in the drop down. (I took a picture and posted it below if you can't find the Keyboard Shortcut Troubleshooter).
The image might have funny declensions because I am on a dual monitor setup with 1 1080x1920 curved screen and one 1080x720 screen.... I cropped it to a STD HD 1920 width.
I Got My Trouble-shooter working, and Output open, now What Jay?
Okay... Well your at the right spot. Now every-time you press some keys, you should see your OUTPUT WINDOW working like crazy. It should be logging all sorts of stuff, which is good, very good.
This Part Is Important! READ CAREFULLY
What you want to do is use your keybinding that you feel isn't working appropriately. Use it when focus is set on an editor, use it when focus is set on a different editor, use it when focus is set on the sidebar. Where you are focused at in the editor at any given time can greatly affect a keybinding. The output is gonna write lines every-time you use your keybinding. Try not to hit any other keys while doing this, so you have a column in you output that includes logging from the keybinding you are testing only. Read the output see what it says. See if it looks right, or wrong. If it looks wrong, you can visit this link, to the VSCode site that covers this topic, and see if you can fix it your self. If you can't fix it your self, come back here, and edit your question. When you edit your question make sure that it includes the Troubleshooter's Logging Output.
VSCode Troubleshooting Keybindings (Keyboard Shortcuts) # https://code.visualstudio.com/docs/getstarted/keybindings#_troubleshooting-keybindings
Image that shows how to open the Keyboard Debugger
Also shows how to set the OUTPUT to Log(Window)
StackOverflow-2021-JUNE-26018:34-PST
Another thing to check for is, for lack of a better term, combination key bindings. For example, I was having an issue with ⌘+s. VSCode gave a message that it was waiting for the second key binding. After looking at the keybindings.json, I noticed I had a key binding for opening up user snippets as
{
"key": "cmd+s cmd+n",
"command": "workbench.action.openSnippets"
}
The ⌘+s portion of this key binding conflicted with the default ⌘+s

How to open 'Go to definition' in other split tab for same file in VS Code?

I'm considerably new to Visual Studio Code and I am struggling a bit with an easy functionality. Imagine the case when I am reviewing some code in a file a.py and I select 'Go to definition' on a function x of the same file. Is there any possible configuration or shortcut that opens the definition on a split on the editor?
I have tried having already the same file split in the editor an it still opens the definition in the same file what is uncomfortable when you have to trace some code because you need to scroll up and down whenever you want to see another function of the same file.
An extension that allows to do the trick will also be well received. Thanks.
There are 2 ways to achieve this.
Either:
Left-click function name that you want to open.
Ctrl + t
Ctrl + enter
Or:
Change setting Editor > Goto Location: Multiple to be gotoAndPeek or goto
Ctrl + Alt + [click on function name]
Both methods will open the definition in the split to the right (or create a new split if this is the right-most split).
You like?
The shortcut is Ctrl+K, F12 but this can be changed in the shortcuts (search for shortcut editor.action.revealDefinitionAside)
More details here https://github.com/microsoft/vscode/issues/112136
For me setting workbench.editor.revealIfOpen to true like described here worked.
Controls whether an editor is revealed in any of the visible groups if opened. If disabled, an editor will prefer to open in the currently active editor group. If enabled, an already opened editor will be revealed instead of opened again in the currently active editor group. Note that there are some cases where this setting is ignored, e.g. when forcing an editor to open in a specific group or to the side of the currently active group.
[option + command + left click] or [option + command + F12] opens definition in split tab.
You have two different ways to do that task:
First one is, with the cursor on the word that you want to go to definition, press Alt+F12.
It opens a new tab like this one:
Second one is edit the settings and add this line:
"workbench.editor.enablePreview": false
It disables all the previews for code files so always that you want to go to definition, they will be open in a new tab, as you can see here:

How can I navigate the VS Code Explorer & open a file via keyboard only?

I'm doing a code review of a project, which means cycling through all the files in it. I want to keep my hands on the keyboard but neither do I want to have to CMD+P and type in the name of each file.
I've bound CMD+K,CMD+E to workbench.files.action.focusFilesExplorer which enables me to easily get to the Explorer, but then I can only explorer.openToSide, which isn't exactly what I want. I want to be able to open them directly, full-screen even if I have other windows open.
Are there commands for this that I can bind to? I suspect this isn't a feature yet.
By default on a mac you can use cmd+down to open the file.
Pressing Enter will edit the filename.
To open a file, just press Enter once you've selected it. It's bound to the list.select command by default. This also works for expanding / collapsing folders.
After workbench.files.action.focusFilesExplorer you can press Up or Down to navigate through file list while File explorer is fosuced. Then hit Enter to open the selected file.
Also I set Ctrl + E for workbench.files.action.focusFilesExplorer.
In my case, sequential navigating through list of files is a repeating sequence of these shortcuts:
Ctrl + E
Down
Enter
...
Ctrl + E
Down
Enter
...

Open current editing file in explorer tree

I'm wondering if there is a shortcut for VS Code that highlights in solution explorer tree current opened file. Like we have in Visual Studio:
Alt + Shift + L
Couldn't live with no complete answer, so figured out the following:
Without a direct keyboard shortcut:
Open the command palette via Cmd-Shift-P (or Cmd+P then >) and type Files: Reveal Active File in Side Bar.
This reveals the active file in the side bar similar to Visual Studio's Alt+Shift+L
Then, take the above and map a keyboard shortcut to it:
Open keyboard shortcut preferences file via Cmd-Shift-P followed by Preferences: Open Keyboard Preferences File.
Add in the following (taking Visual Studio's lead, I've personally mapped it to Alt+Shift+L, but map to what you want).
// Place your key bindings in this file to overwrite the defaults
[
{
"key": "shift+alt+l",
"command": "workbench.files.action.showActiveFileInExplorer",
},
]
Note that it's not as good as Visual Studio, Atom, etc. in that you can't then navigate with arrow keys in the tree (arrow keys navigate the active file contents), but I guess I'll eventually figure out how to do that.
#Tomoyuki Aota points out that you can do the following to navigate with arrow keys:
After Files: Reveal Active File in Side Bar, press Ctrl+Shift+E (Show
Explorer). After that, I can navigate the files in the explorer by the
arrow keys.
I don't think there's a command for that, but there is a shortcut to enable/disable revealing the current file as you open it:
"explorer.autoReveal": true
Try this:
Together with #Rob's correct answer:
"explorer.autoReveal": true
then Ctrl-Shift-E (Show explorer) focuses that file in the explorer and the arrow keys will navigate up/down/left/right like any list. This works even if the explorer is closed prior to the Ctrl-Shift-E.
Ctrl-Shift-E has the added bonus in that it will toggle focus between the highlighted file and its editor as well.
For mac, use Cmd-Shift-E
Right-click the file tab (not necessarily current one) and click "Reveal in Side Bar".
I was able to achieve this by customizing the workbench.files.action.focusFilesExplorer command with keybinding Alt + shift + L
Open the Keyboard Shortcuts ctrl + K + shift + S
search for focusFilesExplorer command & customize
Provide your keybindings that you are used to - I set mine to Alt + Shift + L
F1 then type "reveal" also works from in VS Code, at least for me.
It looks like the actual version of VS Code offers a command for this feature now.
In the settings, type revealInExplorer in the search bar to find the command. There is no default keybinding, so just put your own. Works like a charm!
Download the open in browser extension, that's it.
Right click > Open in primary/secondary browser function appears, and the ALT+B / ALT+SHIFT+B shortcuts become available.
Had a similar case and ended up here looking for help.
I opened a file in "preview" mode after clicking a link in a .md file and wanted to know where this opened file was located in the project.
I found this button in the upper right corner of VS Code which was exactly what I wanted:
It's called "show source"
Also check "Explorer: Auto Reveal Exclude" patterns in VS Code settings. Files that fit any of the patterns will not be auto revealed in the Explorer tree.

How can I navigate back to the last cursor position in Visual Studio Code?

What is the keyboard shortcut to navigate back to the last cursor position in Visual Studio Code?
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
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?
This will be different for each OS, based on the information in Key Bindings for Visual Studio Code.
Workbench configuration:
Go Back: workbench.action.navigateBack
Go Forward: workbench.action.navigateForward
Linux:
Go Back: Ctrl+Alt+-
Go Forward: Ctrl+Shift+-
Mac OS X:
Go Back: Ctrl + -
Go Forward: Ctrl + Shift (⇧) + -
Windows:
Go Back: Alt + ⬅️
Go Forward: Alt + ➡️
To answer for your question, for:
Windows use Alt+← for backward, and Alt+→ for forward navigation.
macOS use Ctrl+- for backward, and Ctrl+Shift+- for forward navigation.
Linux use Ctrl+Alt+- for backward, and Ctrl+Shift+- for forward navigation.
You can find the current key-bindings in "Keyboard Shortcuts editor."
You can even edit the key-binding as per your preference.
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.
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.
Open Settings: Code > Preferences > Keyboard Shortcuts
Find Go Forward and Go Back and set them to whatever you like.
If a key shortcut is double-assigned VSCode will tell you, and you can click-in and change/remove the other ones. eg. by shortcuts were already assigned to indent-in and indent-out.
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.
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!
Use Alt + ← / →
You can find all shortcuts in Key Bindings for Visual Studio Code.
Mac OS (MacBook Pro):
Back: Ctrl(control) + - (hyphen)
Back forward: Ctrl + Shift + - (hyphen)
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.
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)
from https://github.com/microsoft/vscode-docs/blob/vnext/release-notes/v1_65.md
Cursor locations:
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"
Edit locations:
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 locations
canNavigateForwardInNavigationLocations: Whether it is possible to go forward in navigation locations
canNavigateToLastNavigationLocation: Whether it is possible to go to the last navigation location
canNavigateBackInEditLocations: Whether it is possible to go back in edit locations
canNavigateForwardInEditLocations: Whether it is possible to go forward in edit locations
canNavigateToLastEditLocation: Whether it is possible to go to the last edit location
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.
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:
Go to Definition
Poke around at the definition for a while (i.e., move the cursor around)
Go Back to where you were in step 1.
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.
Last Edit Position v. Last Navigation Position
Go To Last Edit Position:
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.
Navigate Recent Cursor Positions:
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.
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.
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:
Go to the Last Edit Location
Cursor Undo (meaning: go to last cursor location)
Cursor Redo (Opposite of above)