键入命令时查找和替换

键入命令时查找和替换

我在 BASH 中输入了一个长命令,但就在我输入命令时,我发现命令或参数拼写错误。我希望使用就地搜索将光标转到我拼写错误的参数或命令,而不是按箭头键转到特定命令或参数。

例如

ls *.txt | grep -e 'foo' >> list_of_text_files_containing_foo.txt

并且我想通过在命令中查找并替换为 来将foogrep 中的更改为,而无需按箭头键。有没有办法将所有出现的 更改为。barfoobarfoobar

答案1

您可能对最后一条命令中的 bash 快速替换感兴趣:

!!:gs/old/new

例子:

$ ls *.txt | grep -e 'foo' >> list_of_text_files_containing_foo.txt
$ !!:gs/foo/bar
ls *.txt | grep -e 'bar' >> list_of_text_files_containing_bar.txt

有关 bash 替换的更多信息,请man bash从第 3630 行开始阅读。

答案2

我认为您无法进行就地搜索,而是学习键盘快捷键来处理 Bash。

Ctrl + L   Clear the Screen, similar to the clear command
 Ctrl + u   Cut/delete the line before the cursor position.

  Alt + Del Delete the Word before the cursor.
  Alt + d   Delete the Word after the cursor.
 Ctrl + d   Delete character under the cursor
 Ctrl + h   Delete character before the cursor (Backspace)
 Ctrl + w   Cut the Word before the cursor to the clipboard.
 Ctrl + k   Cut the Line after the cursor to the clipboard.
  Alt + t   Swap current word with previous
 Ctrl + t   Swap the last two characters before the cursor (typo).
 Esc  + t   Swap the last two words before the cursor.
 ctrl + y   Paste the last thing to be cut (yank)
  Alt + u   UPPER capitalize every character from the cursor to the end of the current word.
  Alt + l   Lower the case of every character from the cursor to the end of the current word.
  Alt + c   Capitalize the character under the cursor and move to the end of the word.
  Alt + r   Cancel the changes and put back the line as it was in the history (revert).
 ctrl + _   Undo

参考 :关联

相关内容