Never been to CodeSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

2 total

Fine-tuning tab completion in Bash

export IFS=$' \t\n'

help complete
open /usr/share/doc/bash/builtins.pdf

# cf. http://codesnippets.joyent.com/posts/show/1630
bind -p | egrep '^"' | egrep -v 'do-lowercase-version|self-insert' | nl
bind -p | egrep '^"' | egrep -v 'do-lowercase-version|self-insert' | grep -i complete | nl
bind -p | egrep '^"\\C' | egrep -v 'do-lowercase-version|self-insert' | grep -i complete | nl
bind -p | egrep '^"\\M' | egrep -v 'do-lowercase-version|self-insert' | grep -i complete | nl
bind -p | egrep '^"\\e' | egrep -v 'do-lowercase-version|self-insert' | grep -i complete | nl


# ~/.bashrc

echo '

shopt -s cmdhist
shopt -s histappend
export HISTIGNORE="&:clear:exit"
#export HISTIGNORE="&:[ \t]*:ls:[bf]g:history*:clear:exit"
#export HISTCONTROL=ignoredups
#PROMPT_COMMAND="history -a; ${PROMPT_COMMAND}"

# make sure Bash programmable completion is enabled
shopt progcomp > /dev/null || shopt -s progcomp
complete -d cd find
complete -G "*.txt" nano
# cf. locate /usr*.sh
#complete -G "/usr/lib/*.sh" nano

# enabe [ctrl-s] (opposite of [ctrl-r])
/bin/stty stop ""

' >> ~/.bashrc


# ~/.inputrc

echo '

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

# [esc-d]
"\ed": kill-whole-line
#"\M-d": kill-whole-line


# up & down arrow keys
"\e[A": history-search-backward
"\e[B": history-search-forward

# use [ctrl-arrowright] or [ctrl-arrowleft] to jump between words
"\e[5C": forward-word
"\e[5D": backward-word

# op[esc-h] will show the last command that began with op
"\eh": dynamic-complete-history


# enable case-insensitive TAB-completion (for file names)
set completion-ignore-case on

# enable one-tab completion
set show-all-if-ambiguous on 
  
# turn on inline mode for tab completion
"\t": menu-complete

# shift-TAB to menu-complete backwards
# cf. http://www.tikirobot.net/wp/2006/03/29/reverse-menu-complete-in-bash/
\C-y: "\e--\C-i"
#\C-y:"\M--\C-i"


# add the missing slash when tab-completing symbolic links to directories
set mark-symlinked-directories on

#set page-completions off
#set completion-query-items 100
#set mark-directories on
#set visible-stats on
#set meta-flag on
#set input-meta on
#set convert-meta off
#set output-meta on


# ... fine-tuning magic space ...
# [esc-1], [esc-2], ...
# cf. http://codesnippets.joyent.com/posts/show/1630
"\e0": "!-1:0"
"\e1": "!-1:1"
"\e2": "!-1:2"
"\e3": "!-1:3"
"\e4": "!-1:4"
"\e5": "!-1:5"
"\e6": "!-1:6"
"\e7": "!-1:7"
"\e8": "!-1:8"
"\e9": "!-1:9"
"\er": "!-1:1-"

$if Bash
  Space: magic-space
$endif

' >> ~/.inputrc


open -a Terminal; kill -HUP $$

[cmd-n]   # new shell

# now try
cd [tab]
find [tab]
nano [tab]


# ... fine-tuning magic space ...
echo this is a very long command indeed '!!!'

[esc-0][space] [esc-1][space] [esc-r][space]


#--------------------------------------------------


# some further tab completion examples

[tab]           # list all available commands
[esc][esc]      # alternative
[esc-?]          # list all possible completions
[esc-*]          # insert all possible completions after the cursor

ds[tab]          # list all available commands beginning with ds
ds[esc][esc]
ds[esc-?]
ds[esc-*]

cd ~/Des[tab] 
#cd ~/Desktop
cd [tab]      # list all subdirectories in ~/Desktop
cd [esc][esc]
cd [esc-?]
cd [esc-*]

$[tab]       # list all set system variables
~[tab]       # list users
~[esc][esc]   # alternative
*[tab] 
=[tab]
/[tab]
@[tab]      # list entries form /private/etc/hosts


# disable completion for a single command
help complete
complete -r cd[tab]
cd[tab]


#-------------------------------------------


# man [tab]

function listmans() {
for dir in $(/usr/bin/man -W | /usr/bin/tr ':' '\n'); do 
   /usr/bin/find -x "${dir}" ! -type d -name "*.*" -print0 2>/dev/null | /usr/bin/xargs -0 /usr/bin/basename | /usr/bin/sort -u
done 
return 0
}

listmans | less
listmans | sed -E -n 's/^.+(\.[^\.]+)$/\1/p' | sort -u
listmans | sed -E -n 's/^.+(\.[^\.]+\.gz)$/\1/p' | sort -u
listmans | sed -E -n 's/^.+(\.[[:lower:]]+)$/\1/p' | sort -u
listmans | sed -E -n 's/^.+(\.[[:alnum:]]+)$/\1/p' | sort -u


complete -W "$(
   for dir in $(/usr/bin/man -W | /usr/bin/tr ':' '\n'); do 
      /usr/bin/find -x "${dir}" ! -type d -print0 2>/dev/null | /usr/bin/xargs -0 /usr/bin/basename | \
      /usr/bin/sed -E -n 's/^(.+)\.[[:digit:]]+$|\.[[:digit:]]+[[:lower:]]+$|\.[[:lower:]]+$|\.[^\.]+\.gz$/\1/p' | /usr/bin/sort -u

      #/usr/bin/find -x "${dir}" \( -type f -or -type l \) -print0 2>/dev/null | /usr/bin/xargs -0 /usr/bin/basename | \
      #/usr/bin/sed -E -n 's/^(.+)\.[[:digit:]]+$|\.[[:digit:]]+[[:lower:]]+$|\.[[:lower:]]+$|\.[^\.]+\.gz$/\1/p' | /usr/bin/sort -u
   done
)" man

man ds[tab]
man ssh[tab]

man m[esc][esc]
man m[esc-?]
man m[esc-*]

man chmod 2[esc-t]      # swap the positions of two words


#---------------------------------------------


# open -a [tab]

# cf. http://codesnippets.joyent.com/posts/show/1643
/usr/bin/sudo /bin/ln -is "$(/usr/bin/locate lsregister | /usr/bin/head -n 1)" /bin/lsregister

function opena() { /usr/bin/open -a "$@"; return 0; }
alias opena='/usr/bin/open -a'

# version 1
complete -W "Mail Safari Terminal Address\\\\\\ Book" opena
opena A[tab]

# version 2
time -p lsregister -dump | sed -E -n -e '/Applications/{s/^.+ ((\/Applications|\/Developer\/Applications).+\.app)$/\1/p;}' | \
        sed 's/ /\\ /g' | xargs basename -s '.app' | sed 's/ /\\\\\\ /g' | nl

time -p lsregister -dump | sed -E -n -e '/\/Applications/{s/^.+ ((\/Applications|\/Developer).+\.app)$/\1/p;}' | \
        sed 's/ /\\ /g' | xargs basename -s '.app' | sed 's/ /\\\\\\ /g' | nl


complete -W "$(
/bin/lsregister -dump | /usr/bin/sed -E -n -e '/\/Applications/{s/^.+ ((\/Applications|\/Developer).+\.app)$/\1/p;}' | \
     /usr/bin/sed 's/ /\\ /g' | /usr/bin/xargs /usr/bin/basename -s '.app' | /usr/bin/sed 's/ /\\\\\\ /g'
)" opena

opena S[tab]
opena C[esc][esc]
opena [esc-.]
opena [esc-*]



Further information:

- Working more productively with bash 2.x/3.x
- Bash complete examples (Apple Darwin source code)
- Mac Bash Completion
- Create on-the-fly hostname lists for ssh tab completion
- bash: completion (with ssh)
- Custom Tab Completion
- CLI Magic: Bash complete
- My Bash Profile
- HOWTO: make life easier in terminal using bash aliasing
- Cdots - Change directory back - 1-7 times - and forth with TAB-completion
- some handy terminal.app crud
- cd effectively (links)
- Tapping the Bash command line history
- Handling control characters on the command line

Select inline styles

Selects inline styles. I purposely excluded quotation marks from the character set because they cause the expression to select attributes of other tags on the same line.

style="[\s\d\w\.\-\:\;]+"


Edit: Here's a shorter way of accomplishing the same thing:

style="[^"']+"


It's probably worth adding a space before the expression to make sure that you don't leave in unnecessary spaces in the tag and to avoid selecting things that are not styles, such as the bold part of


<!-- fwtable fwsrc="navbar.png" fwbase="navbar.gif" fwstyle="Dreamweaver" fwdocid = "742308039" fwnested="0" -->
2 total