builtin

[!TIP|label:references:]

eval

evalconstruct command by concatenating arguments

reference:

  • example

    • without eval:

      $ foo='ls | less'
      $ $foo
      ls: cannot access '|': No such file or directory
      ls: cannot access 'less': No such file or directory
    • with eval:

      $ foo='ls | less'
      $ eval $foo
      Applications
      Library
      System
      Users
      ...
  • example

    $ x=10
    $ y=x
    $ foo='$'$x
    $ echo $foo
    $x
    
    # with eval
    $ eval foo='$'$x         # with eval
    $ echo $foo
    10
    
    # or
    $ foo=\$$x
    $ eval echo $foo          # with eval
    10
    $ echo $foo
    $x

[!NOTE] reference:

show current status

  • show on/off

    $ set -o
    $ set -o
    allexport       off
    braceexpand     on
    emacs           on
    errexit         off
    errtrace        off
    functrace       off
    hashall         on
    histexpand      on
    history         on
    ignoreeof       off
    interactive-comments  on
    keyword         off
    monitor         on
    noclobber       off
    noexec          off
    noglob          off
    nolog           off
    notify          off
    nounset         off
    onecmd          off
    physical        off
    pipefail        off
    posix           off
    privileged      off
    verbose         off
    vi              off
    xtrace          off
  • show +o/-o

    $ set +o
    $ set +o
    set +o allexport
    set -o braceexpand
    set -o emacs
    set +o errexit
    set +o errtrace
    set +o functrace
    set -o hashall
    set -o histexpand
    set -o history
    set +o ignoreeof
    set -o interactive-comments
    set +o keyword
    set -o monitor
    set +o noclobber
    set +o noexec
    set +o noglob
    set +o nolog
    set +o notify
    set +o nounset
    set +o onecmd
    set +o physical
    set +o pipefail
    set +o posix
    set +o privileged
    set +o verbose
    set +o vi
    set +o xtrace

option name

set VS. shopt

  • set originates from the bourne shell (sh) and is part of the POSIX standard;

  • shopt is bourne-again shell (bash) specific

    $ set | grep -e SHELLOPTS -e BASHOPTS
    # for shopt
    BASHOPTS=cdspell:checkwinsize:cmdhist:complete_fullquote:expand_aliases:extglob:extquote:force_fignore:globasciiranges:histappend:interactive_comments:login_shell:progcomp:promptvars:sourcepath
    # for set
    SHELLOPTS=braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor
    
    $ set -o |  column -t | grep -v off
    braceexpand           on
    emacs                 on
    hashall               on
    histexpand            on
    history               on
    interactive-comments  on
    monitor               on
    
    $ shopt | column -t | grep -v off
    cdspell                  on
    checkwinsize             on
    cmdhist                  on
    complete_fullquote       on
    expand_aliases           on
    extglob                  on
    extquote                 on
    force_fignore            on
    globasciiranges          on
    histappend               on
    interactive_comments     on
    login_shell              on
    progcomp                 on
    promptvars               on
    sourcepath               on

[!TIP|label:tips:]

  • iMarslo : filename expansion check the shopt on/off ( set +o )

  • off

    $ shopt -u extglob
    $ shopt extglob
    extglob         off
    $ echo $?
    1
  • on

    $ shopt -s extglob
    $ shopt extglob
    extglob         on
    $ echo $?
    0

check without output via -q

  • on

    $ shopt -q extglob; echo $?
    0
  • off

    $ shopt -q failglob; echo $?
    1

shopt

shopt [-pqsu] [-o] [optname …]

options

  • assoc_expand_once

  • autocd

  • cdable_vars

  • cdspell

  • checkhash

  • checkjobs

  • checkwinsize

  • cmdhist

  • compat31

  • compat32

  • compat40

  • compat41

  • compat42

  • compat43

  • compat44

  • complete_fullquote

  • direxpand

  • dirspell

  • dotglob

    • -s: * will including all .*

  • execfail

  • expand_aliases

  • extdebug

  • extglob

  • extquote

  • failglob

    • -s: show error msg and cmd not been exectued

  • force_fignore

  • globasciiranges

  • globstar

  • gnu_errfmt

  • histappend

  • histreedit

  • histverify

  • hostcomplete

  • huponexit

  • inherit_errexit

  • interactive_comments

  • lastpipe

  • lithist

  • localvar_inherit

  • localvar_unset

  • login_shell

  • mailwarn

  • no_empty_cmd_completion

  • nocaseglob

  • nocasematch

  • nullglob

  • progcomp

  • progcomp_alias

  • promptvars

  • restricted_shell

  • shift_verbose

  • sourcepath

  • xpg_echo

