How to use the command dmenu (with examples)
- Linux
- December 25, 2023
The dmenu
command is a dynamic menu that creates a menu from a text input with each item on a new line. It is a powerful tool that allows users to create interactive menus in their scripts or to quickly select options from a list of choices.
Use case 1: Display a menu of the output of the ls
command
Code:
ls | dmenu
Motivation: The ls
command lists the files and directories in the current directory. By piping the output of ls
to dmenu
, we can display the list of files and directories as a menu, allowing the user to select an option.
Explanation: The ls
command lists the files and directories in the current directory. The |
character is a pipe, which takes the output of the command on the left and uses it as input for the command on the right. The dmenu
command then creates a menu from the input.
Example output: If the current directory contains files named “file1.txt” and “file2.txt”, the output of ls | dmenu
would display a menu with the options “file1.txt” and “file2.txt”.
Use case 2: Display a menu with custom items separated by a new line
Code:
echo -e "red\ngreen\nblue" | dmenu
Motivation: In some cases, users may want to create a custom menu with specific options. By using the echo
command to generate a list of items separated by new lines (\n
) and piping it to dmenu
, users can create a custom menu with their desired options.
Explanation: The echo
command outputs the specified text, in this case, “red\ngreen\nblue”, followed by a newline character. The -e
option enables interpretation of backslash escapes, such as \n
. The pipe (|
) character takes the output of echo
and uses it as the input for dmenu
.
Example output: The command echo -e "red\ngreen\nblue" | dmenu
would display a menu with the options “red”, “green”, and “blue”.
Use case 3: Let the user choose between multiple items and save the selected one to a file
Code:
echo -e "red\ngreen\nblue" | dmenu > color.txt
Motivation: Sometimes, users may want to allow the user to select an option from a menu and save the selected option for further processing. By redirecting the output of dmenu
to a file using >
, users can save the selected option to a file.
Explanation: The echo
command is used to generate a list of items separated by new lines. The -e
option enables interpretation of backslash escapes. The pipe (|
) character takes the output of echo
and feeds it as input to dmenu
. The >
character is used to redirect the output of dmenu
to the file color.txt
.
Example output: If the user selects “green” from the menu, the output of echo -e "red\ngreen\nblue" | dmenu > color.txt
would save the selected option “green” to the file color.txt
.
Use case 4: Launch dmenu on a specific monitor
Code:
ls | dmenu -m 1
Motivation: When using multiple monitors, users may want to display dmenu on a specific monitor. By specifying the monitor number using the -m
option, users can choose on which monitor dmenu is displayed.
Explanation: The ls
command lists the files and directories in the current directory. The |
character is a pipe, which takes the output of the command on the left and uses it as input for the command on the right. The dmenu
command creates a menu from the input. The -m 1
option specifies that dmenu should be launched on monitor number 1.
Example output: If monitor 1 is the primary monitor, the command ls | dmenu -m 1
would display the dmenu on monitor 1.
Use case 5: Display dmenu at the bottom of the screen
Code:
ls | dmenu -b
Motivation: By default, dmenu is displayed at the top of the screen. However, users may prefer to display it at the bottom for convenience. By using the -b
option, users can display dmenu at the bottom of the screen.
Explanation: The ls
command lists the files and directories in the current directory. The |
character is a pipe, which takes the output of the command on the left and uses it as input for the command on the right. The dmenu
command creates a menu from the input. The -b
option tells dmenu to display itself at the bottom of the screen.
Example output: The command ls | dmenu -b
would display dmenu at the bottom of the screen instead of the default top position.
Conclusion:
The dmenu
command is a versatile tool for creating dynamic menus from text input. With its various options and piping capabilities, users can create interactive menus, save selected options to files, and customize the appearance of the menu. By understanding the different use cases and examples provided, users can make the most of the dmenu
command and incorporate it into their scripts and workflows.