bash(或任何外壳,就此而言)是否具有“覆盖”模式?

Wil*_*n F 4 command-line bash shortcut-keys bash-history

我正在(例如)一组文件上运行类似的命令。举个例子,假设我正在做这样的事情(这不是我在做的,这只是一个例子):

> cat path/to/dir/file01.txt
  [file contents]
> another-command path/to/dir/file01.txt
  [more output]
> cat path2/to/dir/file02.txt
  [file contents, from which I can tell I should do something different]
> different-command path2/to/dir/file02.txt
  [yet more output]
> cat path3/to/dir/file03.txt
  [file contents]
> another-command path3/to/dir/file03.txt
  [output]
Run Code Online (Sandbox Code Playgroud)

等等。

如果在使用键返回上一个命令后,我可以覆盖非重复文本会很方便吗?比如文件名,或者路径的公共部分——而不必删除它并重新输入它。

有没有办法做到这一点?

mur*_*uru 5

不是您直接要求的,但您可以使用各种形式的历史交互来简化您的任务:

$ cat path/to/dir/file01.txt
cat: path/to/dir/file01.txt: No such file or directory
$ different-command !$
different-command path/to/dir/file01.txt
bash: different-command: command not found
$ cat !$:s/1/2/
cat path/to/dir/file02.txt
cat: path/to/dir/file02.txt: No such file or directory
$ ^2^3
cat path/to/dir/file03.txt
cat: path/to/dir/file03.txt: No such file or directory
$ !-3:s/1/3/
different-command path/to/dir/file03.txt
bash: different-command: command not found
$ !diff:s/3/4/
different-command path/to/dir/file04.txt
bash: different-command: command not found
Run Code Online (Sandbox Code Playgroud)

忽略错误,每次我使用历史交互(!$!$:s/1/2^2^3)时,您都可以看到bash它是如何扩展的。

  • !$ - 上一条命令的最后一个字
  • :s/1/2- 替换所选单词中第一次出现的1with 2(在这种情况下,!$又是)。
  • ^2^3-取代的第一次出现23在整个前一命令。
  • !-3 - 运行倒数第三个命令。
  • !diff- 运行以diff.


col*_*mar 5

您可以将键绑定到$HOME/.inputrc.

"code": overwrite-mode
Run Code Online (Sandbox Code Playgroud)

要发现一个特定的按键生成的代码CtrlV在一起,然后打的关键。例如,当您按下Ins(插入)键时,您会看到^[[2~。请注意,这^[只是 ESC 的屏幕表示,用\efor readline表示。因此,您应该将以下行添加到$HOME/.inputrc

"\e[2~": overwrite-mode
Run Code Online (Sandbox Code Playgroud)

并重新启动bash。

  • 是的,只有一些有关绑定任何选定密钥的附加详细信息。 (2认同)