Sublime Forum

Change working dir for hotkeyed command

#1

Hello! :cowboy_hat_face:

I am using sublime text 3 in folders / projects mode, not single files.
I am using Edit Command Palette plugin.

I have user keymap with one entry, like that:
Default (Windows).sublime-keymap — User

[
	{
		"keys": ["f5"],
		"command": "exec",
		"args": { "cmd": "mingw32-make.exe -j12 --always-make" },
	}
]

It runs by F5 just fine, but only if currently opened file located in root directory, where my Makefile is located. The goal is to run that by F5 with any file opened, maybe from child directories. Is there any way to do that?

Note: I am aware of build systems. With user build system I can do what I need that way:
MyBuildSystem.sublime-build

{
	"cmd": [ "mingw32-make.exe", "-j12", "--always-make" ],
	"working_dir": "${folder}"
}

That build system works just fine, no complaints, exactly what I need.
Is there analogue for “working_dir” and ${folder} but for keymapped commands?

Upd:
I tried that: no effect and no output.

[
	{
		"keys": ["f5"],
		"command": "exec",
		"args": {
			"cmd": [ "mingw32-make.exe", "-j12", "--always-make" ],
			"working_dir": "${folder}"
		},
	}
]

And I tried that: outputs “${folder}: No such file or directory.”

[
	{
		"keys": ["f5"],
		"command":"exec",
		"args": { "cmd": "mingw32-make.exe -j12 --always-make -C ${folder}" },
	}
]
0 Likes

#2

The special variables you can use in build systems ($file, $file_path, $folder, etc) are expanded by the build command before it executes the build, and as such are not available generically to key bindings, menu entries or command palette entries without some sort of custom plugin unless you use the build command to do it.

So all else being equal, the easiest thing to do is use the build command, which already does what you want. This does require that you define a sublime-build file, but you can bind any key you want to execute the build.

The easiest thing to do would be to bind the F5 key to the build command, and then add an appropriate selector to select your build system or select it specifically from the menu, and the key will run the build:

	{ "keys": ["f5"], "command": "build" },

Assuming you have some other build system you like to use for other things and so you don’t want to do this, you can bind a key to build and have it execute a specific build regardless of what’s currently selected as the build.

For example, assuming that you have a file in your User packages named MyBuildSystem.sublime-build as you outlined above:

{
	"cmd": [ "mingw32-make.exe", "-j12", "--always-make" ],
	"working_dir": "${folder}"
}

You can create your key binding like this:

[
	{
		"keys": ["f5"],
		"command": "build",
		"args": {
			"build_system": "Packages/User/MyBuildSystem.sublime-build",
			"variant": ""
		},
	}
]

Now when you press the key, Sublime will execute the build from the build system directly, even if you have a different build system selected in the menu (or it’s set to Automatic and this one doesn’t apply). Since it’s the build command doing the work, the proper variables are expanded out for you and everything will work the way you want.

Failing either of these options (or if you’re just generally opposed to creating a build system), you would need a custom plugin, such as this one that defines a menu_exec command that does this (if you want to use this and don’t know how, see this video on how to use plugins for a tutorial).

However, the build command is easier to do, particularly since you defined the sublime-build file already. As an added benefit, your build can still function as a regular build if you so desire.

The information above regarding the key binding and using it to execute a build is from the below video, which also shows other uses and ways to use this feature (there is also a playlist of videos that discusss build systems in general as well).

1 Like

#3

Thank you!
Your solution with using “build” command and specifying exact build file work better than perfect!
I have several different sublime-build files that can be applied to single C++ project, e.g. “fast build”, “full build”, “clean”, “debug build”, “validate with vulkan layers” (for vulkan project)… Getting tired of switching those manually every minute. But now I feel the power :ok_hand:

I saw the “build” command when I tried to do that on my own, but I did not notice the option to specify exact build, so I thought that this command can only use currently selected build. Of course it is better to use my existing build files, because that way I don’t have to duplicate build parameters.

BTW, great youtube channel! I am definitely going to watch it and improve my siblime skills with it =)

1 Like