How to interactively extend a bang command in bash?

In shells such as zsh and bash, there are bang commands that start with an exclamation point and expand to elements in the user's history.

To get the last argument of the last running command, you can use !$ , For example.

 $ echo one two three $ echo !$ !$ !$ > three three three 

In zsh, you can deploy these bang commands interactively:

 touch foo bar ls !$<TAB> 

!$ will be expanded to foo inline.

This is very useful because it often made it difficult for me to make a mistake: I press tab to expand and make sure that I understand correctly and C-/ to cancel the extension when I'm sure.

Is there a parameter in bash for interactive extension of bang commands?

How about extending subshells and common variables in this regard (i.e. from echo $(uname)<TAB> to echo Linux and echo $SHELL<TAB> to echo /bin/bash ).

+5
source share
1 answer

There are several options you can use to expand your story. One of them is the modifier :p , which prints an extended command instead of executing it.

 $ echo foo $ !!:p echo foo 

Another is to use the histverify parameter, which puts the result of history expansion into the shell buffer for editing instead of executing it immediately.

 $ shopt -s histverify $ echo foo foo $ !! $ echo foo 

If you are happy with this command, just press Enter again to execute it as if you had just typed it.

By default, the Readline history-expand-line command is bound to M-^ ( Alt-^ or Esc-^ , depending on what your terminal emulator sends as a meta key), which extends any history extensions on the current command line.

There is also a general Readline shell-expand-line command (attached to MCe by default) that expands everything on the command line, just like a shell after hitting Enter, but just before executing it.

+8
source

Source: https://habr.com/ru/post/1234858/


All Articles