In bash, how does one clear the current input?

2019-01-12 15:47发布

Suppose in bash you start writing a command like:

$ rm -rf /foo/bar/really/long/path/here

and then realize you don't want to execute this after all. Is there a way to clear the input with one or two keystrokes?

What I have been doing lately is prepending echo and enclosing the input in quotes (Ctrl+A, echo ", Ctrl+E, ") then hitting enter. Is there a faster way?

标签: bash input
11条回答
神经病院院长
2楼-- · 2019-01-12 16:24

If you are using Bash in vi mode (set it with set -o vi), then press Esc to switch to the normal mode of vi, and type dd to delete the current line!

查看更多
倾城 Initia
3楼-- · 2019-01-12 16:28

There are two options to do this

ctrl+c - this clears the whole line, no matter where the cursor is.

ctrl+u - this clear the line from the position of the cursor until the beginning.

查看更多
兄弟一词,经得起流年.
4楼-- · 2019-01-12 16:28

To delete the current line, try:

Ctrl-X, Ctrl-U

As an alternative you may use:

Esc-D

which requires in ~/.inputrc:

"\ed": kill-whole-line 

see: http://codesnippets.joyent.com/posts/show/1690

查看更多
趁早两清
5楼-- · 2019-01-12 16:33

Ctrl-U, Ctrl-K does the trick as well.

Ctrl-U deletes everything from the beginning of the line up to the cursor, Ctrl-K deletes everything from the cursor to the end of the line. (It is sometimes useful to use only one of them.)

查看更多
别忘想泡老子
6楼-- · 2019-01-12 16:35
  1. Press Ctrl-U to delete everything before the cursor. The deleted command will be stored into a buffer. Press Ctrl-Y to paste the deleted command.

    (Optional: Press End or Ctrl-E to jump to the end of the input first.)

  2. Alternatively, press Ctrl-C to abort what you're typing.

查看更多
登录 后发表回答