How to use the command 'rofi' (with examples)
- Linux
- December 17, 2024
Rofi is a versatile tool designed primarily as an application launcher and window switcher for Unix-like operating systems. It is renowned for its simplicity and efficiency, allowing users to easily find and launch applications, execute commands, or switch between open windows using a minimalist interface. Besides its primary purposes, Rofi can be customized in various ways, including creating a dynamic menu powered by standard input and output, expanding its functionality beyond just launching apps or switching windows. Let’s explore some practical use cases of Rofi to understand its capabilities better.
Use case 1: Show the list of apps
Code:
rofi -show drun
Motivation:
In a graphical user interface environment, launching applications quickly and efficiently is essential for maintaining productivity. While many desktop environments provide built-in app launchers, they can be visually cluttered or slow, especially when the system has an extensive list of installed applications. Rofi provides a clean and responsive alternative by displaying a simple menu of available applications. This use case focuses on listing all desktop applications, helping users to launch their desired apps swiftly without navigating through complex menus.
Explanation:
rofi
: This is the command that runs the Rofi application itself.-show drun
: The-show
option tells Rofi what mode to start in. Thedrun
(desktop run) mode is specifically for launching desktop applications, typically those defined in.desktop
files in your system, offering a user-friendly approach to access installed software.
Example Output:
Upon execution, Rofi presents a graphical interface with a text input field. As you type, Rofi will filter the application list to match your search query. Selecting an application either by mouse or keyboard will promptly launch it.
Use case 2: Show the list of all commands
Code:
rofi -show run
Motivation:
Quick command-line access is invaluable for users who need to execute common commands without opening a terminal window. This use case is beneficial for users who frequently run diagnostic commands, scripts, or applications from the terminal. Rofi in run
mode allows users to type in and execute any command directly, streamlining workflows and eliminating the need to toggle between applications and terminal windows.
Explanation:
rofi
: Invokes the Rofi application.-show run
: The-show run
option sets Rofi to allow manual entry of executable commands. It lists the executable commands found in directories listed in the user’s PATH, facilitating access to command-line utilities and scripts.
Example Output:
Rofi will display a simple prompt where you can enter a command. As an example, typing firefox
will run the Firefox browser. It effectively mimics a bare terminal command line.
Use case 3: Switch between windows
Code:
rofi -show window
Motivation:
In multitasking environments, users often have numerous windows open simultaneously. Efficiently managing these windows is crucial for productivity. Although alt-tabbing is a common method to cycle through windows, it can be inefficient when you’re dealing with a high number of applications or windows. Rofi in window
mode provides an efficient solution by listing all open windows, allowing users to switch to any window directly by searching or selecting from the list.
Explanation:
rofi
: Calls the Rofi utility.-show window
: The-show window
option puts Rofi into window switching mode, displaying a list of currently open windows from which the user can select to quickly jump to a specific window.
Example Output:
A user interface pops up listing all open windows, usually showing the window’s application name or title. Selecting a window either by typing part of its title or using keyboard/mouse navigation instantly brings it into focus.
Use case 4: Pipe a list of items to stdin
and print the selected item to stdout
Code:
printf "Choice1\nChoice2\nChoice3" | rofi -dmenu
Motivation:
Beyond its regular modes, Rofi’s dmenu
mode is a powerful feature for scripting and automation. It allows developers and users to feed custom lists into Rofi and capture the selection, facilitating user interaction in scripts or customized workflows. This is particularly useful in creating simple input dialogs, choosing configuration options, or any scenario requiring user choice from a predefined set of options.
Explanation:
printf "Choice1\nChoice2\nChoice3"
: This command generates a list of items separated by newline characters, simulating a predefined list of choices.|
: The pipe operator feeds the output of one command as input to another.rofi -dmenu
: Here, Rofi is set todmenu
mode, which accepts input fromstdin
and outputs the user’s selection tostdout
. This mode is well-suited for integrating Rofi in pipelines or larger shell scripts.
Example Output:
Rofi displays each of the choices provided in a search-friendly menu. When the user selects an option and presses Enter, that choice is printed to the terminal.
Conclusion:
Rofi is an exceptional utility tool for Linux users looking for flexibility and efficiency in managing applications, windows, and user inputs. Its ability to launch applications, switch between windows, execute commands, and accept custom inputs makes it a valuable tool for both casual and power users, offering improvements in productivity through its simple yet powerful interface. Through the examples provided, users can start leveraging Rofi’s capabilities to enhance their workflows and streamline common tasks.