As described in this answer is possible to map keystrokes to commands in a terminal. And to do this, there is a specific bash syntax for describes each key, as \e[11~ for F1 or Control-o for ControlO
How not everyone is deductible, I would like to find a way to discover each key associated string. If I just press it in terminal nothing happens for most of non-alphanumeric keys
I think you can alternatively install expect, start autoexpect and see what are the codes for your key strokes in generated file.
Related
I don't understand how to create custom escape sequence for Ctrl+Shift+S like in this question
My problem is that I want to use Ctrl+Shift+S in emacs no-window mode under MacOS terminal. Currently I'm trying to bind this escape sequence to Ctrl+Shift+F12 but I have troubles creating that escape sequence. Maybe there is a batter way to use complex (Ctrl-Shift+anykey) bindings in MacOS terminal with emacs in no-window mode?
You're probably assuming that F12 is a character. It is not. Conventionally, function keys send a sequence of characters.
In Terminal's preferences screen, you can review (most of) the key bindings:
You can also add a key definition (after deciding what characters it ought to send), and associate the Shift and Control modifiers with that key:
For the sake of example, I used the sequence that xterm would send. However, Terminal is not the same as xterm, and you may find other sequences more interesting.
Regarding the related question "What is the correct escape code for control/shift/s": there is no correct escape code because terminals (unless specially configured as illustrated above) do not distinguish between shifted- and unshifted-control characters. To determine what is "correct", you will have to examine your emacs bindings to see what you should send to make it recognize the function you are asking about.
As far as I can see, in Konsole both CTRL+ENTER and ENTER look like byte 13 in the stdin of the running app. But when I run mc which obviously uses ncurses lib, pressing CTRL+ENTER inserts the name of the file into the command line while ENTER opens the file. How is it implemented? I tried to look into sources, but they are totally unreadable for me.
mc (midnight commander) doesn't use ncurses for input, but may use it for output. Essentially it is looking for specific character sequences.
mc makes little use of the terminfo database, essentially only to check on the xterm mouse- and alternate-screen features.
In principle, it could read the user-defined capabilities from the ncurses terminfo database (see for example ncurses trapping extended keys (Control-left, Shift-Function etc)), but it does not.
Since you are looking at sources, see the source of mc, in lib/tty/key.c, which contains tables which mc uses as a set of predefined keys. Doing it that way "works" when mc is configured to use slang, for instance, though it has the drawback that it's hardcoded and may not actually match your terminal.
However - as I said, mc does its own input. Further down in key.c, you may see a chunk in get_modifier() ifdef'd with HAVE_TEXTMODE_X11_SUPPORT. Inside that is a call which ultimately goes to XQueryPointer, which mc uses to find the current state of the modifier keys — if it happens to be running in an X display, and if the feature is enabled. You are probably seeing that.
In vim, to find a character you can use 'f' to find the next matching character in the line that your cursor is on, and 'F' to find the previous matching character in the line that your cursor is on.
Is there a way to move around like that on the bash command line?
I know that you can set bash to be in vim mode, by saying set -o vim, and this works great for my local machine, but I administer a lot of machines where I can't change that setting.
Ignoring for a moment the security issues associated with everybody in your office sharing the same user, you could add a key binding to the readline command character-search:
# ~/.inputrc
C-]: character-search
To use the search, type Ctrl-] followed by the character you want to search for. You can bind the command to any key sequence, not just Ctrl-], but for obvious reasons you probably don't want to emulate vi mode by binding it to the letter f.
This would be less invasive than turning on vi mode so most users would probably not even notice the change. However, somebody could easily stumble upon your key sequence by accident and become very confused. You would also have to use three keystrokes instead of the two you're accustomed to with vi.
Lots of times I'll use Ctrl-R for reverse search and mistype some letter. Bash jumps up hundreds of lines and I'm in the middle of commands I was using a week ago.
Is there a shortcut for jumping back down to the lastest commands I had typed?
Edit: after testing it out on a CentOS server and Mac OS X, it looks like this only happening on OS X.
I've struggled with this same issue.
You can solve this by aborting with ctrl-c. Whether you're in the middle of a reverse search or scrolling through history with the arrows, aborting returns you to a prompt with the history scroll just after the last command.
UPDATE
Here's a nice trick I just learned. Bash and many other programs use Readline under the hood for command-line interpretation. Key bindings for Readline can be configured in a .inputrc file or with the bind command. The bindings can make use of a few functions provided by Readline. For example, I use Bash in vi mode but I still like to use Emacs-style ctrl-A so I have this line in my .bashrc file:
bind '\C-a:beginning-of-line'
To list all the available Readline functions:
bind -l
Among the functions is end-of-history. The function does like its name suggests. The difference between this approach and just using the abort command is that this keeps you on the same prompt.
If using libreadline, Alt-> (or Meta->). More info on Readline shortcuts or search for Commands for Manipulating the History in the man page.
On Mac, try command + .
It works for me.
I was trying alt+. and alt+shift+. , neither works for me. And then found command + . actually works
Maybe not exactly what you want, but you can fix your mistyped character(s) by using backspace when you're in the CTRL-r (reverse-i-search) mode.
You may wan to try "suggest box"-like history HSTR. It reads the bash history and allows quick navigation and filtering - you can see the context of similar history entries. Once you select a history entry it can be edited on the command line.
In Zsh with emacs binding set the actual default key sequence is ^[> binded to end-of-buffer-or-history command rather than command-. suggested above (or end-of-history depending on effect you want to achieve)
Cmd-. produces in Apple Terminal the similar or the same key sequence as Ctrl-C, which can be confirmed by running something useless and long, e.g. find . >/dev/null 2>&1 and pressing one and then other keys on keyboard.
Ctrl-C forces input to be ended and reset. and history scroll is just a side effect for it.
I am writing my own shell and need to implement a history feature where up and down arrow keys show history of commands executed. I need to find out when up and down keys are pressed.
How do i do this?
you want to be capturing input in raw mode. this can get kinda complicated, but here's an example that should get you on the right path:
http://docs.linux.cz/programming/c/unix_examples/raw.html
i'm assuming you're writing your shell in c. if you're using a more high-level language, there might be an easy way to get raw input. in python, for instance, i would use the ncurses module.