Go to Definition and Back to Reference in VS Code

avatar
Borislav Hadzhiev

Last updated: Apr 6, 2024
5 min

banner

# Table of Contents

  1. Go to Definition and Back to Reference in VS Code
  2. Going back to the Reference
  3. Open definition in Peek mode in VS Code
  4. Disabling the setting that opens Definitions in Peek mode by default
  5. Using the Go to Source definition action in VS Code
  6. Using the Go to Type definition action in VS Code

# Go to Definition and Back to Reference in VS Code

To go to a definition in VS Code:

  1. Set your cursor on the variable, function or class.
  2. Press the F12 keyboard shortcut.

Alternatively, you can right-click on a reference and select Go to Definition.

click go to definition

Here is a short clip that demonstrates the process.

right click go to definition

You can also use the Command Palette to go to a definition:

  1. Set your cursor on the reference.

  2. Press:

  • Ctrl + Shift + P on Windows and Linux.
  • Command + Shift + P on macOS.
Note: you can also press F1 to open the Command Palette.
  1. Type Go to Definition and select the option.

go to definition command palette

Alternatively:

  1. On Windows: press and hold Ctrl (or Alt) and click on the reference.
  2. On macOS: press and hold Cmd (or Option) and click on the reference.
  3. On Linux: press and hold Ctrl (or Alt) and click on the reference.

hold ctrl and click

When you press and hold Ctrl (or Cmd) and hover over a symbol, it becomes clickable.

You can also open the definition to the side:

  1. Set your cursor on the reference.

  2. Press:

  • Ctrl + Shift + P on Windows and Linux.
  • Command + Shift + P on macOS.
Note: you can also press F1 to open the Command Palette.
  1. Type Open Definition to the Side and select the option.

open definition to the side

# Going back to the reference

If you need to go back to the reference:

  1. On Windows: press Alt + Left arrow key.
  2. On macOS: press Ctrl + -.
  3. On Linux: press Ctrl + Alt + -.

You can also view the Go Back keyboard shortcut for your operating system in this table in the docs.

Alternatively, you can use the Command Palette:

  1. Press:
  • Ctrl + Shift + P on Windows and Linux.
  • Command + Shift + P on macOS.
Note: you can also press F1 to open the Command Palette.
  1. Type Go Back and select the option.

go back action

You can also view the keyboard shortcut for your operating system directly in the Command Palette.

If you need to change the keyboard shortcut:

  1. Press:
  • Ctrl + Shift + P on Windows and Linux.
  • Command + Shift + P on macOS.
Note: you can also press F1 to open the Command Palette.
  1. Type Keyboard Shortcuts and select Preferences: Open Keyboard Shortcuts.

preferences open keyboard shortcuts

  1. Search for go back and double-click on the option.

change go back keyboard shortcut

  1. Specify the key combination and press Enter.

# Open definition in Peek mode in VS Code

If you need to peek a definition:

  1. Right-click on a reference.

  2. Click on Peek and then select Peek definition.

peek definition

The definition opens in Peek mode, directly in the current file.

open definition in peek mode

You can also use the Command Palette to open a definition in peek mode:

  1. Select the reference.

  2. Press:

  • Ctrl + Shift + P on Windows and Linux.
  • Command + Shift + P on macOS.
Note: you can also press F1 to open the Command Palette.
  1. Type peek definition and select the option.

peek definition command palette

# Disabling the setting that opens Definitions in Peek mode by default

There is a setting that opens definitions in peek mode by default.

If you need to disable or enable the setting:

  1. Press Ctrl + Shift + P (or Command + Shift + P on macOS).
Note: you can also press F1 to open the Command Palette.
  1. Type user settings and select Preferences: Open User Settings.

open user settings

You can also open the settings screen by pressing Ctrl + , on Windows and Linux or Cmd + , on macOS.

  1. Type definition peek and disable or enable Editor: Definition Link Opens in Peek.

disable definition opens in peek mode by default

# Using the Go to Source definition action in VS Code

If you need to go to the implementation of the function or class:

  1. Right-click on the reference.
  2. Select Go to Source Definition.

go to source definition

Here is a short clip that demonstrates using the action.

go to source definition action

When using the Go to Source Definition action, we jump to the location where the function or class is implemented (e.g. in a JavaScript file in node_modules).

You can also use the action via the Command Palette:

  1. Select the reference.

  2. Press:

  • Ctrl + Shift + P on Windows and Linux.
  • Command + Shift + P on macOS.
Note: you can also press F1 to open the Command Palette.
  1. Type go to source definition and select TypeScript: Go to Source Definition.

go to source definition command palette

# Using the Go to Type definition action in VS Code

There is also a Go to Type definition action if you need to jump to the type definition of the symbol:

  1. Right-click on the reference.
  2. Select Go to Type Definition.

right click go to type definition

Here is a short clip that demonstrates the process.

go to type definition

You can also use the Command Palette to trigger the Go to Type Definition action:

  1. Select the reference.

  2. Press:

  • Ctrl + Shift + P on Windows and Linux.
  • Command + Shift + P on macOS.
Note: you can also press F1 to open the Command Palette.
  1. Type go to type definition and select the action.

go to type definition command palette

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.