本文翻译自:How do I clear/delete the current line in terminal?
If I'm using terminal and typing in a line of text for a command, is there a hotkey or any way to clear/delete that line? 如果我正在使用终端并为命令输入一行文本,是否有热键或任何方式来清除/删除该行?
For example, if my current line/command is something really long like: 例如,如果我当前的行/命令非常像:
> git log --graph --all --blah..uh oh i want to cancel and clear this line <cursor is here now>
Is there a hotkey or command to go from the above to: 是否有热键或命令从上面到:
>
? ?
Usually I will press the ↓ key, and if my current line is a brand new one on the history, that will clear it. 通常我会按↓键,如果我的当前行是历史上的全新行,那将清除它。 But if I'm going through my command history via the ↑ key and start editing or using those commands, ↓ will only change the prompt to the next newest command in history, so it doesn't work here unless I press ↓ multiple times. 但是如果我通过↑键浏览我的命令历史并开始编辑或使用这些命令, ↓只会将提示更改为历史记录中的下一个最新命令,因此除非我多次按↓,否则它不起作用。
#1楼
参考:https://stackoom.com/question/ec9Q/如何清除-删除终端中的当前行
#2楼
Just to summarise all the answers: 只是总结一下所有的答案:
- Clean up the line: You can use Ctrl + U to clear up to the beginning. 清理线:您可以使用Ctrl + U清除开头。
- Clean up the line: Ctrl + E Ctrl + U to wipe the current line in the terminal 清理线: Ctrl + E Ctrl + U擦除终端中的当前行
- Clean up the line: Ctrl + A Ctrl + K to wipe the current line in the terminal 清理线: Ctrl + A Ctrl + K擦除终端中的当前行
- Cancel the current command/line: Ctrl + C . 取消当前命令/行: Ctrl + C.
- Recall the deleted command: Ctrl + Y (then Alt + Y ) 调用已删除的命令: Ctrl + Y (然后Alt + Y )
- Go to beginning of the line: Ctrl + A 转到行首: Ctrl + A.
- Go to end of the line: Ctrl + E 转到行尾: Ctrl + E.
- Remove the forward words for example, if you are middle of the command: Ctrl + K 例如,如果您在命令的中间,则删除前向单词: Ctrl + K.
- Remove characters on the left, until the beginning of the word: Ctrl + W 删除左侧的字符,直到单词的开头: Ctrl + W.
- To clear your entire command prompt: Ctrl + L 要清除整个命令提示符: Ctrl + L.
- Toggle between the start of line and current cursor position: Ctrl + XX 在行的开始和当前光标位置之间切换: Ctrl + XX
#3楼
Ctrl + W将清除左侧的单词。
#4楼
CTRL + R and start typing to search for previous commands in history. CTRL + R并开始键入以搜索历史记录中的先前命令。 Will show full lines. 将显示完整的线条。
CTRL + R again to cycle. CTRL + R再次循环。
#5楼
- Ctrl + u : move up to the beginning of your line to a ring buffer Ctrl + u : 向上移动到行的开头到环形缓冲区
- Ctrl + k : move up to the end of your line to a ring buffer Ctrl + k : 向上移动到行的末尾到环形缓冲区
Ctrl + w : move characters and (multiple) words left from your cursor to a ring buffer Ctrl + w : 将光标中剩余的字符和(多个)字移动到环形缓冲区
Ctrl + y : insert last entry from your ring buffer and then you can use Alt + y to rotate through your ring buffer. Ctrl + y :从环形缓冲区中插入最后一个条目,然后您可以使用Alt + y在环形缓冲区中旋转。 Press multiple times to continue to "previous" entry in ring buffer. 按多次以继续环形缓冲区中的“上一个”条目。
#6楼
To delete the whole line no matter where the cursor is, you can use the kill-whole-line
command, but it is unbound by default. 要删除整行,无论光标在哪里,都可以使用kill-whole-line
命令,但默认情况下它是未绑定的。 It can be bound to, for example, Ctrl + Alt + K by inserting 例如,它可以通过插入绑定到Ctrl + Alt + K.
"\e\C-k": kill-whole-line
into your Readline init file (conventionally ~/.inputrc
). 进入你的Readline init文件(通常是~/.inputrc
)。
Various remarks: 各种评论:
To avoid accidentally re-assigning a key sequence that is already in use for something else, you can check all your bindings with
bind -P
. 为避免意外重新分配已用于其他内容的键序列,可以使用bind -P
检查所有绑定。 Check for the suggested binding with 检查建议的绑定bind -P | grep '\\\\e\\\\Ck'
- The Readline init file name is is taken from the shell variable
INPUTRC
. Readline init文件名取自shell变量INPUTRC
。 If it is unset, the default is~/.inputrc
, or (if that doesn't exist)/etc/inputrc
. 如果未设置,则默认为~/.inputrc
,或者(如果不存在)/etc/inputrc
。 Notice that if you have~/.inputrc
,/etc/inputrc
will be ignored. 请注意,如果你有~/.inputrc
,/etc/inputrc
将被忽略。 - To reload your Readline init file, you can use Ctrl + X Ctrl + R . 要重新加载Readline初始化文件,可以使用Ctrl + X Ctrl + R.
- Links to relevant manual sections: 相关手册部分的链接: