#$if Bash
set bell-style none
set expand-tilde off
set completion-ignore-case on

set convert-meta off
set input-meta on
set output-meta on

#set show-all-if-ambiguous on
#TAB:menu-complete

# vim mode
#set editing-mode vi

$if mode=vi
  set keymap vi-command

  set keymap vi-insert

  "\C-b": backward-char
  "\eb": backward-word
  "\e<": beginning-of-history
  "\C-a": beginning-of-line
  "\C-xe": call-last-kbd-macro
  "\ec": capitalize-word
  "\C-]": character-search
  "\e\C-]": character-search-backward
  "\C-l": clear-screen
  "\e\e": complete
  "\e!": complete-command
  "\e/": complete-filename
  "\e@": complete-hostname
  "\e{": complete-into-braces
  "\e~": complete-username
  "\e$": complete-variable
  "\C-d": delete-char
  "\e\\": delete-horizontal-space
  "\e\C-i": dynamic-complete-history
  "\C-x\C-e": edit-and-execute-command
  "\C-x)": end-kbd-macro
  "\e>": end-of-history
  "\C-e": end-of-line
  "\C-x\C-x": exchange-point-and-mark
  "\C-f": forward-char
  "\ef": forward-word
  "\C-x*": glob-expand-word
  "\C-xg": glob-list-expansions
  "\e^": history-expand-line
  "\e#": insert-comment
  "\e*": insert-completions
  "\C-k": kill-line
  "\ed": kill-word
  "\C-n": next-history
  "\en": non-incremental-forward-search-history
  "\ep": non-incremental-reverse-search-history
  "\C-o": operate-and-get-next
  "\C-x!": possible-command-completions
  "\e=": possible-completions
  "\e?": possible-completions
  "\C-x/": possible-filename-completions
  "\C-x@": possible-hostname-completions
  "\C-x~": possible-username-completions
  "\C-x$": possible-variable-completions
  "\C-p": previous-history
  "\C-x\C-r": re-read-init-file
  "\e\C-r": revert-line
  "\C-@": set-mark
  "\e ": set-mark
  "\e\C-e": shell-expand-line
  "\C-x(": start-kbd-macro
  "\e&": tilde-expand
  "\et": transpose-words
  "\C-x\C-u": undo
  "\C-_": undo
  "\e.": yank-last-arg
  "\e_": yank-last-arg
  "\e\C-y": yank-nth-arg
  "\ey": yank-pop
$endif

# kill word
#"\eh" : backward-kill-word
#"\ed" : kill-word

# Search (Especially C-s is not set by default)
"\C-r": reverse-search-history
"\C-s": forward-search-history

# default to re-read .inputrc
"\C-x\C-r": re-read-init-file
"\C-xr": re-read-init-file

# grep prefix
"\C-g": ""
# grep -r "" ./ # and avoid svn/git
"\C-g\C-r": "\C-e\C-ufind . -path \"./.svn\" -prune -o -path \"./.git\" -prune -o -type f -print0|xargs -0 grep --color=auto -s \"\"\C-b"
"\C-gr": "\C-e\C-ufind . -path \"./.svn\" -prune -o -path \"./.git\" -prune -o -type f -print0|xargs -0 grep --color=auto -s \"\"\C-b"
#"\er": "\C-e\C-ufind . -print0 -path \"./.svn\" -prune -o -path \"./.git\" -prune -o -type f -o -type l|xargs -0 grep \"\"\C-b"
# grep "" *
"\C-g\C-g": "\C-e\C-ugrep -I *\C-b\C-b \"\"\C-b"
"\C-gg": "\C-e\C-ugrep -I *\C-b\C-b \"\"\C-b"
# find ./
"\C-g\C-f": "\C-e\C-ufind . -path \"./.svn\" -prune -o -path \"./.git\" -prune -o -name \"**\" -print\eb\C-b\C-b\C-b\C-b"
"\C-gf": "\C-e\C-ufind . -path \"./.svn\" -prune -o -path \"./.git\" -prune -o -name \"**\" -print\eb\C-b\C-b\C-b\C-b"
# grep uniq
"\C-g\C-u": "\C-e\C-ufind . -path \"./.svn\" -prune -o -path \"./.git\" -prune -o -type f -print0|xargs -0 grep -l \"\"\C-b"
"\C-gu": "\C-e\C-ufind . -path \"./.svn\" -prune -o -path \"./.git\" -prune -o -type f -print0|xargs -0 grep -l \"\"\C-b"
# grep uniq and open with vi
"\C-g\C-v": "\C-e\C-uvi $(find . -path \"./.svn\" -prune -o -path \"./.git\" -prune -o -type f -print0|xargs -0 grep -l \"\")\C-b\C-b"
"\C-gv": "\C-e\C-uvi $(find . -path \"./.svn\" -prune -o -path \"./.git\" -prune -o -type f -print0|xargs -0 grep -l \"\")\C-b\C-b"
# grep Japanese with nkf
"\C-gj": "\C-e\C-ucat |nkf --ic=utf8|grep \"\"\C-a\ef "
"\C-g\C-j": "\C-e\C-ucat |nkf --ic=utf8|grep \"\"\C-a\ef "

