I am not sure if this is just a sub scenario of clearing the currently edited line in any Unix/Linux terminal or is the behavior actually different when you are running MySQL command or maybe it depends on the terminal app I am running...
It isn't dependent from your terminal emulator; MySQL is different but not to what you might think; and it's not a subset.
Background
Simple Unix and Linux programs just read()
from standard input for their input. The historical convention is that terminals in Unix and Linux normally operate in what is known colloquially by its historical name "cooked mode" and what is formally and properly known as "canonical mode". In this mode, a part of the kernel known as the line discipline does all of the line editing, and the program only receives the final edited line from read()
when the user sends the end-of-line character (normally LF
) from the terminal.
In canonical mode, there are a whole load of special characters that the line discipline recognizes and acts specially upon. The end-of-line character is but one. Another is kill character. This "kills" the (line discipline's) editing buffer. Since the cursor can only be at the end of the line, the line discipline's editing facility not having mechanisms for moving a cursor within the line being edited, this kills the entire line.
Back in the 1980s and 1990s, people wanted a better TUI than this. They wanted the ability to move the cursor within the line. They wanted insert/overstrike modes. They wanted command history.
Thus along came GNU Readline and its ilk. GNU Readline is a library that is used by a number of popular programs including the Bourne Again shell and the mysql
command. Readline switches the terminal into what is colloquially known by the historical name "raw mode" (the formal name being "non-canonical mode"). The program read()
s the characters from the terminal as they are sent, without any intervening processing by the line discipline. Readline does all of the editing, outwith the kernel.
Nowadays, terminals spend quite a lot of their time in non-canonical mode. They do when modern shells are reading their input. (Not all modern shells use Readline. But they all have something which uses non-canonical mode. The Z Shell has its own mechanism, ZLE. BSD-licensed programs use the BSD libedit
library, which Christos Zoulas took from the Tenex C Shell for 4.4BSD, or Editline.) They do when programs that use ncurses
are active. Canonical mode is predominantly encountered by people when they do things like accidentally grep
the terminal, or use cat
to create a file from terminal input, and is heading towards being the exception more than the norm.
In the usual case, Readline reads all of the special keys from the line discipline and creates key bindings (mappings from character sequences to editing actions) that match the special characters to actions that do the same as what the line discipline would have done in canonical mode.
You can see what the line discipline thinks that the special characters are with the stty -a
command. In particular, it will tell you what your kill
special character is. It's commonly Control+U the ASCII NAK
character. Symbolically, this special character is known as VKILL
, from the name of the constant used to set it in C and C++ programs.
Usually, Readline (or libedit
, or ZLE, or Editline) will have a binding from the line discipline's VKILL
character to one of its "kill" actions. In fact, unless Readline's bind-tty-special-characters
variable is off, it will redefine such a binding every time that it is called to read a fresh line of input. It's actually hard to turn this off.
There are three caveats.
- These libraries are libraries. They have various program-controlled tuning knobs available, and so programs can employ them in subtly different ways.
- The
mysql
command can be compiled with either GNU Readline or libedit
. If it's compiled with libedit
then the conventional key binding is from VKILL
to the em-kill-line
action. This kills the entire line and does what you want.
- If
mysql
is compiled with GNU Readline, the VKILL
special character is conventionally bound to unix-line-discard
. This doesn't do exactly what you want, however, because it only erases from the current cursor position to the beginning of the line. Of course, this is indistinguishable from the line discipline's action in canonical mode if one only sticks to what that can do, because the cursor position is always the end of the line.
Answer
- If
mysql
was built with Readline:
- The conventional Control+U (or whatever
stty -a
says to be your kill
character) will get you Readline's unix-line-discard
action. This doesn't do exactly what you want, because it only erases from the current cursor position to the beginning of the line.
- The action that you actually want is Readline's
kill-whole-line
, which deletes the whole line, regardless of the cursor position.
- Unfortunately,
kill-whole-line
isn't bound to your VKILL
character, or indeed to any character by default; and Readline will reset the binding of VKILL
back to unix-line-discard
every time that you start a fresh line of input, unless you set bind-tty-special-chars
off and lose the automatic mirroring of what special characters are set in the line discipline by stty
.
- So bind some other character sequence of your choosing to
kill-whole-line
in your ~/.inputrc
file, unless you are happy with the effects of bind-tty-special-chars
being off.
- If
mysql
was built with libedit
:
- As long as you are in "emacs" mode, the conventional Control+U (or whatever
stty -a
says to be your kill
character) will get you libedit
's em-kill-line
action ("emacs kill line"). This does exactly what you want.
- You don't have to do anything in
libedit
's ~/.editrc
file.
- In "vi" mode, your
VKILL
character is bound to libedit
's vi-kill-line-prev
action ("vi kill line up to cursor").
None of this involves your terminal emulator in any way.
Further reading
1what happens if you press Ctrl+U ? – Petter H – 2014-02-09T08:47:30.867
it trims to the beginning of the line - which is not bad it means I can press end and then CRTL+U and get what I want. If I don't get a single key combination that does this regardless of where in the line you are then you can post as an answer and I will accept. Thanks. – epeleg – 2014-02-09T09:02:44.233