Automatically Open the Current Directory in VS Code From Terminal
Shannon Crabill

Shannon Crabill @scrabill

About: Front-End Software Engineer

Location:
Baltimore, MD
Joined:
Apr 6, 2017

Automatically Open the Current Directory in VS Code From Terminal

Publish Date: Jan 5 '21
38 14

Last year, I was blown away by some Terminal commands that I learned. My favorite one is open . to open a Finder window for whichever directory you are currently in.

At one point I had a similar command to instantly open a folder in VS Code (Visual Studio Code), but deleted it after I switched from VS Code to Atom. I’ve since switched back to VS Code and after begrudgingly dragging folders into VS Code to open them, I decided to figure out how to enable that command.

Luckily, it takes a few steps to activate.

  • With VS Code running, enter Command + Shift + P to open the Command Palette (or View > Command Palette from the menu bar)

Alt Text

  • A search bar will open up. Search for “Shell” or “Shell Command” and you should see one named Shell Command: install "code" command in PATH.

Alt Text

  • Select it and a confirmation Shell command "code" successfully installed in PATH. should pop up (for me the pop up appeared in the lower, righthand corner).

Alt Text

  • If you already have a Terminal session running, quit or restart it.

  • When you are in the directory with the files you want to open in VS Code, type code . (that is the word “code” followed by a space, then a period) and the folder will automatically open in VS Code.

Alt Text

The post Automatically Open the Current Directory in VS Code From Terminal appeared first on Shannon Crabill — Front End Software Engineer.

Comments 14 total

  • Mike McQueen
    Mike McQueenJan 6, 2021

    This plus the gpm plugin, which let's me open any git project from within vscode Without trying my hands off the keyboard make me feel like "Hackerman".

  • Ben Sinclair
    Ben SinclairJan 6, 2021

    I'm pretty sure VS Code installs itself into the path anyway, doesn't it? I've never had to do anything and it works using code even on Windows.

    A hot tip though using open is that you can pass anything to it and it'll behave like you clicked that thing in the GUI, even URLs. So on a Mac, you could do open https://dev.to and it'd open in your default browser. Since VS Code registers itself as the handler for vscode schemes, you can also do this:

    open 'vscode://file//Users/myusername/projects/myfile.txt:123:45'
    
    Enter fullscreen mode Exit fullscreen mode

    And it'll open VS Code with the file /Users/myusername/projects/myfile.txt and your cursor already on line 123 and column 45. This is how those error messages that say things like "syntax error at line 20" get to magically work in some cases.

    If you use VSCodium, change the appropriate bit to vscodium://, if you use Gnome or any derivatives, change open to gnome-open. Other Linux systems have other syntaxes, but generally one of those two will get you by.

    • Nick Taylor
      Nick TaylorJan 6, 2021

      It doesn't from what I've understood. You have to do it explicitly. I typically do it via the command palette like Shannon did.

      • Ben Sinclair
        Ben SinclairJan 6, 2021

        Have you tried it without? What OS are you using?

        • Mel DeJesus
          Mel DeJesusApr 16, 2023

          Yep; I'm on Ventura OS ~ it might depend on how VS code was installed, but I've had to follow Shannon's approach.

    • Shannon Crabill
      Shannon CrabillJan 7, 2021

      A hot tip though using open is that you can pass anything to it and it'll behave like you clicked that thing in the GUI, even URLs. So on a Mac, you could do open dev.to and it'd open in your default browser. Since VS Code registers itself as the handler for vscode schemes, you can also do this:

      open 'vscode://file//Users/myusername/projects/myfile.txt:123:45'
      
      Enter fullscreen mode Exit fullscreen mode

      And it'll open VS Code with the file /Users/myusername/projects/myfile.txt and your cursor already on line 123 and column 45. This is how those error messages that say things like "syntax error at line 20" get to magically work in some cases.

      Whooooooooa I did not know you could get this specific with open .. This should be it's own blog post.

  • James Q Quick
    James Q QuickJan 6, 2021

    Yassss!! Use this all the time. Share in Discord :)

  • Phu Ngo
    Phu NgoJun 19, 2021

    Anyway to do it the other way around? When I open a folder from vscode, to then automatically open the terminal?

  • Saad Bakhiyi
    Saad BakhiyiJan 8, 2022

    Thank you, you help a lot

  • Joshua Blauvelt
    Joshua BlauveltFeb 23, 2022

    This is great, thanks! I can open any folder from the integrated terminal but it will start a new instance of VS Code; is there a way to open the folder within the same VS Code instance? screenshot

    • Rob
      RobMar 6, 2022

      Try code -r [name of file/folder]

    • Sudhakar
      Sudhakar Mar 22, 2022

      Try code -a . (It opens the folder within the same VS Code instance)
      Try code -r . (It refreshes the same VS Code instance and opens the folder, terminal also gets refreshed)

      • Jay Rodriguez
        Jay RodriguezSep 13, 2024

        What about files? can you open them as well from the same instance if your in the directory?

  • Ilay Rosenstein
    Ilay RosensteinJul 11, 2022

    thank you! been looking for it myself for a while.

Add comment