PDA

View Full Version : How to clear command line



neo_in_matrix
28th November 2008, 10:43 PM
In Windows command prompt, I can press ESC to clear what I already typed on the command line.

What is the equivalent key in Terminal?

stefan1975
28th November 2008, 10:49 PM
<backspace>

or

<ctrl><c>

or

<#>

pwca
28th November 2008, 11:44 PM
You can also type



clear

neo_in_matrix
28th November 2008, 11:54 PM
I think ^+c is nearest to my need, but not exactly.

In Windows command prompt, pressing ESC will clear all typed chars. Say after I typing:

C:\>move a.file b.file

I suddenly don't want to run this command, so I press ESC, all the chars I typed are gone, and I still at:

C:\>

without jumping to a new line of C:\> prompt, like this;

C:\>move a.file b.file
C:\>

marko
29th November 2008, 12:59 AM
Ctrl+U will do what you want (clear the line)

"clear" clears the entire window

neo_in_matrix
29th November 2008, 01:17 AM
^+U works! Great! Thanks.

stevea
29th November 2008, 02:13 AM
^+U works! Great! Thanks.

ctrl-U is fine,but study the emacs compatible line editing (many Linux utils use it).

ctrl-A ctrl-K does the job too

ctrl-A - goto beginning of line
esc-b - back one word
esc-f - forward one word
ctrl-d - delete next char
backspace - delete previous char
esc-d - delete next word
esc-backspace - delete previous word
esc-u - up-case next word
esc-l - lower-case one word
ctrl-K - kill to end of line
ctrl-y - yank the killed line buffer back

ctrl-r string - search back thru command history for command with string

hundreds more

RupertPupkin
29th November 2008, 03:19 AM
ctrl-U is fine,but study the emacs compatible line editing (many Linux utils use it).

ctrl-A ctrl-K does the job too
Well, the problem with this is that it assumes that everyone is using the emacs key bindings in bash. I use those myself, but there are (surprisingly) quite a few people who prefer the vi key bindings (set -o vi). The nice thing about ctrl-u is that it works for both the emacs and vi key bindings in bash, while ctrl-a ctrl-k only works in emacs mode (ctrl-x backspace is another way of doing that).

stevea
29th November 2008, 04:18 AM
Well, the problem with this is that it assumes that everyone is using the emacs key bindings in bash.

Emacs is the DEFAULT bash lineedit mode. In a simply reply it's certainly not a "problem" to not enumeration all the options of a complex command.


I use those myself, but there are (surprisingly) quite a few people who prefer the vi key bindings (set -o vi). The nice thing about ctrl-u is that it works for both the emacs and vi key bindings in bash, while ctrl-a ctrl-k only works in emacs mode (ctrl-x backspace is another way of doing that).

Yeah well - to delve a little deeper.

The ctrl-u is one of the handful of characters that is handled by the tty driver. You can remap these characters with an ioctl. "stty -a" will show the whole control character set.
[stevea@nidula ~]# stty -a
speed 38400 baud; rows 24; columns 127; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = M-^?; eol2 = M-^?; swtch = M-^?; start = ^Q; stop = ^S; susp = ^Z;
rprnt = ^R; werase = ^W; lnext = ^V; flush = ^O; min = 1; time = 0;
...

The "man stty" output explains the meanings of the various characters and how to remap these. Yes these work for all interactive devices that use the generic tty kernel code.

==
Many programs use "readline(3)" which accepts a load of emacs or vi style line editing commands. Bash has its own flavor of readline with more features including history recall.

The topic of which editor is best is a religious issue, but it's fair to say that emacs is an editor lacking no feature. Vi was was originally written by Bill Joy of SunMS fame as a simple editor that would be common across all Unix installs. Emacs is difficult to learn due to the vast number of commands, but once learned is terse, nimble and shockingly powerful. Vi seems to me like trying to playing piano while wearing boxing gloves. It seems to share a lot of heritage from the ancient Dec teco editor philosophy including it's modal design; but lacks it powerful macro features; basic like a box of rocks.

Anyway for basic line editing either will do - but understanding the line-editing capabilities of bash will make life a lot more pleasant.

scottro
29th November 2008, 04:43 AM
I'm a vi user--nothing against emacs, but after playing with it, I decided to learn a simpler operating system. :) However, I use the emacs keybindings for command line.
I can understand vi users changing it to vi, but I think that most people, even vi users, get used to the emacs shell keys and leave them alone. Every time I've done the set -o vi I forget that I did it, and then the command line doesn't act the way I'm expecting it to act.

Habit I guess--when I first began playing with Linux, I tended to go with defaults, and learned the basic emacs keystrokes for the command line, so I stick with it.

stevea
29th November 2008, 05:23 AM
I'm a vi user--nothing against emacs, but after playing with it, I decided to learn a simpler operating system. :)

What - you didn't like M-x vi-more (vi mode) in emacs ? ;)



It's now the GNU Emacs of all terminal emulators.
(Linus Torvalds, regarding the fact that Linux started off as a terminal emulator.)

stefan1975
29th November 2008, 07:17 AM
Well, the problem with this is that it assumes that everyone is using the emacs key bindings in bash. I use those myself, but there are (surprisingly) quite a few people who prefer the vi key bindings (set -o vi). The nice thing about ctrl-u is that it works for both the emacs and vi key bindings in bash, while ctrl-a ctrl-k only works in emacs mode (ctrl-x backspace is another way of doing that).

i even use vi in windows. for a text editor and to make me coffee. JAY! for 'vi'.

stefan

stefan1975
29th November 2008, 07:19 AM
Ctrl+U will do what you want (clear the line)

"clear" clears the entire window

good one indeed! clear would not even work in his case. her would have to ^u first because there is already data on host line.

stefan