Matt Brown – Journeys of a software engineer

April 3, 2009

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

Filed under: console, Keyboard — Tags: , — mattbsoftware @ 12:55 am

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 “\33[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 “\33[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) “33[4~”
“\e[4~”:end-of-line
## HOME key — MAC must be remapped in Terminal options (keyboard) “33[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 “33[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)

Advertisements

April 2, 2009

Colorful ls (dircolors) on a mac console (terminal.app)

Filed under: console, coreutils, ls — Tags: , , — mattbsoftware @ 1:55 am

On linux systems when I listed the contents of a directory (ls) I saw a colorful list. Different colors were used for compressed files, directories, images, etc.


Install GNU coreutils Steps:
  1. Download coreutils (I used v7.2). d/l http://ftp.gnu.org/gnu/coreutils site: http://www.gnu.org/software/coreutils
  2. untar it tar xzvf coreututils-7.2.tar.gz
  3. cd into the coreutils-7.2 directory
  4. ./configure –prefix=/usr
  5. make
  6. make check – (I had 2 test failures, but no problems)
  7. Install all GNU core utils by executing ‘sudo make install’
  8. OR just install ls and dircolors, and leave everything else BSD by copying ls & dircolors from ./src to /usr/bin
Setup bash_profile
  1. edit .bash_profile
  2. add these lines
<code>
#parses .dircolors and makes env var for GNU ls
eval `dircolors`

alias ls=’ls -hF –color=auto’

</code>
Setup .dircolors
————————————-BOF .dircolors
<code>
# Configuration file for dircolors, a utility to help you set the
# LS_COLORS environment variable used by GNU ls with the –color option.

# The keywords COLOR, OPTIONS, and EIGHTBIT (honored by the
# slackware version of dircolors) are recognized but ignored.

# Below, there should be one TERM entry for each termtype that is colorizable
TERM linux
TERM linux-c
TERM mach-color
TERM console
TERM con132x25
TERM con132x30
TERM con132x43
TERM con132x60
TERM con80x25
TERM con80x28
TERM con80x30
TERM con80x43
TERM con80x50
TERM con80x60
TERM xterm
TERM xterm-debian
TERM rxvt
TERM screen
TERM screen-w
TERM vt100

# Below are the color init strings for the basic file types. A color init
# string consists of one or more of the following numeric codes:
# Attribute codes:
# 00=none 01=bold 04=underscore 05=blink 07=reverse 08=concealed
# Text color codes:
# 30=black 31=red 32=green 33=yellow 34=blue 35=magenta 36=cyan 37=white
# Background color codes:
# 40=black 41=red 42=green 43=yellow 44=blue 45=magenta 46=cyan 47=white
NORMAL 00 # global default, although everything should be something.
FILE 00 # normal file
DIR 01;34 # directory
LINK 01;36 # symbolic link. (If you set this to ‘target’ instead of a
# numerical value, the color is as for the file pointed to.)
FIFO 40;33 # pipe
SOCK 01;35 # socket
DOOR 01;35 # door
BLK 40;33;01 # block device driver
CHR 40;33;01 # character device driver
ORPHAN 40;31;01 # symlink to nonexistent file

# This is for files with execute permission:
EXEC 01;32

# List any file extensions like ‘.gz’ or ‘.tar’ that you would like ls
# to colorize below. Put the extension, a space, and the color init string.
# (and any comments you want to add after a ‘#’)

# If you use DOS-style suffixes, you may want to uncomment the following:
#.cmd 01;32 # executables (bright green)
#.exe 01;32
#.com 01;32
#.btm 01;32
#.bat 01;32

.tar 01;31 # archives or compressed (bright red)
.tgz 01;31
.arj 01;31
.taz 01;31
.lzh 01;31
.zip 01;31
.z 01;31
.Z 01;31
.gz 01;31
.bz2 01;31
.deb 01;31
.rpm 01;31
.jar 01;31
.dmg 01;31

# image formats
.jpg 01;35
.png 01;35
.gif 01;35
.bmp 01;35
.ppm 01;35
.tga 01;35
.xbm 01;35
.xpm 01;35
.tif 01;35
.png 01;35
.mpg 01;35
.avi 01;35
.fli 01;35
.gl 01;35
.dl 01;35

</code>
————————————————EOF .dircolors

All Done, type ls at a console prompt and your golden, or purple(directories), green(executables), etc…

– mattb

PS: Once an Apple update, updated ls, not problems, just remake and reinstall

PPS: A friend reminded me that if you want to keep BSD ls on your mac, you can simply alias ls=’ls- -G’ It is not as configurable that I know, but anything is better than colorless

Create a free website or blog at WordPress.com.