# Quote/Bracket
"\e\"": "\eb\"\ef\""
"\e\'": "\eb\'\ef\'"
"\e\`": "\eb\`\ef\`"
"\e\]": "\eb\[\ef\]"
"\e\}": "\eb\{\ef\}"
"\e\)": "\eb\(\ef\)"

# Variable ${}
"\eq": "${}\C-b"

# ls
"\el":"\C-uls\C-m"
"\es":"\C-uls -ltr\C-m"

# du
"\ed": "\C-ufor a in *;do du -s -m "$a";done\C-m"

# /dev/null
"\e;": ">& /dev/null\C-m"
"\e:": "2>&1 |tee log\C-m"

# disable C-t
"\C-t": ""

# xargs
"\ex": "\C-ufind . -print0 -name \"**\" 2>/dev/null| xargs -0\C-a\ef\ef\ef\C-f\C-f\C-f"

# ps
"\ep": "\C-ups -U $USER\C-m"

# cl
"\eh": "\C-ucl -H\C-m"
"\er": "\C-ucl -r\C-m"

# ----------------------------------------------------------------------------
## Macros
$if Bash

# bc                                   :: =
"\C-x=": "bc <<< 'scale=20; '\C-b"

# find                                 :: /
"\C-x/": "find . -iname '*\e ' -printf '%M %u %g %P\\n'\C-x\C-x"

# GNU parallel                         :: \
"\C-x\\": " | parallel -X \e  {} \C-x\C-x"

# IPs                                  :: 0, 1, 7, 9
"\C-x0": "127.0.0.1"
"\C-x1": "192.168.0."
"\C-x2": "10.0.0."
"\C-x3": "172.16.0."

# Ranges                               :: -, .
"\C-x-": "{1\e ..}\C-b"
"\C-x.": "{1\e ..}\C-b"

# Output in columns                    :: |
"\C-x|": " | column -t"

# /dev/null                            :: _
"\C-x_": "/dev/null"

# awk                                  :: a, A
"\C-xa": "awk '/\e / {print $}' \e3\C-b"
"\C-xA": "awk '{sum += $1} END {print sum}' "

# Braces                               :: b, B, ]
"\C-xb": "(())\e2\C-b"
"\C-xB": "{}\C-b"
"\C-x]": "[[  ]]\e3\C-b"

# Counting row occurrences in a stream :: c
"\C-xc": " | sort | uniq -c | sort -rn"

# Diff                                 :: d, D
"\C-xd": "diff -u <() <(\e )\e5\C-b"
"\C-xD": "diff -uq\e  --from-file "

# ed                                   :: e, E
"\C-xe": "printf '%s\\n' H \e  wq | 'ed' -s "
"\C-xE": "printf '%s\\n' H 0i \e  . wq | 'ed' -s "

# Loops                                :: f, F, u, w
"\C-xf": "for i in \e ; do  \"$i\"; done\e2\eb\e3\C-b"
"\C-xF": "for ((i = 0; i < \e ; i++)); do  \"$i\"; done\e2\eb\e3\C-b"
"\C-xu": "until \e ; do ; done\eb\e2\C-b"
"\C-xw": "while \e ; do ; done\eb\e2\C-b"

# lsof                                 :: l
"\C-xl": 'lsof -i :'

# File renaming (mv)                   :: m, M
"\C-xm": "find . -maxdepth 1 -iname '*\e ' ! -path . -printf \"mv '%P' '%P'\\n\" | v -c\"Tabularize/'.\\{-}'/l1l0\" -c'se ft=sh' -\C-x\C-x"
"\C-xM": "parallel mv -- {} {.}.\e  ::: *."

# Directory statistics                 :: n, o, @
"\C-xn": "echo 'Newest: '; (shopt -s nullglob; unset newest; for file in *\e ; do [[ $file -nt $newest ]] && newest=\"$file\"; done; echo \"$newest\")"

"\C-xo": "echo 'Oldest: '; (shopt -s nullglob; unset oldest; for file in *\e ; do [[ $file -ot $oldest || ! $oldest ]] && oldest=\"$file\"; done; echo \"$oldest\")"

"\C-x@": "(shopt -s nullglob dotglob; shopt -u failglob; inodes=(*\e ); echo There are \"${#inodes[@]}\" items)"

# printf                               :: p
"\C-xp": "printf '%s\\n' "

# rsync                                :: r, R
"\C-xr": "rsync -ai\e  -e'ssh -q' "
"\C-xR": "-f'- '\C-b"

# Backticks                            :: `
"\C-x`": "$()^b"

# systemd                              :: s, S
"\C-xs": "systemctl status"
"\C-xS": "systemctl \e start\C-x\C-x"

# Test                                 :: t
"\C-xt": " && echo ok"

# tcpdump                              :: T
"\C-xT": "tcpdump -iany -s0 -nnq "

# Array                                :: [
"\C-x[": "\"${[@]}\"\e5\C-b"

# Extended globs: @?*+!(item1|item2)   :: *
"\C-x*": "\e @(|)\C-b\C-b"