examples

  • show all status

    $ shopt | column -t
    # or
    $ shopt -p
    $ shopt | column -t
    autocd                   off
    assoc_expand_once        off
    cdable_vars              off
    cdspell                  on
    checkhash                off
    checkjobs                off
    checkwinsize             on
    cmdhist                  on
    compat31                 off
    compat32                 off
    compat40                 off
    compat41                 off
    compat42                 off
    compat43                 off
    compat44                 off
    complete_fullquote       on
    direxpand                off
    dirspell                 off
    dotglob                  off
    execfail                 off
    expand_aliases           on
    extdebug                 off
    extglob                  on
    extquote                 on
    failglob                 off
    force_fignore            on
    globasciiranges          on
    globskipdots             on
    globstar                 off
    gnu_errfmt               off
    histappend               on
    histreedit               off
    histverify               off
    hostcomplete             off
    huponexit                off
    inherit_errexit          off
    interactive_comments     on
    lastpipe                 off
    lithist                  off
    localvar_inherit         off
    localvar_unset           off
    login_shell              on
    mailwarn                 off
    no_empty_cmd_completion  off
    nocaseglob               off
    nocasematch              off
    noexpand_translation     off
    nullglob                 off
    patsub_replacement       on
    progcomp                 on
    progcomp_alias           off
    promptvars               on
    restricted_shell         off
    shift_verbose            off
    sourcepath               on
    varredir_close           off
    xpg_echo                 off
    $ shopt -p
    shopt -u autocd
    shopt -u assoc_expand_once
    shopt -u cdable_vars
    shopt -s cdspell
    shopt -u checkhash
    shopt -u checkjobs
    shopt -s checkwinsize
    shopt -s cmdhist
    shopt -u compat31
    shopt -u compat32
    shopt -u compat40
    shopt -u compat41
    shopt -u compat42
    shopt -u compat43
    shopt -u compat44
    shopt -s complete_fullquote
    shopt -u direxpand
    shopt -u dirspell
    shopt -u dotglob
    shopt -u execfail
    shopt -s expand_aliases
    shopt -u extdebug
    shopt -s extglob
    shopt -s extquote
    shopt -u failglob
    shopt -s force_fignore
    shopt -s globasciiranges
    shopt -u globstar
    shopt -u gnu_errfmt
    shopt -s histappend
    shopt -u histreedit
    shopt -u histverify
    shopt -u hostcomplete
    shopt -u huponexit
    shopt -u inherit_errexit
    shopt -s interactive_comments
    shopt -u lastpipe
    shopt -u lithist
    shopt -u localvar_inherit
    shopt -u localvar_unset
    shopt -s login_shell
    shopt -u mailwarn
    shopt -u no_empty_cmd_completion
    shopt -u nocaseglob
    shopt -u nocasematch
    shopt -u nullglob
    shopt -s progcomp
    shopt -u progcomp_alias
    shopt -s promptvars
    shopt -u restricted_shell
    shopt -u shift_verbose
    shopt -s sourcepath
    shopt -u xpg_echo
  • show single option

    # shopt -s sourcepath
    $ shopt -q sourcepath; echo $?
    0
    
    # shopt -u xpg_echo
    $ shopt -q xpg_echo; echo $?
    1
  • save terminal commands in bash history in real time

    $ shopt -s histappend ; PROMPT_COMMAND="history -a;$PROMPT_COMMAND"

