Thursday, April 2, 2009

Doskey like tab completion and various other fixes /power user hacks for the mac console prompt (ctrl-left,right arrow)

Want to have the same keystrokes (plus more) that are seen in M$ command/doskey or cmd on the mac console? Especially ctrl-left-arrow, ctrl-right-arrow. I did. Many years ago (Slackware v2) I started this .inputrc and have since updated and translated it for the mac console (Terminal.App).

For additional help "man bash" and search for "bind". You will find the definitions of the commands in .inputrc (i.e. menu-complete). See the links at the end of the included .inputrc for additional help. These instructions are for the console, look here for fixing home and end in regular mac GUI applications: http://www.starryhope.com/tech/2006/mac-os-x-home-and-end-keys/. If your really curious about how all this works, "man stty"

This first part focuses on home and end, I put comments in the .inputrc that will explain all the other cool keystrokes.

Only two Steps:
  1. Change keybindings in Terminal.App
  2. Create/add to .inputrc
___________________________________________________________

1. Change keybindings in Terminal.App
Goto Terminal's Preferences. Select Keyboard. Find the end key and click Edit. Change the end key's action to "send to string to shell". You'll notice that backspace/delete do not work properly. Use the Delete One Character button to backspace. Set the string to "\\033[4~"

I had some issues typing the "\" character in the string to send edit box so I cut-n-pasted from some other random key and just changed the trailing end of the keycode.

Do the same thing for the home key, except use "\\033[1~". When your done Terminal's preference's should look something like this:


2. Drop this into ~/.inputrc -----------------------------------BOF
#"\e[11~": "Function Key 1"
#"\e[12~": "Function Key 2"
#"\e[13~": "Function Key 3"

#"\e[14~": "Function Key 4"
#"\e[15~": "Function Key 5"
#"\e[17~": "Function Key 6"
#"\e[18~": "Function Key 7"
#"\e[19~": "Function Key 8"
#"\e[20~": "Function Key 9"
#"\e[21~": "Function Key 10"

# For Bash, all terminals, add some Bash specific hacks.
$if Bash
"\C-xv": show-bash-version
"\C-x\C-e": shell-expand-line


### fix delete key
"\e[3~": delete-char

## END key -- MAC must be remapped in Terminal options (keyboard) "\033[4~"
"\e[4~":end-of-line
## HOME key -- MAC must be remapped in Terminal options (keyboard) "\033[1~"
"\e[1~": beginning-of-line

$endif

# For FTP, different hacks:
$if Ftp
"\C-xg": "get \M-?"
"\C-xt": "put \M-?"
"\M-.": yank-last-arg
$endif

" ": self-insert

$if Bash

# ctrl-backspace - none of this ever worked :( --- use ctrl-w instead (default)
### never worked -->"\e\C-[D": backward-kill-word
### Control-Rubout: backward-kill-word
##Map control-foward-delete to F14 "\033[26~"
##"\e[26~": backward-kill-word


#### doskey like completion
set completion-ignore-case On
"\e[A": history-search-backward
"\e[B": history-search-forward

#### doskey like line movement
#ctrl-left arrow
"\e[5D": backward-word
#ctrl-right arrow
"\e[5C": forward-word


# auto list tab completions
set show-all-if-ambiguous on

# show a filetype indicator in tab completions
set visible-stats on

## tab cycles through completions -- I like the show-all-if-ambiguous instead
#"\t": menu-complete
## shift-tab to reverse cycle complete http://www.tikirobot.net/wp/2006/03/29/reverse-menu-complete-in-bash/
#\C-y: "\e--\C-i"

### didn't like this
#Space: magic-space



$endif

# Include system wide settings which are ignored by default if one has their own .inputrc
$include /etc/inputrc


##helpful sites
#http://codesnippets.joyent.com/tag/inputrc
#http://linuxart.com/log/archives/2005/10/13/super-useful-inputrc/
-----------------------------------EOF



If ~/.inputrc doesn’t work, then you need to add

export INPUTRC=~/.inputrc

to your .bashrc/.bash_profile

If your wondering why I am bother with home/end it's because I use a M$ ergo keyboards on my macs. Why doesn't Apple make one an ergonomic keyboard?


PS: Read-line keys natively work in man mac apps (ctrl+e for end / ctrl+a for end / ctrl+k for kill line, etc)

No comments:

Post a Comment