如何清除/删除终端中的当前行?

本文翻译自: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: 相关手册部分的链接:
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在终端删除一行,可以使用以下快捷键: 1. Ctrl + U:删除光标所在位置到首的所有内容。 2. Ctrl + K:删除光标所在位置到尾的所有内容。 3. Ctrl + W:删除光标面的一个单词。 4. Ctrl + L:清空屏幕,相当于执 clear 命令。 如果要删除当前,可以使用以下方法: 1. 输入 Ctrl + U 清空当前。 2. 按下 Enter 键跳到下一行。 这样就可以删除当前了。 ### 回答2: 在Linux终端,可以使用以下方法删除一行: 1. 使用快捷键:按下Ctrl + U组合键,这将删除光标当前位置到首的所有字符,包括上一行内容。 2. 使用清除命令:输入clear或者Ctrl + L来清除屏幕上的所有内容,这将清除整个终端窗口的显示。 3. 使用回退符号:按下Ctrl + H来删除光标之的一个字符。可以重复按下Ctrl + H来逐渐删除一行内容,直到删除完整为止。 4. 使用命令历史:输入history并按下Enter键来查看之命令的历史记录。找到上一行的命令,并使用Backspace键或者Ctrl + H来逐渐删除命令内容,直到删除完整为止。 这些方法可以根据具体情况选择使用。如果只是需要删除一行的某个字符,可以使用Ctrl + H或者Ctrl + W删除光标之的字符或者单词。如果需要清除整个终端窗口的显示,可以使用clear或者Ctrl + L。如果需要删除内容,可以使用Ctrl + U或者通过命令历史逐渐删除。 ### 回答3: 在Linux终端删除一行有几种方法。以下是其一种常用的方法: 1. Ctrl+U:在终端内按下Ctrl+U组合键可以删除光标位置到首的所有字符,包括光标所在位置的字符。这个组合键可以用于删除命令。 除了Ctrl+U外,还有其他一些删除命令的快捷键: 2. Ctrl+W:在终端内按下Ctrl+W组合键可以删除光标位置到当前单词开头的字符。 3. Ctrl+H或Backspace:在终端内按下Ctrl+H组合键或Backspace键可以删除光标面的一个字符。 4. 使用命令编辑器:在终端内按下Ctrl+X+E组合键,可以在定义的默认编辑器里编辑当前命令。例如,使用Vi编辑器可以按下Ctrl+X+E进入编辑模式,然后删除一行内容。 以上方法,Ctrl+U是最常用的方法之一,它可以一次性删除命令,方便快捷。其他方法则可以根据个人喜好和习惯选择使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值