Score:4

Use multiple commands in a single keyboard shortcut

pk flag

I would like to associate these two commands with two keyboard shortcuts to activate and quickly deactivate the television connected to the PC as if it were a second monitor.

To activate TV monitor and audio:

xrandr --output HDMI-0 --auto --right-of DVI-0; pacmd set-default-sink 1

To turn off:

xrandr --output HDMI-0 --off; pacmd set-default-sink 0

The commands in the terminal work, but as I associate them with a keyboard shortcut they stop working.

Ideas on how to solve? why does this happen?

Muhammed Özen avatar
in flag
Cant you try adding && between the commands?
Jacob Vlijm avatar
by flag
/bin/bash -c "command_1 && command_2", must be a dupe of something.
vanadium avatar
cn flag
@JacobVlijm for sure it will, but try for yourself searching "multiple commands single keyboard shortcut". It is not always easy to find matching duplicates.
Jacob Vlijm avatar
by flag
@vanadium sure! it's not a reproof, I just can't search atm on the phone. So a +1 from me to the answer and the question.
Terrance avatar
id flag
I see 4 separate commands there. You might want to see: https://stackoverflow.com/questions/6152659/bash-sh-difference-between-and showing that `;` and `&&` are used to run separate commands on the same line. I believe that in your new shortcut that you use you can put them all into a single line command by adding a `;` between both of your lines making them 1 line.
vanadium avatar
cn flag
@Terrance two commands are for two shortcut keys, one to enable TV screen and audio, and the second to disable.
vanadium avatar
cn flag
@JacobVlijm also upvoting the question: it is concise, straightforward and well documented with a title that reflects the actual question.
Terrance avatar
id flag
@vanadium I see that now, thanks! However, it is still 4 separate commands regardless.
vanadium avatar
cn flag
@Terrance, did a small edit so it is clear at first glance ;)
Score:12
cn flag

Only a single command indeed can be entered into the shortcut definition dialog. Two workarounds are 1) call a script that contains the commands and 2) use the construct sh -c "...".

In your case with only two commands, option 2 is simplest:

sh -c "xrandr --output HDMI-0 --off; pacmd set-default-sink 1"
Pinguto avatar
pk flag
I still have to test it properly, but for the moment the second option is working! I happily accept your solution!
Pinguto avatar
pk flag
Thank you very much for improving the readability of my question, but I had to edit the question as the activation and deactivation commands were reversed .. :)
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.