Mastering Zsh Keyboard Shortcuts for Efficient Shell Navigation and Editing
Efficiency and speed are essential when working in a shell environment. Zsh (Z Shell), a powerful alternative to the standard Bash shell, offers a plethora of keyboard shortcuts that can significantly enhance your productivity. This article will explore a comprehensive list of Zsh keyboard shortcuts for efficient navigation, editing, process management, history retrieval, and mode switching.
Shell Keybindings:
Navigation
- Alt + f/b — Move cursor to previous/next word: Use the Alt key along with the f or b key to navigate the cursor to the previous or next word, respectively. This shortcut allows you to jump quickly through commands or text.
- Ctrl + a/e — Move cursor to beginning/end of command: Press Ctrl along with the key to move the cursor to the beginning of the command line, or Ctrl with the e key to move it to the end. This is handy when editing a command or accessing a specific part quickly.
- Ctrl + xx — Toggle between the start of the line and the current cursor position: This shortcut lets you switch between the start of the line and the current cursor position with each successive press of Ctrl + xx. It allows for easy navigation and editing within a command.
Editing ✏
- Ctrl + x,e — Open command in editor: Press Ctrl along with the x and e keys simultaneously to open the current command line in your default editor. This feature comes in handy when you need to make extensive modifications to a command.
- Ctrl + k — Cut till end: Use Ctrl + k to cut (delete) text from the current cursor position until the end of the line. This shortcut is useful when you want to remove a portion of a command quickly.
- Ctrl + u — Delete whole line (zsh)/cut until beginning (bash): In Zsh, Ctrl + u deletes the entire line, while in Bash, it cuts (deletes) text from the current cursor position to the beginning of the line. It’s a convenient way to clear the command line entirely or remove unnecessary input.
- Alt + w — Delete until beginning (zsh): With Alt + w, you can delete text from the current cursor position to the beginning of the word. It’s useful when you want to delete a specific portion of a word quickly.
- Alt + l/u — Lowercase/Uppercase word: Press Alt along with the l or u key to convert the current word to lowercase or uppercase, respectively. This shortcut simplifies text transformations in commands.
- Alt + c — Capitalize Word: Alt + c capitalizes the first character of the current word. It’s useful for quickly adjusting the capitalization of a word in a command.
- Ctrl + w — Cut previous word: This shortcut deletes the previous word, starting from the current cursor position. It’s handy when you want to remove a portion of the command quickly.
- Alt + Del — Delete previous word: Alt + Del deletes the word preceding the current cursor position. It’s an alternative shortcut for deleting the previous word.
- Alt + d — Delete next word: Use Alt + d to delete the word following the current cursor position. It allows for swift removal of unnecessary text in a command.
- Alt + . or !$ — Previous command’s last argument: Alt + . or !$ retrieves the last argument from the previous command. It saves time when you need to reuse or reference the last argument frequently.
- !* — All arguments of the previous command: Typing !* retrieves all the arguments from the previous command. It’s useful when you want to reuse multiple arguments from a previous command.
- Alt + t — Swap current word with previous: Alt + t swaps the current word with the previous word. It helps in quickly rearranging words in a command.
- Ctrl + t — Swap the last two characters before the cursor (typo): Press Ctrl + t to swap the last two characters before the cursor. It’s convenient for correcting typing mistakes efficiently.
- Esc + t — Swap the last two words before the cursor: This shortcut swaps the position of the last two words before the cursor. It simplifies rearranging words in a command.
- Ctrl + y — Paste: Use Ctrl + y to paste (yank) previously cut or copied text back into the command line. It’s useful when you need to reinsert a portion of text that you previously deleted.
- Ctrl + _ — Undo: Press Ctrl + _ to undo the most recent change to the command line. This shortcut is handy when you want to revert an unintended modification.
- Alt + r — Cancel the changes, revert: Alt + r cancels any changes made to the current command line and reverts it back to the last saved state. It’s helpful when you want to discard modifications quickly.
Process
- Ctrl + l — Clear screen: Pressing Ctrl + l clears the screen, providing a clean workspace. It’s equivalent to the “clear” command.
- Ctrl + c — Interrupt/Kill: Use Ctrl + c to send an interrupt signal to the currently running command, effectively terminating its execution.
- Ctrl + d — Close Current Shell: Press Ctrl + d to close the current shell session. It’s equivalent to typing “exit” or “logout.”
- Ctrl + z — Background/Foreground job: Ctrl + z suspends the currently running foreground job and moves it to the background. You can later bring it back to the foreground using the “fg” command.
History
- Ctrl + r — History search: Press Ctrl + r and start typing to search through your command history interactively. It allows you to quickly find and reuse previously executed commands.
- Ctrl + s — Go back to the next most recent command: Ctrl + s retrieves the next most recent command from your history. It’s useful when you want to cycle through recently executed commands.
- ^abc^def — Run previous command, replacing abc with def: Typing ^abc^def replaces the first occurrence of “abc” in the previous command with “def” and executes the modified command.
Modes
- Ctrl + x,v — vi modes (zsh): Press Ctrl + x followed by v to switch to vi mode in Zsh. This mode allows you to use vi-like keybindings for command-line editing.
- set -o vi — Vi mode: Using the “set -o vi” command in your shell enables the vi mode for the current session. It offers powerful editing capabilities similar to the vi text editor.
Mastering keyboard shortcuts in Zsh can significantly improve your productivity and efficiency when working in a shell environment. The shortcuts provided in this article cover various aspects, including navigation, editing, process management, history retrieval, and mode switching. By incorporating these shortcuts into your workflow, you’ll be able to streamline your shell interactions and accomplish tasks more swiftly.
source : https://gist.github.com/2KAbhishek/9c6d607e160b0439a186d4fbd1bd81df