Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

How to paste Escape sequences in Terminal's preferences UNIX
Goal: To move the shell cursor around quicker.
Problem: Cannot enter escape character sequences in terminal inspector

You can add some convenience functions to your shell via a file called .inputrc in your home directory. For example, the line:
"e[5C": forward-word
will move the cursor to the next whitespace when the character sequence "Escape-[-5-C" is sent to readline by the Terminal (note that ESCAPE is, in fact, a non-printable ASCII character).

Now, you could jump to the next whitespace by pressing the Escape key, the [ key, the 5 key and SHIFT-C key in sequence. As this is not really convenient, Terminal.app allows you to define key bindings. The default setting is to generate the above character sequence when the right arrow key and the Control key are pressed simultaneously. thus, when you press Control-Right on your keyboard, your shell cursor now jumps to the next whitespace. Very convenient (you may have to start a new shell to make it re-read .inputrc).

However, there is a problem in terminal.app: how can you define custom Escape character sequences? For simple, non-Escape character sequences, all you have to do is open Terminal.app's Inspector and push the Add button in the Keyboard view. Set key to cursor right, modifier to control and action to send string to shell. In the textfield, just enter the desired character sequence.

The problem with Escape sequences is that there apparently is no way to enter the Escape non-printable character in this text field. The inspector says to "Hold the option key and press esc or ctrl to create a string containing those special characters." However, pressing Option-Escape does nothing at all, and pressing Option-Escape-[ only generates the unicode character """, like in all Cocoa applications.

So to enter the desired escape character sequence (ESCAPE-[-5-C in this example), you can do the following. In the shell, enter:
echo -ne "e[5C"|pbcopy
This will put the correct escape character sequence in the pasteboard. Just switch to the Inspector again, and paste the pasteboard's content into the textfield. The non-printable escape character will not be visible in the textfield, but will get sent to the shell when the key combination it was bound to is pressed.

Another nice setting is to bind del(forward delete) to Control-D. Strangely enough, pressing Option-Control-D in the Inspector's textfield will generate exactly this character seqeuence (as opposed to the greek delta, which is generated by pressing Option-D in other Cocoa applications).

For further information about shell convenience functions, read the manpage for readline.

[robg adds: I couldn't find a manpage for readline on my machine; the best I could do was any number of web pages with readline man page entries; this one, for instance.]
    •    
  • Currently 3.60 / 5
  You rated: 4 / 5 (5 votes cast)
 
[22,908 views]  

How to paste Escape sequences in Terminal's preferences | 14 comments | Create New Account
Click here to return to the 'How to paste Escape sequences in Terminal's preferences' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
How to paste Escape sequences in Terminal's preferences
Authored by: hopthrisC on Jan 22, '04 12:32:39PM
Formatting error in this posting!!

To generate an escape character you have to use a backslash (which apparently got eaten by the grue...)

You have to replace every

"e[5C"
with
"\e[5C"
in this hint.

[ Reply to This | # ]
How to paste Escape sequences in Terminal's preferences
Authored by: folkert on Jan 22, '04 02:06:01PM
true, i should have used the [ code ] tag instead of the < code > tag.
stupid grues ;)


folkert

---
$ hexdump /mach_kernel|head -1
0000000 feed face 0000 0012 0000 0000 0000 0002


[ Reply to This | # ]

How to paste Escape sequences in Terminal's preferences
Authored by: fds on Jan 22, '04 02:33:40PM
The Ctrl-arrow key combos are very useful.

I find it interesting that these are already in the global /etc/inputrc on Red Hat Linux / Fedora Core, and I'd assume most other Linux distros:


# for linux console and RH/Debian xterm
"\e[1~": beginning-of-line
"\e[4~": end-of-line
"\e[5~": beginning-of-history
"\e[6~": end-of-history
"\e[3~": delete-char
"\e[2~": quoted-insert
"\e[5C": forward-word
"\e[5D": backward-word
"\e\e[C": forward-word
"\e\e[D": backward-word
Strange that Panther doesn't seem to have a default inputrc file shipped by default.

[ Reply to This | # ]
readline() and forward-word
Authored by: ngb on Jan 22, '04 05:13:22PM

readline() is in section 3 of the manpages (make sure /usr/share/man is in your manpath). There's also a reference to in the bash manpage (but not in the tcsh manpage).

Additionally, it's possible to set key bindings using bind (or bindkey in tcsh) and explicitly bind forward-word to whatever you want,

Finally, I'm not sure this is even necessary, as both bash and tcsh seem to default to Esc-f and Esc-b to moving the cursor forward or back one whole word respectivly.



[ Reply to This | # ]
readline() and forward-word
Authored by: jpryor68 on Jan 22, '04 06:17:55PM

Not sure that readline is included in a fresh Panther + Xcode install. I've fiddled with my MANPATH, but /usr/share/man is included:

% printenv MANPATH; man readline
/Library/MySQL/man:/Library/Frameworks/Python.framework/Versions/Current/man:/usr/local/teTeX/man:/usr/local/man:/usr/local/share/man:/usr/share/man
No manual entry for readline

Plus, when I ls the /usr/share/man directories, I don't find readline:

% ls /usr/share/man/man3/readline*
tcsh: ls: No match.

Thanks for the tip about esc-f and esc-b, though. I just bound them to option-rightarrow and option-leftarrow in my Terminal Preferences.



[ Reply to This | # ]
getting shell to recognize .inputrc file
Authored by: ngb on Jan 22, '04 05:17:36PM
You don't need to logout or start a new terminal window. Just type:

% source .inputrc
Also works if you've edited your any other rc or profile file.

[ Reply to This | # ]
How about...
Authored by: josephaw on Jan 22, '04 05:31:14PM

pressing just "ctrl v" and then pressing the esc key or any other key that is needed?



[ Reply to This | # ]
readline()
Authored by: ngb on Jan 22, '04 10:23:41PM
I figured it out. I've got fink installed, and include /sw/share/man in my manpath. That's where the readline(3) entry is kept. Sorry for confusing everyone.

A lot of the same info, with regards to commands like forward-word, can be found in the bash and tcsh man pages, too.

[ Reply to This | # ]

How to paste Escape sequences in Terminal's preferences
Authored by: sjmills on Jan 22, '04 11:43:24PM

I can get none of this to work. I'm using tcsh. Does the terminal type matter? I'm using ansi. All I want is to make get editing to work like it does in all correct Mac apps: option-left and -right for prev and next work, and forward delete to map to ^D.



[ Reply to This | # ]
How to paste Escape sequences in Terminal's preferences
Authored by: tanvach on Jan 23, '04 08:39:50AM

I've tested the hint and seems to only work with bash shell. Also, you need to do 'echo -ne "\e[5C"|pbcop' in bash since the -ne argument doesn't work with tcsh.



[ Reply to This | # ]
How to paste Escape sequences in Terminal's preferences
Authored by: clith on Jan 23, '04 02:20:51PM
Yes, in bash "\e" is translated to escape by the shell before it is parsed by the command you run (in this case echo). In tcsh you just type control-v and then type whatever special character you like. This is because tcsh by default uses escape sequences to edit text on the command line (esc-b is backward-word for instance -- see the tcsh man page for the "bindkey" built-in or type "bindkey" by itself to get a dump of all bindings.

So please note that this hint is bash-specific and sould read:

"How to bind keys for editing the command line in bash"


[ Reply to This | # ]
How to paste Escape sequences in Terminal's preferences
Authored by: ClarkGoble on Jan 23, '04 06:00:26PM
Is there a way in tcsh to do word forward / back or line begin / end? I'd truly like those features. I looked at the tcsh docs on bindkey and didn't see those as options.

Tcsh

[ Reply to This | # ]

How to paste Escape sequences in Terminal's preferences
Authored by: barthez on Mar 22, '04 05:41:21PM

For tcsh, put the following in your .tcshrc:

# moving backwards and forward one word with control-arrow
bindkey ";5D" backward-word
bindkey ";5C" forward-word

Bug: This prevents you from entering a plain old ";" on the cli. Thus, for chaining commands together, you'll have to use "&&" instead of ";". If you find a way to work around this, I'd be happy to know...



[ Reply to This | # ]
How to paste Escape sequences in Terminal's preferences
Authored by: geohar on Apr 01, '04 03:38:07AM
Make sure you've set up the following

set echo_style=both
Then the line below will get your sequence on the clipboard

echo -n "\\e[5~" | pbcopy
This can be made to work in tcsh.

[ Reply to This | # ]