如何在命令行上的现有BASH字符串中插入换行符

eri*_*icj 6 linux bash

假设我在命令行上输入并执行了一个长BASH命令.现在我想分开它.所以有了历史,我又有了长命令,但是现在我不能给Enter输入换行符了.你是怎样做的?

kam*_*uel 12

您可以使用两个快捷方式来做到这一点ctrl + k和ctrl + y:

echo "some command" && echo "some other long command"
Run Code Online (Sandbox Code Playgroud)

现在将光标移动到某处(在我的示例中,光标标记为>):

echo "some command" && > echo "some other command"
Run Code Online (Sandbox Code Playgroud)

现在按ctrl + k- 这将删除光标后的所有内容:

echo "some command" && >
Run Code Online (Sandbox Code Playgroud)

现在放\(反斜杠)并按enter:

echo "some command" && \
>
Run Code Online (Sandbox Code Playgroud)

现在粘贴您之前切割过的部分ctrl + y:

echo "some command" && \
echo "some other long command"
Run Code Online (Sandbox Code Playgroud)

编辑:要在长命令中更容易移动,您可以使用快捷方式:

  • alt + b-向后移动一个字(在Mac OS X: ESC + b)
  • alt + f-移动一个字转发(在Mac OS X: ESC + f)

超解决方案

您还可以使用Ctrl-x + Ctrl-e(两个快捷方式,一个接一个)在编辑器中打开当前行.然后将其编辑为常规文本文件,保存并退出,然后执行编辑的命令.

如果要选择使用哪个编辑器,只需设置EDITOR环境变量即可.

  • @devnull - 我做到了,我也发现了`ctrl-x``ctrl-e`我想我的生活只是改变了一点;) (2认同)