Ever since I discovered Cmus, a music player for the terminal, I rarely use GUI music players.
With cmus-remote
, you can even control Cmus remotely. However, using cmus-remote
still requires starting Cmus first, otherwise it will prompt "cmus-remote: cmus is not running".
And starting cmus requires opening a terminal, which is similar to GUI music players. This is unacceptable for a lazy person like me. So, is there a way to start Cmus in the background when it is launched?
The answer is tmux!
tmux is a terminal multiplexer, similar to screen and others.
How to do it?#
Let's organize our thoughts first.
First, we want to use cmus-remote
to control Cmus, but we need to start Cmus first. So, we need to find a way to start Cmus in the background, as we don't need to see its interface. We can accomplish this using tmux
.
The process is as follows:
- Create a
tmux
session. - Start Cmus within the session.
- Put the session in the background.
The normal approach won't work because we need the entire process to be invisible and completely run in the background.
So, we need to:
- Create a
tmux
session in the background. - Pass the command to this background session to start Cmus.
The command to create a background tmux
session is as follows:
tmux new -s session_name -d
The command to pass a command to tmux
is as follows:
tmux send-keys -t session_name "command_to_pass"
So, the entire process can be written into a script with just two lines of code:
tmux new -s cmus -d # Create a tmux session named cmus in the background
tmux send-keys -t cmus "cmus" C-m # Send the command "cmus" to the tmux session named cmus and press Enter
# Note that if the command needs to be executed with Enter, you need to add C-m at the end, which represents an Enter key press.
Once it starts, we naturally want it to play. So, we can control Cmus to play using cmus-remote
after it starts:
tmux new -s cmus -d
tmux send-keys -t cmus "cmus" C-m
sleep 1s
cmus-remote --play
The sleep
is added here to prevent cmus-remote
from executing before cmus has finished starting, which would result in no music playing.
Once it can start, it should also be able to be closed. Here's the code for that:
tmux send-keys -t cmus "cqy" # Send "cqy" to the tmux session named cmus to exit cmus
# c stands for pause, q stands for quit, y is for confirmation. Since we don't need to press Enter, we don't need to add C-m.
tmux kill-session -t cmus # Kill the tmux session named cmus
Of course, you can also simply kill the tmux
session...
Final Product#
By writing the two parts above into functions and using an if
statement to determine which one to trigger based on the input parameter, we get the final product:
play() {
tmux new -s cmus -d
tmux send-keys -t cmus "cmus" C-m
sleep 1s
cmus-remote --play
}
quit() {
tmux send-keys -t cmus "cqy"
tmux kill-session -t cmus
}
if [ $1 == "play" ];then
play
elif [ $1 == "quit" ];then
quit
else
echo "Invalid command!"
fi
cmus-remote Commands#
You can use cmus-remote --help
to view the help, or simply use man
.
Here are some commonly used commands:
# Play
cmus-remote --play
or
cmus-remote -p
# Pause
cmus-remote --pause
or
cmus-remote --u
# Previous track
cmus-remote --prev
or
cmus-remote -r
# Next track
cmus-remote --next
or
cmus-remote -n
dwm Key Bindings#
Those who use dwm should already know, so I won't go into detail here.