How to get VS Code active window's current file path?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
omareg94
Posts: 94
Joined: 27 Jun 2016, 22:46

How to get VS Code active window's current file path?

Post by omareg94 » 27 Oct 2020, 00:01

I need to get active window's file path of VS Code.
I'm able to get active window's file path of Sublime Text using the method below:

Code: Select all

RunWait, "subl --command ""copy_path""",,Hide
filepath = Clipboard
I've searched for a similar CLI command for VS Code but I couldn't find any.
Last edited by omareg94 on 27 Oct 2020, 09:54, edited 1 time in total.

User avatar
boiler
Posts: 17400
Joined: 21 Dec 2014, 02:44

Re: How to get VS Code active window's current file path?

Post by boiler » 27 Oct 2020, 04:30

You can get the file path from the title of the VS Code window. The RegExMatch below removes the and space preceding the filename if it appears (indicating unsaved changes) and the - Visual Studio Code from the end.

Code: Select all

WinGetTitle, filepath, ahk_exe Code.exe
RegExMatch(filepath, Chr(9679) . "? ?\K.*(?= - Visual Studio Code)", filepath)

omareg94
Posts: 94
Joined: 27 Jun 2016, 22:46

Re: How to get VS Code active window's current file path?

Post by omareg94 » 27 Oct 2020, 09:51

boiler wrote:
27 Oct 2020, 04:30
You can get the file path from the title of the VS Code window. The RegExMatch below removes the and space preceding the filename if it appears (indicating unsaved changes) and the - Visual Studio Code from the end.

Code: Select all

WinGetTitle, filepath, ahk_exe Code.exe
RegExMatch(filepath, Chr(9679) . "? ?\K.*(?= - Visual Studio Code)", filepath)
This doesn't work for new versions of VS Code (I have 1.50.1) because VS Code shows only the file name on title.
Also I don't prefer to use this approach (even if there's some setting in VS Code to show full path) because I want to keep windows' titles simplified.
I hope there's another approach to get file's path.

User avatar
boiler
Posts: 17400
Joined: 21 Dec 2014, 02:44

Re: How to get VS Code active window's current file path?

Post by boiler » 27 Oct 2020, 10:15

I also have 1.50.1, and it shows the full path. Perhaps I changed the setting to show the full path at some point to allow it to grab the path for other scripts.

In case you don't eventually find an alternate approach and you want to change your title to the full path, you would change the window.title setting to ${activeEditorLong}.

KnIfER
Posts: 12
Joined: 27 Nov 2023, 06:10

Re: How to get VS Code active window's current file path?

Post by KnIfER » 28 Nov 2023, 00:11

I have vscode 1.64 and the title only contains filename.

My workaround : send hotkey to vscode and copy full path to clipbpard.

Post Reply

Return to “Ask for Help (v1)”