How To Run a Terminal Command Using a Keyboard Shortcut On Mac.
With the built-in Terminal application on your Mac, you can run a series of commands to perform various actions on your computer. Terminal commands cover a lot of things that you usually do on your machines, from taking screenshots to renaming an entire group of files at once.
The only thing that may not seem inconvenient to you is to launch the Terminal application every time you want to execute a command. What if there was a better and faster way to run terminal commands on Mac?
Well, it really is. In fact, there are several ways to launch a Terminal command using a keyboard shortcut on a Mac. You can assign your favorite keyboard shortcut to your specific command, and pressing that shortcut will execute that command on your computer.
Use an application to run commands with a shortcut on a Mac
The easiest way to assign keyboard shortcuts to your commands is to use a third party application called iCanHazShortcut. This app makes it much easier to assign any keyboard shortcut to literally any command on your Mac.
To customize an application, all you need to know is the keyboard shortcut you want to assign and the command you need to execute.
Download the free open source application to your Mac and move it to the Applications folder. After installation, launch the application.
When the application interface has loaded, you will by default be on the Shortcuts tab. On this screen, find the button with the + (plus) sign at the bottom and click on it to add a new shortcut.
On the next screen, you can customize the shortcut as well as the command to be executed. Here’s what you need to enter in each of the fields on the screen.
Shortcut – Place your cursor in this field and enter the shortcut you want to assign to the command.
An action is an optional name that you can assign to later search for a shortcut in the list.
Command – Enter here the exact command you want to execute.
Workdir – If your team needs a specific directory as a working directory, select it here.
You can do a test run by clicking on the play icon below. If you’re happy with it, click on the icon next to it and the shortcut will be saved.
The app’s Settings tab also has several options that you can tweak. This should give you more control over how the application works on your Mac.
From now on, when you press the specified keyboard shortcut, it launches your Terminal command.
If more than one command needs to be executed, you can also add them to the application. Labels can be changed and even removed if you like.
Execute commands with a shortcut using Automator
Automator also lets you run your commands with a keyboard shortcut. First, you need to create a service containing your command and then assign a keyboard shortcut to the service.
Launch the Automator app on your Mac. When the new document screen appears, click Tools and select Select.
On the next screen, find the action named “Run Shell Script” in the list of actions. When you find it, drag it to the main panel on the right side.
You will see a large white box below the action you just added. Enter all the commands you want to execute in this field. Think of this field as a terminal window where you enter your commands.
When you’ve entered your commands, click the File menu at the top and select Save to save the service. Enter a friendly name for the service and click Save.
Now that the service has been created, it’s time to assign a keyboard shortcut to it. To do this, click the Apple logo in the upper left corner and select System Preferences. On the next screen select “Keyboard”.
Go to the Shortcuts tab and select Services from the list on the left. Then find your service in the list on the right, click it and press the desired keyboard shortcut.
Your service will be assigned the keyboard shortcut of your choice.
When you click this shortcut, it starts a service, which in turn executes your terminal command on your Mac.
Use ActionShortcuts to run commands with the shortcut
ActionShortcuts lets you run more things than just traditional terminal commands. It allows you to run Apple scripts, workflows, services, and of course, terminal commands.
Unlike other methods, this app is paid and costs $ 2.99. You can use a 7-day trial if you want to try it first.
Shown below is how to run a terminal command using a keyboard shortcut using this application.
Launch TextEdit app on your Mac. Open the Format menu and choose Make Plain Text to remove the formatting.
Enter in the file all the Terminal commands that you want to execute. Then save the file by clicking the File menu and choosing Save.
In the Save As dialog box, enter any file name, but make sure the extension is a command. Click “Save” to save the file.
Download, install and open the ActionShortcuts app on your Mac. Click the Open Scripts Folder button in the main interface.
When the folder opens, drag the batch file into it. Go back to the app and you will see your file listed. Click the Record Shortcut button next to the file to assign a keyboard shortcut.
Once a shortcut is assigned, clicking on it will launch a .command file containing your commands on your Mac.
If you want to add additional files to run, you can do so by clicking the application icon in the menu bar and choosing Open Scripts Folder. All commands that need to be executed must be placed in this folder and the application will recognize them.