How to Quickly Type a Command Again in the Terminal

All the Command-Line Keyboard Shortcuts Y'all Demand to Know

Photo by Juan Gomez on Unsplash

Whether y'all are an experienced command-liner looking to larn new shortcuts, or y'all avoid using the last because you hate using the arrows to navigate through long commands one character at a time just to modify a parameter at the end or the beginning of the line, this commodity is for you lot.

I have compiled a list of, what I consider, the near useful keyboard shortcuts you can use in your final of choice.

As for the trounce, I utilise Zsh but almost of, if not all, the shortcuts also use to Bash. And at the end, I also show how you tin can create your own custom shortcuts!

Beat Line Editor

A line editor is a text editor that allows users to edit lines that correspond commands. It's the first office of the crush you probably interacted with, as it handles all the commands you type.

Every shell provides some kind of line editing capabilities, including entering and deleting characters, word navigation, deleting lines, etc.

There are ii modes of operation available to the Zsh (and Bash) line editor, one (the default) based on Emacs, and the other based on six. I recommend starting with Emacs mode, mastering it, and after trying the vi style.

I won't get downwards to the nitty-gritty of how line editors work, if you are interested, you tin notice more information nigh zle (Zsh line editor) and about Readline (used by Fustigate).

Switching modes

Although Emacs mode is the default, if you lot have your $EDITOR set to half-dozen (or any editor that contain "six" such every bit vim), 6 mode will exist enabled.

To switch to Emacs style, run (or add to your .rc file):

            # zsh
bindkey -e
# fustigate
gear up -o emacs

To switch to vi mode, run (or add to your .rc file):

            # zsh
bindkey -5
# bash
set -o six

To list all the current central bindings, run:

            # zsh
bindkey
# bash
bind -P

Both Zsh and Bash support keymaps, so you can accept a dissimilar fix of key bindings active at different times.

Emacs Mode

Moving the cursor

  • CTRL-A/Domicile: Move to the beginning of a line.
  • CTRL-E/End: Move to the end of a line.
  • CTRL-B/LEFT: Move left 1 character.
  • CTRL-F/Right: Motion right ane character.
  • ALT-B/CTRL-LEFT: Move left ane word.
  • ALT-F/CTRL-RIGHT: Move right one word.
  • CTRL-Xx: Hold CTRL and press 10 twice to move the cursor to the first of the line, and hold CTRL and press Ten twice over again to motion the cursor back.

Editing text

  • CTRL-U: Cut all the characters.
  • CTRL-1000: Cutting the characters to the right of the cursor.
  • CTRL-H/BACKSPACE/DELETE (MACOS): Delete one grapheme to the left.
  • CTRL-D/DELETE/FN DELETE (MACOS): Delete ane character to the correct.
  • CTRL-W: Cut one word to the left.
  • ALT-D: Cutting one word to the right.
  • CTRL-Y: Paste the characters previously cutting.
  • CTRL-_: Disengage the last edit.
  • CTRL-XE: Open the $EDITOR to edit the line.
  • ALT-U: Change one word to the right to uppercase.
  • ALT-Fifty: Change ane discussion to the right to lowercase.

Command completion

  • TAB: Try shell expansion on the electric current word. If that fails, effort completion.
            gi<TAB>     # git
ls *<TAB> # ls folder1 folder2 file3
  • CTRL-G: List the expansion of the current word.

Managing the screen

  • CTRL-L: Clear screen (just like clear).
  • CTRL-S: Stop screen output. Useful for preventing processes from spamming the stdout.
  • CTRL-Q: Resume screen output.

Managing processes

  • CTRL-C: Terminate the foreground process. (As well tin can be used to delete the whole line.)
  • CTRL-Z: Append the foreground procedure (utilize fg and bg to resume).
  • CTRL-D: Go out shell (just like go out).

Accessing Command History

  • CTRL-R: Search the command history. Accept with ENTER/RETURN, arrest with CTRL-Thou.
  • CTRL-P/Upward: The previous command in history.
  • CTRL-Northward/DOWN: The next control in history.

Vi Style

Just like any vi-based editor, vi mode operates in either insert mode (keys produce text on the screen) or normal fashion (keys represent editing commands).

Commands

To switch to normal mode, press ESC or CTRL-[, past default, in that location won't be any visual indication almost which mode is currently active. If you want to switch back to insert style, just like vi, printing a (or A, i, I, depending on where you lot desire the cursor to exist).

For more than commands, see this cheat sail.

So, in vi mode, if you want to delete the whole line, y'all printing ESC (to switch to control mode), dd (to delete the line), and a (to switch back to insert mode).

Depending on your shell, or if you accept any plugins, some of the Emacs-based shortcuts might piece of work when in insert mode.

Why non both?

If you, similar me, like both modes, at that place'southward a actually good Zsh plugin that not only enhances vi fashion with more features like mode visual indicator and support for surround bindings, but also makes almost of the Emacs key bindings available.

Custom Keyboard Shortcuts

It's easy to add your own keyboard shortcuts. If you are not certain what code represents a fundamental combination (e.m ALT-Z is ^[z), first press CTRL-Five followed by the fundamental combination.

Line editor commands

For a list of Zsh-bindable commands, visit this site, for Bash'due south, visit this site.

For example, to bind undo to ALT-Z:

            # zsh
bindkey '^[z' undo
# bash
bind '"^[z":disengage'

Macros

You can too demark strings:

            # zsh
bindkey -southward '^[[24~' 'You`ve just pressed F12'
# bash
bind '"\e[24~":" You`ve merely pressed F12"'

Vanquish commands

You can as well ascertain cardinal bindings to execute shell commands without affecting the current command line.

To bind printing the current directory to CTRL-P:

            # bash
demark -x '"\C-p":"pwd"'

With Zsh, it's a flake more than complicated just way more flexible. The mode we achieve that is via Widgets:

Thanks to Benoît de Chezelles, here's a better version that shows the result below the command line:

Decision

Did I miss whatsoever useful shortcuts? Let me know.

Thanks for reading — I promise you lot learned something new today.

Take care and I'll meet you next fourth dimension.

grieveabloome.blogspot.com

Source: https://betterprogramming.pub/boost-your-command-line-productivity-with-keyboard-shortcuts-4de2f6cbd069

Related Posts

0 Response to "How to Quickly Type a Command Again in the Terminal"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel