How to store formed command line without running it in bash or terminal?

Posted on

Problem :

For example, I have formed a long complicated command that will improve the world and lower an entropy in the Universe. But then (huh!) I see that I forgot to make a dir required. I want to store my command, run mkdir and then restore my command.

Assume doing it in a raw tty.

Is there a way to do so?

Solution :

On your command-line press Ctrlu (it stores it in the kill-ring), issue the other one mkdir for example, and then press Ctrly.

Use set -o vi command or set -o emacs (typically one would store this in .bashrc for convenience). This enables a vi-style (or emacs type) line editing interface.

This provides you with a command line vi editor. You can search for previous commands, edit commands, etc.

Examples:

Use ESC key to enter vi command mode, then

$ /search-string

Use UP and DOWN keys, (or j and k) to list found commands

$ long-command-you-want-to-edit

Hit ESC key
type vi
Then edit the long command in a vi editor.

Leave a Reply

Your email address will not be published.