Sublime Forum

Unable to change build system in a keybinding

#1

Hello everyone, I am a c++ games developer and I have recently switched to sublime text 4 from visual studio. One of reasons why I switched was because of the customisability and powerful build system configuration which sublime text offered. So far it has been great, however I have run into a few issues when trying to set up my build systems.

What I like to do when I am developing c++ games, is to have 2 seperate keybindings for compiling the code (Ctrl+b) and for running the code (Ctrl+e). So far I have made 2 build systems for running and compiling the code which I have called cpp_run.sublime-build and cpp_build.sublime-build, I have checked to see if these have worked individually and they have worked. However, when it came time to try and put these commands into a keybinding I have had some issues:

The keybindings I have added are the following:

[
	{ "keys": ["Ctrl+s"], "command": "save {\"async\": true}"},
	{ 
		"keys": ["Ctrl+b"],
		"command": "run_multiple_commands",
		"args": {
			"commands": [
				{ "command": "set_build_system {\"file\": \"Packages/User/cpp_build.sublime-build\"}", "context": "window" },
				{ "command": "build", "context": "window" }
			]

		}
	},
	
	{ 
		"keys": ["Ctrl+e"],
		"command": "run_multiple_commands",
		"args": {
			"commands": [
				{ "command": "set_build_system {\"file\": \"Packages/User/cpp_run.sublime-build\"}", "context": "window" },
				{ "command": "build", "context": "window" }
			]

		}
	}
]

I have installed and used a package called run_multiple_commands (link to github repo: https://github.com/johnyluyte/sublime-text-multi-commads) to allow me to run multiple commands in a keybinding (this is from this sublime text discussion: How to bind multiple commands for a keybinding?). For some reason it will build the code but it will not select the specified build system I have specified (it will just stick to the default build system which was already selected when I started sublime). I have tried adding "context": "window" to the command for selecting the build system however this also did not make any difference.

Is there a way I can solve this?

0 Likes

#2

That is because the following is an invalid command.
{ "command": "set_build_system {\"file\": \"Packages/User/cpp_build.sublime-build\"}", "context": "window" },

The arguments for the command should not be specified inside the command itself. The correct syntax is

{ 
    "command": "set_build_system", "args": { "file": "Packages/User/cpp_build.sublime-build" }
}

The reason why it’s building the code is because your build command is running and since you have ran it prior, it was automatically selected. The set_build_system command is of course wrongly done (as specified above), hence it wasn’t running.

Also since you are running ST4, there is no need for an additional package to run multiple commands. There is a built in chain command that can be used.

Refactoring your key bindings to use chain command and correctly using commands, we will have

{
    "keys": ["ctrl+b"],
    "command": "chain",
    "args": {
        "commands": [
            { "command": "set_build_system", "args": { "file": "Packages/User/cpp_build.sublime-build" } },
            { "command": "build" },
        ]
    }
},
{
    "keys": ["ctrl+e"],
    "command": "chain",
    "args": {
        "commands": [
            { "command": "set_build_system", "args": { "file": "Packages/User/cpp_run.sublime-build" } },
            { "command": "build" },
        ]
    }
}

I haven’t tested this but it should theoretically work. And it doesn’t require any additional package as well.

Side note: Not sure why you have { "keys": ["Ctrl+s"], "command": "save {\"async\": true}"}, Besides the command being wrongly set, ctrl + s is already mapped to the save command.

0 Likes

#3

Thanks so much! This finally made it work! (by the way Ctrl+s was mapped to save the file because I was running neovintageous, and so Ctrl+s does not save the file in normal mode)

0 Likes