Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the base directory in visual studio code snippet?

Tags:

I am trying to get the file current directory in a snippet for visual studio code.

VSCode has a variable: TM_DIRECTORY, which is the fullpath.

eg:

{folder: "$TM_DIRECTORY"} 

would be replaced by

{folder: "/Volumes/my-project-path/ParentFolder/MyFolder"} 

But I want only MyFolder.

Normally, we can use a transform as indicated in the docs. Sublime Text works in the same way. But for as much as I try, the snippet simply outputs the whole regex.

Could someone answer with the magical variable/transform? :)

like image 827
Kev Avatar asked Jan 21 '18 10:01

Kev


2 Answers

Ok, finally found it.

${TM_DIRECTORY/^.+\\/(.*)$/$1/} gives the base directory.

The part I didn't get was the "double escape" of the directory separator / -> \\/.

like image 194
Kev Avatar answered Oct 10 '22 23:10

Kev


I wanted to add that in Windows the code above will print the entire directory.

You need to add a quadruple backslash vs the forward-slash:

${TM_DIRECTORY/^.+\\\\(.*)$/$1/} 
like image 38
Jose A Avatar answered Oct 11 '22 01:10

Jose A