[!NOTE|label:references:]

  • more samples

    • "\e[11~" : ESC [ 1 1 ~’ : "Function Key 1"

    • keyname

      Control-u: universal-argument
      Meta-Rubout: backward-kill-word
      Control-o: "> output"
    • keyseq

      "\C-u": universal-argument
      "\C-x\C-r": re-read-init-file
      "\e[11~": "Function Key 1"

vi mode

[!NOTE]

$ cat .inputrc
set editing-mode vi
set keymap vi

# or
$ set -o vi

hybrid mode

[!TIP|label:references:]

#=============================================================================
#     FileName : .inputrc
#       Author : marslo.jiao@gmail.com
#      Created : 2020-08-25 15:22:56
#   LastChange : 2024-01-13 21:08:28
#=============================================================================
# [GNU Readline Library](https://tiswww.case.edu/php/chet/readline/rluserman.html)
# [PS1](https://github.com/marslo/mylinux/blob/master/confs/home/.marslo/.env#L209)
# set the mode string and cursor to indicate the vim mode
#   - cursor shape:       `\1\e[<number> q\2` ( i.e.: `\1\e[4 q\2` )
#   - cursor shape+color: `\1\e[<nubmer> q\e]12;<color>\a\2` ( i.e.: `\1\e[1 q\e]12;orange\a\2` )
#     0: blinking block
#     1: blinking block (default)
#     2: steady block
#     3: blinking underline
#     4: steady underline
#     5: blinking bar (xterm)
#     6: steady bar (xterm)
#                                 string settings       cursor shape
#                        +------------------------------++--------+
#                           color begin         color end
#                          +------------+         +---+
set emacs-mode-string   "\1\e[38;5;240;1m\2╰╶ ᓆ \1\e[0m\2\1\e[3 q\2"
#                                      string settings              cursor shape
#                        +------------------------------------------++--------+
#                           color begin       color begin   color end
#                          +------------+       +------+      +---+
set vi-ins-mode-string  "\1\e[38;5;240;1m\2╰╶ \1\e[33;1m\2ᓎ \1\e[0m\2\1\e[5 q\2"
set vi-cmd-mode-string  "\1\e[38;5;240;1m\2╰╶ \1\e[34;1m\2ᓏ \1\e[0m\2\1\e[4 q\2"
set show-mode-in-prompt on

# allow iso-latin1 characters to be inserted
set convert-meta                   off
# don't strip characters to 7 bits when reading
set input-meta                     on
set completion-ignore-case         on
set show-all-if-ambiguous          on
set show-all-if-unmodified         on
set mark-symlinked-directories     on
set print-completions-horizontally on
# https://github.com/scop/bash-completion
set visible-stats                  on
set enable-bracketed-paste         off
# https://groups.google.com/g/iterm2-discuss/c/K6YazwKUvjQ/m/7eqeT-AvBgAJ

# TAB: menu-complete
# set colored-completion-prefix on
set colored-stats                  on
set skip-completed-text            on
# ask if more than 100 candidates
# set completion-query-items       100

set keymap emacs
"\ee": vi-editing-mode

# https://www.usenix.org.uk/content/bash.html#input
set keymap vi-command
"\ee": emacs-editing-mode
# key bindings to get out of vi-editing-mode
"v"  : ''
"#": insert-comment
"dw": kill-word
"dd": kill-whole-line
"db": backward-kill-word
"D":kill-line
"da\"": "lF\"df\""
"di\"": "lF\"lmtf\"d`t"
"ci\"": "di\"i"
"da'": "lF'df'"
"di'": "lF'lmtf'd`t"
"ci'": "di'i"
"da`": "lF\`df\`"
"di`": "lF\`lmtf\`d`t"
"ci`": "di`i"
"ca`": "da`i"
"da(": "lF(df)"
"di(": "lF(lmtf)d`t"
"ci(": "di(i"
"ca(": "da(i"
"da)": "lF(df)"
"di)": "lF(lmtf)d`t"
"ci)": "di(i"
"ca)": "da(i"
"da{": "lF{df}"
"di{": "lF{lmtf}d`t"
"ci{": "di{i"
"ca{": "da{i"
"da}": "lF{df}"
"di}": "lF{lmtf}d`t"
"ci}": "di}i"
"ca}": "da}i"
"da[": "lF[df]"
"di[": "lF[lmtf]d`t"
"ci[": "di[i"
"ca[": "da[i"
"da]": "lF[df]"
"di]": "lF[lmtf]d`t"
"ci]": "di]i"
"ca]": "da]i"
"da<": "lF<df>"
"di<": "lF<lmtf>d`t"
"ci<": "di<i"
"ca<": "da<i"
"da>": "lF<df>"
"di>": "lF<lmtf>d`t"
"ci>": "di>i"
"ca>": "da>i"
"da/": "lF/df/"
"di/": "lF/lmtf/d`t"
"ci/": "di/i"
"ca/": "da/i"
"da:": "lF:df:"
"di:": "lF:lmtf:d`t"
"ci:": "di:i"
"ca:": "da:i"
"\C-_": undo
"\C-a": beginning-of-line
"\C-b": backward-char
"\C-d": delete-char
"\C-e": end-of-line
"\C-f": forward-char
"\C-g": abort
"\C-k": kill-line
"\C-l": clear-screen
"\C-p": previous-history
"\C-n": next-history
"\C-w": unix-word-rubout
"\C-q": quoted-insert
"\C-x\C-r": re-read-init-file
"\e#": insert-comment
"\e.": insert-last-argument
"\e.": yank-last-arg

set keymap vi-insert
"\ee": emacs-editing-mode
"\C-_": undo
"\C-a": beginning-of-line
"\C-b": backward-char
"\C-d": delete-char
"\C-e": end-of-line
"\C-f": forward-char
"\C-g": abort
"\C-k": kill-line
"\C-l": clear-screen
"\C-p": previous-history
"\C-n": next-history
"\C-w": unix-word-rubout
"\C-q": quoted-insert
"\C-x\C-r": re-read-init-file
"\e#": insert-comment
"\e.": insert-last-argument
"\e.": yank-last-arg

  • change cursor shape

    [!NOTE|label:references:]

    set emacs-mode-string   \1\e[4 q\e]12;red\a\2
    set vi-ins-mode-string  \1\e[5 q\e]12;green\a\2
    set vi-cmd-mode-string  \1\e[4 q\e]12;orange\a\2
    
    # or
    set  emacs-mode-string "\1\e[3 q\2"
    set vi-ins-mode-string "\1\e[5 q\2"
    set vi-cmd-mode-string "\1\e[4 q\2"

PS1

[!NOTE|label:see also:]

PS1='\n\[\033[1m\]\[\033[38;5;240m\]╭╶ (\u@\h\[\033[1m\] '
PS1+='\[\033[0;31m\]\w\[\033[1m\]\[\033[38;5;240m\]) '
PS1+='$(__git_ps1 "- (\[\033[32;2m\]%s\[\033[0m\]\[\033[38;5;240m\]) ")\[\033[1m\]'
PS1+='\[\033[38;5;240m\]`if [ $? = 0 ]; then echo \[\033[38\;5\;240m\]\-\>; else echo \[\033[0\;31m\]\-\>; fi`\[\033[1m\]'
PS1+='\n\[\033[38;5;240m\] $ \[\033[1m\]\[\033[0m\]'
export PS1

inputrc

set show-mode-in-prompt on
#                                 string settings       cursor shape
#                        +------------------------------++--------+
#                           color begin         color end
#                          +------------+         +---+
set emacs-mode-string   "\1\e[38;5;240;1m\2╰╶ ᓆ \1\e[0m\2\1\e[3 q\2"
#                                      string settings              cursor shape
#                        +------------------------------------------++--------+
#                           color begin       color begin   color end
#                          +------------+       +------+      +---+
set vi-ins-mode-string  "\1\e[38;5;240;1m\2╰╶ \1\e[33;1m\2ᓎ \1\e[0m\2\1\e[5 q\2"
set vi-cmd-mode-string  "\1\e[38;5;240;1m\2╰╶ \1\e[34;1m\2ᓏ \1\e[0m\2\1\e[4 q\2"

set keymap emacs
"\ee": vi-editing-mode

set keymap vi-command
"\ee": emacs-editing-mode

set keymap vi-insert
"\ee": vi-movement-mode
  • example

    $ export PS1=" ┌錄 \[\e[32m\]\u\[\e[m\]\[\e[32m\]@\[\e[m\]\[\e[32m\]\h\[\e[m\] \w \\$ \n "
    $ bind 'set show-mode-in-prompt on'
    $ bind 'set vi-ins-mode-string " └──錄 (ins):"'
    $ bind 'set vi-cmd-mode-string " └──錄 (cmd):"'
  • example

    #################### VIM ####################
    # FOR MORE INFORMATION CHECK:
    # https://wiki.archlinux.org/index.php/Readline
    
    # TURN ON VIM (E.G. FOR READLINE)
    set editing-mode vi
    
    # SHOW THE VIM MODE IN THE PROMPT (COMMAND OR INSERT)
    set show-mode-in-prompt on
    
    # SET THE MODE STRING AND CURSOR TO INDICATE THE VIM MODE
    #   FOR THE NUMBER AFTER `\e[`:
    #     0: blinking block
    #     1: blinking block (default)
    #     2: steady block
    #     3: blinking underline
    #     4: steady underline
    #     5: blinking bar (xterm)
    #     6: steady bar (xterm)
    set vi-ins-mode-string (ins)\1\e[5 q\2
    set vi-cmd-mode-string (cmd)\1\e[1 q\2
  • example

    $ cat ~/.inputrc
    set editing-mode vi
    set vi-ins-mode-string \1\e[5 q\e]12;green\a\2
    set vi-cmd-mode-string \1\e[1 q\e]12;orange\a\2
    set show-mode-in-prompt on
  • example

    # Set the default readline mode as vi
    set editing-mode vi
    
    # Show the vi mode indicators
    set show-mode-in-prompt on
    
    # The following is a little hard to understand
    # a full example omiting the wrapping \1 and \2
    #
    # \e[     (open sequence: ESC CSI)
    # 48;5;   (specifies 256 bg)
    # 2       (bg color)
    # m       (end)
    # 1;      (bold)
    # 38;5;   (specifies 256 fg)
    # 0       (fg color)
    # m       (end)
    # COMMAND (some text to display)
    # \e[     (open sequence)
    # 0       (reset)
    # m       (end)
    # \e[     (open sequence)
    # 0       (cursor type)
    # q       (end)
    
    # Configures the cmd mode display
    set vi-cmd-mode-string "\1\e[48;5;2m\2\1\e[1;38;5;0m\2 N \1\e[0m\2 \1\e[0 q\2"
    
    # Configures the ins mode display
    set vi-ins-mode-string "\1\e[48;5;4m\2\1\e[1;38;5;0m\2 I \1\e[0m\2 \1\e[6 q\2"

show options

# display readline variable names and values can be used as input or in a Readline initialization file
$ bind -v

# list current readline variable names and values
$ bind -v
set bind-tty-special-chars on
set blink-matching-paren off
set byte-oriented off
set colored-completion-prefix off
set colored-stats on
set completion-ignore-case on
set completion-map-case off
set convert-meta off
set disable-completion off
set echo-control-characters on
set enable-active-region off
set enable-bracketed-paste off
set enable-keypad off
set enable-meta-key on
set expand-tilde off
set history-preserve-point off
set horizontal-scroll-mode off
set input-meta on
set mark-directories on
set mark-modified-lines off
set mark-symlinked-directories on
set match-hidden-files on
set menu-complete-display-prefix off
set meta-flag on
set output-meta on
set page-completions on
set prefer-visible-bell on
set print-completions-horizontally on
set revert-all-at-newline off
set show-all-if-ambiguous on
set show-all-if-unmodified on
set show-mode-in-prompt on
set skip-completed-text on
set visible-stats on
set bell-style audible
set comment-begin #
set completion-display-width -1
set completion-prefix-display-length 0
set completion-query-items 100
set editing-mode emacs
set emacs-mode-string ╰╶ 
set history-size 5000
set keymap emacs
set keyseq-timeout 500
set vi-cmd-mode-string ╰╶ 
set vi-ins-mode-string ╰╶ 

options

  • active-region-start-color

  • active-region-end-color

  • bell-style

  • bind-tty-special-chars

  • blink-matching-paren

  • colored-completion-prefix

  • colored-stats

  • comment-begin

  • completion-display-width

  • completion-ignore-case

  • completion-map-case

  • completion-prefix-display-length

  • completion-query-items

  • convert-meta

  • disable-completion

  • echo-control-characters

  • editing-mode

  • emacs-mode-string

  • enable-active-region

  • enable-bracketed-paste

  • enable-keypad

  • enable-meta-key

  • expand-tilde

  • history-preserve-point

  • history-size

  • horizontal-scroll-mode

  • input-meta

  • isearch-terminators

  • keymap

  • keyseq-timeout

  • mark-directories

  • mark-modified-lines

  • mark-symlinked-directories

  • match-hidden-files

  • menu-complete-display-prefix

  • output-meta

  • page-completions

  • print-completions-horizontally

  • revert-all-at-newline

  • show-all-if-ambiguous

  • show-all-if-unmodified

  • show-mode-in-prompt

  • skip-completed-text

  • vi-cmd-mode-string

  • vi-ins-mode-string

  • visible-stats

key bindings

# display readline function names and bindings can be used as input or in a readline initialization file
$ bind -p

# list current readline function names and bindings
$ bind -P

## query
$ bind -p
...
"\e&": tilde-expand
# vi-tilde-expand (not bound)
....

$ bind -q tilde-expand
tilde-expand can be invoked via "\e&".

list all names

$ bind -l

inputrc

# https://www.gnu.org/software/bash/manual/bash.html#index-show_002dmode_002din_002dprompt
set show-mode-in-prompt on
# SET THE MODE STRING AND CURSOR TO INDICATE THE VIM MODE
#   FOR THE NUMBER AFTER `\e[`:
#     0: blinking block
#     1: blinking block (default)
#     2: steady block
#     3: blinking underline
#     4: steady underline
#     5: blinking bar (xterm)
#     6: steady bar (xterm)
set emacs-mode-string  ╰╶ 
set vi-ins-mode-string ╰╶ 
set vi-cmd-mode-string ╰╶ 
set emacs-mode-string  \1\e[38;5;240;1m\2╰╶  \1\e[0m\2
set vi-ins-mode-string \1\e[38;5;240;1m\2╰╶ \1\e[33;1m\2ᓎ \1\e[0m\2
set vi-cmd-mode-string \1\e[38;5;240;1m\2╰╶ \1\e[34;1m\2ᓏ \1\e[0m\2

# allow iso-latin1 characters to be inserted
set convert-meta off
# don't strip characters to 7 bits when reading
set input-meta on
set completion-ignore-case on
set show-all-if-ambiguous on
set show-all-if-unmodified on
set mark-symlinked-directories on
set print-completions-horizontally on
# https://github.com/scop/bash-completion
set visible-stats on
# https://groups.google.com/g/iterm2-discuss/c/K6YazwKUvjQ/m/7eqeT-AvBgAJ
set enable-bracketed-paste off

# TAB: menu-complete
# set colored-completion-prefix on
set colored-stats on
set skip-completed-text on
# ask if more than 100 candidates
# set completion-query-items 100

set keymap emacs
"\ee": vi-editing-mode

set keymap vi-command
"\ee": emacs-editing-mode
"dw": kill-word
"dd": kill-whole-line
"db": backward-kill-word
"D":kill-line
"v"  : ''
"\C-_": undo
"\C-a": beginning-of-line
"\C-b": backward-char
"\C-d": delete-char
"\C-e": end-of-line
"\C-f": forward-char
"\C-g": abort
"\C-k": kill-line
"\C-l": clear-screen
"\C-p": previous-history
"\C-n": next-history
"\C-w": unix-word-rubout
"\C-q": quoted-insert
"\C-x\C-r": re-read-init-file
"\e#": insert-comment
"\e.": insert-last-argument
"\e.": yank-last-arg

# key bindings to get out of vi-editing-mode
set keymap vi-insert
"\ee": emacs-editing-mode
"\C-_": undo
"\C-a": beginning-of-line
"\C-b": backward-char
"\C-d": delete-char
"\C-e": end-of-line
"\C-f": forward-char
"\C-g": abort
"\C-k": kill-line
"\C-l": clear-screen
"\C-p": previous-history
"\C-n": next-history
"\C-w": unix-word-rubout
"\C-q": quoted-insert
"\C-x\C-r": re-read-init-file
"\e#": insert-comment
"\e.": insert-last-argument
"\e.": yank-last-arg
# https://www.gnu.org/software/bash/manual/bash.html#index-show_002dmode_002din_002dprompt
set show-mode-in-prompt on
# SET THE MODE STRING AND CURSOR TO INDICATE THE VIM MODE
#   FOR THE NUMBER AFTER `\e[`:
#     0: blinking block
#     1: blinking block (default)
#     2: steady block
#     3: blinking underline
#     4: steady underline
#     5: blinking bar (xterm)
#     6: steady bar (xterm)

set emacs-mode-string  ╰╶ 
set vi-ins-mode-string ╰╶ 
set vi-cmd-mode-string ╰╶ 
set emacs-mode-string  \1\e[38;5;240;1m\2╰╶  \1\e[0m\2
set vi-ins-mode-string \1\e[38;5;240;1m\2╰╶ \1\e[33;1m\2ᓎ \1\e[0m\2
set vi-cmd-mode-string \1\e[38;5;240;1m\2╰╶ \1\e[34;1m\2ᓏ \1\e[0m\2

####################
# \e : Meta        #
# \C : Control     #
####################

# allow iso-latin1 characters to be inserted
set convert-meta off
# don't strip characters to 7 bits when reading
set input-meta on
set completion-ignore-case on
set show-all-if-ambiguous on
set show-all-if-unmodified on
set mark-symlinked-directories on
set print-completions-horizontally on
# https://github.com/scop/bash-completion
set visible-stats on
# https://groups.google.com/g/iterm2-discuss/c/K6YazwKUvjQ/m/7eqeT-AvBgAJ
set enable-bracketed-paste off

# TAB: menu-complete
# set colored-completion-prefix on
set colored-stats on
set skip-completed-text on
# ask if more than 100 candidates
# set completion-query-items 100

set keymap emacs
"\ee": vi-editing-mode

set keymap vi-command
"\ee": emacs-editing-mode

# key bindings to get out of vi-editing-mode
set keymap vi-insert
"\ee": vi-movement-mode

##### emacs #####
$if mode=emacs
"\ee": vi-editing-mode                       # `Esc-e`:vi-insert. https://unix.stackexchange.com/a/409866/29178
# "\e\e": vi-movement-mode
"\e~": complete-username
"\e_": yank-last-arg
"\e?": possible-completions
Meta-Control-h: backward-kill-word
$endif

##### vi #####
$if mode=vi
"\ee": emacs-editing-mode
$endif

##### command mode #####
$if mode=vi-command
"\ee": emacs-editing-mode

v: ""
Control-a: beginning-of-line
Control-b: backward-char
Control-d: delete-char
Control-e: end-of-line
Control-f: forward-char
Control-k: kill-line
Control-n: next-history
Control-p: previous-history
$endif

##### insert mode #####
$if mode=vi-insert
"\ee": emacs-editing-mode

"\C-k": kill-line
"\C-u": unix-line-discard
"\C-p": previous-history
"\C-n": next-history
"\C-a": beginning-of-line
"\C-e": end-of-line
"\C-f": forward-char
"\C-b": backward-char
"\C-l": clear-screen
"\C-d": delete-char
"\C-h": backward-delete-char
"\C-w": unix-word-rubout

"\et": transpose-words
"\ef": forward-word
"\eb": backward-word
"\ed": kill-word
"\e.":yank-last-arg
"\e_": yank-last-arg
"\C-_": undo
$endif

$if Bash
# edit the path
"\C-xp": "PATH=${PATH}\e\C-e\C-a\ef\C-f"
# Quote the current or previous word
"\C-xq": "\eb\"\ef\""
$endif
$ cat ~/.inputrc
set convert-meta off                          # allow iso-latin1 characters to be inserted
set input-meta on                             # don't strip characters to 7 bits when reading
set completion-ignore-case on
set show-all-if-ambiguous on
set show-all-if-unmodified on
set mark-symlinked-directories on
set print-completions-horizontally on
set enable-bracketed-paste off                # https://groups.google.com/g/iterm2-discuss/c/K6YazwKUvjQ/m/7eqeT-AvBgAJ
# set show-mode-in-prompt on
# TAB: menu-complete
# set colored-completion-prefix on
# set colored-stats on
set skip-completed-text on

# set completion-query-items 100              # ask if more than 100 candidates

# https://unix.stackexchange.com/a/409866/29178
set keymap emacs
$if mode=emacs
"\ee": vi-editing-mode
$endif

set keymap vi-insert
$if mode=vi-insert
"\e": vi-movement-mode
# "jk": vi-movement-mode
# "kj": vi-movement-mode
$endif

# set editing-mode vi

# command mode
set keymap vi-command
$if mode=vi-command
"\ee": emacs-editing-mode

v: ""
Control-a: beginning-of-line
Control-b: backward-char
Control-d: delete-char
Control-e: end-of-line
Control-f: forward-char
Control-k: kill-line
Control-n: next-history
Control-p: previous-history
$endif

# insert mode
set keymap vi-insert

$if mode=vi-insert
"\C-k": kill-line
"\C-u": unix-line-discard
"\C-p": previous-history
"\C-n": next-history
"\C-a": beginning-of-line
"\C-e": end-of-line
"\C-f": forward-char
"\C-b": backward-char
"\C-l": clear-screen
"\C-d": delete-char
"\C-h": backward-delete-char
"\C-w": unix-word-rubout

"\et": transpose-words
"\ef": forward-word
"\eb": backward-word
"\ed": kill-word
"\e.":yank-last-arg
"\e_": yank-last-arg
"\C-_": undo
$endif
$ cat /etc/inputrc
# do not bell on tab-completion
#set bell-style none

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

# Completed names which are symbolic links to
# directories have a slash appended.
set mark-symlinked-directories on

$if mode=emacs

# for linux console and RH/Debian xterm
"\e[1~": beginning-of-line
"\e[4~": end-of-line
# commented out keymappings for pgup/pgdown to reach begin/end of history
#"\e[5~": beginning-of-history
#"\e[6~": end-of-history
"\e[5~": history-search-backward
"\e[6~": history-search-forward
"\e[3~": delete-char
"\e[2~": quoted-insert
"\e[5C": forward-word
"\e[5D": backward-word
"\e[1;5C": forward-word
"\e[1;5D": backward-word

# for rxvt
"\e[8~": end-of-line
"\eOc": forward-word
"\eOd": backward-word

# for non RH/Debian xterm, can't hurt for RH/DEbian xterm
"\eOH": beginning-of-line
"\eOF": end-of-line

# for freebsd console
"\e[H": beginning-of-line
"\e[F": end-of-line
$endif

unbind

[!NOTE]

# .bashrc
stty werase undef

# .inputrc
bind '"\C-w":kill-region'

tips

  • How to move the cursor word by word in the OS X Terminal

    $ bind -q backward-word
    backward-word can be invoked via "\M-b".
    
    $ bind -q forward-word
    forward-word can be invoked via "\M-f".
  • re-read-init-file

    $ bind -q re-read-init-file
    re-read-init-file can be invoked via "\C-x\C-r".
    
    # or https://unix.stackexchange.com/a/246422/29178
    $ bind -f ~/.inputrc
  • glob-list-expansions

    $ bind -q glob-list-expansions
    glob-list-expansions can be invoked via "\C-xg"
  • insert-comment

    $ bind -q insert-comment
    insert-comment can be invoked via "\e#".
  • insert-last-argument

    $ bind -q insert-last-argument
    insert-last-argument can be invoked via "\e.", "\e_".
  • upcase-word && downcase-word && capitalize-word

    $ bind -q upcase-word
    upcase-word can be invoked via "\eu".
    
    $ bind -q downcase-word
    downcase-word can be invoked via "\el"
    
    $ bind -q capitalize-word
    capitalize-word can be invoked via "\ec".
  • block cursor

    set editing-mode vi
    set show-mode-in-prompt on
    set vi-ins-mode-string \1\e[6 q\2
    set vi-cmd-mode-string \1\e[2 q\2
    
    # optionally:
    # switch to block cursor before executing a command
    set keymap vi-insert
    RETURN: "\e\n"
  • undo

    $ bind -q undo
    undo can be invoked via "\C-x\C-u", "\C-_".
  • hell-expand-line

    $ bind -q shell-expand-line
    shell-expand-line can be invoked via "\M-\C-e".
  • display-shell-version

    $ bind -q display-shell-version
    display-shell-version can be invoked via "\C-x\C-v".

Last updated