2 ms·
One trick I use all the time: You're typing a long command, then before running it you remember you have to do some stuff first. Instead of Ctrl-C to cancel it
by hikarudo 6mo ago
One trick I use all the time:
You're typing a long command, then before running it you remember you have to do some stuff first. Instead of Ctrl-C to cancel it, you push it to history in a disabled form.
Prepend the line with # to comment it, run the commented line so it gets added to history, do whatever it is you remembered, then up arrow to retrieve the first command.
$ long_command
<Home, #>
$ #long_command
<Enter>
$ stuff_1
$ stuff_2
<Up arrow a few times>
$ #long_command
<home, del>
$ long_command
- fragmede 6mo agoFwiw, in Bash, alt-shift-3 will prepend the current command with # and start a new command.
- j4cobgarby 6mo agoMore generally, it's alt-#. On an ISO (e.g. UK) keyboard layout, shift-3 isn't a hash.
- dotancohen 6mo agoThank you so much! I do this constantly. For me it was Ctrl-A Shift-3 Enter - two keystrokes too many.
- gvalkov 6mo agoIn zsh you can bind "push-line-or-edit". In bash and all readline programs, you can approximate it with C-u followed by C-y (i.e. cut and paste). My history is still full of '#' and ':' (csh trauma) prefixed command-lines like you described though ...
- idoubtit 6mo agoYou missed an easier alternative that was in the article: ctrl-u saves and clears the current line, then you can input new commands, then use ctrl-y to yank the saved command. With zsh, I prefer to use alt-q which does this automatically (store the current line, display a new prompt, then, after the new command is sent, restore the stored line). It can also stack the paused commands, e.g.: $ cp foo/bar dest/ <alt-q> $ wcurl -o foo/bar "$URL" <alt-q> $ mkdir foo <enter> <enter> <enter>
- philsnow 6mo agoWhen you're killing (C-u, C-k, C-w, etc) + yanking (C-y), you can also use yank-pop (bound to M-y in bash and zsh by default) to replace the thing you just yanked with the thing you had killed before it. $ asdf<C-w> $ # now kill ring is ["asdf"] $ qwerty<C-a><C-k> $ # now kill ring is ["qwerty", "asdf"] $ <C-y> # "yank", pastes the thing at the top of the kill ring $ qwerty<M-y> # "yank-pop", replaces the thing just yanked with the next # thing on the ring, and rotates the ring until the next yank $ asdf
- ralgozino 6mo agoCtrl+q in zsh is much better, I don't remember if bash has it. You write a command, you remember that you need to do something else first, press ctrl+q/the lines gets cleared, write a different command and after you press enter to run it the old command appears again :)