Switch to the folder of open file in VS code

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.

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 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.

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.

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

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?

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>")?

Excellent idea with the raw string. My solution is

image

Thanks!

Note that in Emacs,

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

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.

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.

5 Likes

In the meantime, I am using the following solution:

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

The Julia REPL should be open.