# Switch to the folder of open file in VS code

**URL:** <https://discourse.julialang.org/t/switch-to-the-folder-of-open-file-in-vs-code/44813>\
**Category:** General Usage\
**Created:** [August 12, 2020, 3:47pm UTC](https://discourse.julialang.org/t/switch-to-the-folder-of-open-file-in-vs-code/44813 "2020-08-12T15:47:47Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [August 12, 2020, 3:47pm UTC](https://discourse.julialang.org/t/switch-to-the-folder-of-open-file-in-vs-code/44813/1 "2020-08-12T15:47:47Z")

</div>

Is there a command to switch to the folder of the current file in VS code?

I can see that there is a command to copy the name of the current file,  
but at least on Windows that path is unusable in Julia since it has single  
backslashes.

---

<div class="post-metadata">

**Author:** ![swt30](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/swt30/32/4667_2.png) [@swt30](https://discourse.julialang.org/u/swt30)\
**Post date:** [August 12, 2020, 4:04pm UTC](https://discourse.julialang.org/t/switch-to-the-folder-of-open-file-in-vs-code/44813/2 "2020-08-12T16:04:21Z")

</div>

What do you mean to switch to the folder? Do you mean cd in the terminal/Julia, or reopen the whole workbench at a given folder? To do the latter, I use [Open Folder Context Menus for VS Code - Visual Studio Marketplace](https://marketplace.visualstudio.com/items?itemName=chrisdias.vscode-opennewinstance) – right click the folder in the file tree, “reopen workbench here”, and VSC reloads with the scope narrowed to just that folder.

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [August 12, 2020, 4:06pm UTC](https://discourse.julialang.org/t/switch-to-the-folder-of-open-file-in-vs-code/44813/3 "2020-08-12T16:06:44Z")

</div>

Yes, change the current folder in Julia to the folder of the viewed file. I do not want to change the current env when doing this.

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [August 12, 2020, 4:10pm UTC](https://discourse.julialang.org/t/switch-to-the-folder-of-open-file-in-vs-code/44813/4 "2020-08-12T16:10:05Z")

</div>

Are you looking for something different from `cd(@ __DIR__ )`?

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [August 12, 2020, 4:19pm UTC](https://discourse.julialang.org/t/switch-to-the-folder-of-open-file-in-vs-code/44813/5 "2020-08-12T16:19:23Z")

</div>

If I had the code `cd(@ __DIR__ )` in the open file, I could evaluate it to switch the folder.  
Otherwise I don’t see how this would help. What did you have in mind?

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [August 12, 2020, 4:42pm UTC](https://discourse.julialang.org/t/switch-to-the-folder-of-open-file-in-vs-code/44813/6 "2020-08-12T16:42:41Z")

</div>

I’m not sure what behavior you’re looking for - a keyboard shortcut? You can use the `raw_str` macro with Windows filepaths to avoid escaping backslashes. How about `cdfile(fname) = cd(dirname(fname))` in your `startup.jl`, then shift-alt-c to copy the current file’s path to the clipboard, then `cdfile(raw"<ctrl-v>")`?

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [August 12, 2020, 4:57pm UTC](https://discourse.julialang.org/t/switch-to-the-folder-of-open-file-in-vs-code/44813/7 "2020-08-12T16:57:57Z")

</div>

Excellent idea with the raw string. My solution is

![image](https://global.discourse-cdn.com/julialang/original/3X/a/f/af16d389826f10eca23aae5d8e13d28c7740edd3.png)

Thanks!

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [August 13, 2020, 7:07am UTC](https://discourse.julialang.org/t/switch-to-the-folder-of-open-file-in-vs-code/44813/8 "2020-08-13T07:07:28Z")

</div>

Note that in Emacs,

[https://github.com/tpapp/julia-repl/](https://github.com/tpapp/julia-repl/)

has `julia-repl-cd`, bound to C-c C-p. I generally find this useful to eg run parts of a test suite.

---

<div class="post-metadata">

**Author:** ![roble](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roble/32/12303_2.png) [@roble](https://discourse.julialang.org/u/roble)\
**Post date:** [August 13, 2020, 8:32am UTC](https://discourse.julialang.org/t/switch-to-the-folder-of-open-file-in-vs-code/44813/9 "2020-08-13T08:32:06Z")

</div>

It might not be exactly what you are looking for but hear me out.

I usually browse using the explorer. There you have the option to open powershell (or cmd) at your path. In the shell you can type `code .` which opens the folder in vs code.

I do this since opening a file eg. `code foo.jl` does not work properly with the vs code for julia plugin.

---

<div class="post-metadata">

**Author:** ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)\
**Post date:** [August 13, 2020, 10:00am UTC](https://discourse.julialang.org/t/switch-to-the-folder-of-open-file-in-vs-code/44813/10 "2020-08-13T10:00:23Z")

</div>

The proper solution is to add a command and context menu entry to `cd` to the current file; we’ll get to adding that at some point in the near future, I hope.

---

<div class="post-metadata">

**Author:** ![RaulDurand](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rauldurand/32/2044_2.png) [@RaulDurand](https://discourse.julialang.org/u/RaulDurand)\
**Post date:** [November 3, 2020, 3:07pm UTC](https://discourse.julialang.org/t/switch-to-the-folder-of-open-file-in-vs-code/44813/11 "2020-11-03T15:07:49Z")

</div>

In the meantime, I am using the following solution:

```julia
    {
        "key": "ctrl+alt+enter",
        "command": "workbench.action.terminal.sendSequence",
        "args": {
          "text": "cd(\"${fileDirname}\")\ninclude(\"${fileBasename}\")\n"
        },
        "when": "editorTextFocus"
    },

```

The Julia REPL should be open.
