Skip to content

Manual §7 Completion

Koichi Murase edited this page Apr 27, 2024 · 42 revisions

[ 日本語 | English ] ≫ Manual [§1 Intro | §2 Color | §3 Bind | §4 Edit | §5 Emacs | §6 Vim | §7 Comp | §8 Misc]

7. Completion

The settings for completion need to be performed through the hook complete_load or the callback for ble-import core-complete. The following is an example to use the complete_load hook to configure bleopt complete_auto_delay:

# blerc

function my/complete-load-hook {
  bleopt complete_auto_delay=300
}

blehook/eval-after-load complete my/complete-load-hook
# ble-0.3 and before
# ble/array#push _ble_complete_load_hook my/complete-load-hook

The following is an example to use the ble-import callback (ble >= 0.4):

# blerc

function my/set-up-completion {
  bleopt complete_auto_delay=300
}

ble-import core-complete -C 'my/set-up-completion'

7.1 Starting completion (complete)

Key combination Widget Description
C-i, TAB complete Start complete
M-? (Emacs) complete show_menu Show list of completions
M-* (Emacs) complete insert_all Insert all the completions
M-{ (Emacs) complete insert_braces Insert all the completions with brace expansion
C-TAB menu-complete Start menu-complete
S-C-i, S-TAB menu-complete backward Start menu-complete initially selecting the last completion
M-/ (Emacs) complete context=filename Start completing filenames
M-~ (Emacs) complete context=username Start completing user names
M-$ (Emacs) complete context=variable Start completing variables
M-@ (Emacs) complete context=hostname Start completing hostnames
M-! (Emacs) complete context=command Start completing commands
C-x / (Emacs) complete show_menu:context=filename Show list of filename completions
C-x ~ (Emacs) complete show_menu:context=username Show list of user name completions
C-x $ (Emacs) complete show_menu:context=variable Show list of variable completions
C-x @ (Emacs) complete show_menu:context=hostname Show list of hostname completions
C-x ! (Emacs) complete show_menu:context=command Show list of command completions
C-x / (Vim) menu-complete context=filename Start menu-complete of filenames
C-x ~ (Vim) menu-complete context=username Start menu-complete of user names
C-x $ (Vim) menu-complete context=variable Start menu-complete of variables
C-x @ (Vim) menu-complete context=hostname Start menu-complete of hostnames
C-x ! (Vim) menu-complete context=command Start menu-complete of commands
M-g (Emacs) complete context=glob Perform pathname expansion
C-x * complete context=glob:insert_all Perform pathname expansion to insert all
C-x g complete context=glob:show_menu Show list of the result of pathname expansion
C-M-i, M-TAB complete context=dynamic-history Complete by words in history

7.1.1 Widget complete

widget: complete $opts

OPTIONS

  $opts   a colon separated list of fields

    insert_common
    insert_all
    insert_braces
    insert_unique
    toggle_menu
    enter_menu
    show_menu
    context=CONTEXT
    backward
    no-empty
    no-bell

By default, completion can be trigger by C-i or TAB. When the menu of candidates are already shown, it enters the menu completion mode (described later). The first step of the process of the completion is to generate possible candidates. Initially the programmable completion from the setting by Bash builtin complete are checked, and if it is not available the candidate generator built in ble.sh is used instead. If there are no possible candidates, ambiguous candidate generation is performed. The next step is to insert the candidates.

  • When insert_common is specified or no other insertion method is specified, ble.sh inserts the longest common prefix among possible candidates and the existing text. In addition, when the possible candidate is unique, the unique candidate is inserted, and the completion is finished. When there are multiple possible candidates, a menu of the possible candidates is shown.
  • When insert_all is specified, all the candidates are inserted in the command line, and the completion is finished.
  • When insert_braces is specified, all the candidates are inserted with a compact form by contracting common parts by brace expansions, and the completion is finished.
  • When toggle_menu is specified, the completion menu is shown if it is not shown. The completion menu is hidden if it is shown.
  • When enter_menu is specified, the completion menu is shown with the first item being selected, and ble.sh enters the menu completion mode. If backward is specified, the last item is selected instead of the first item. If insert_unique is specified and the generated candidate is unique, intead of entering the menu, the unique candidate is inserted, and the completion is finished.
  • When show_menu is specified, the completion menu is just shown.

When the completion is finished, suffixes and closing quotations are inserted to each inserted candidate if needed.

When M-? is pressed in Emacs editing mode (or when the widget complete receives show_menu as an argument), the menu of possible completions is shown instead of inserting a common prefix. When C-TAB is used (or when the widget complete receives enter_menu as an argument), the menu completion is started instead of inserting a common prefix.

When M-/, M-~, M-$, M-@ or M-! is pressed in Emacs editing mode (or when the widget receives a context argument, context=filename, context=username, context=variable, context=hostname or context=command), possible completions for filenames, users, variables, hostnames or commands are generated, respectively. When C-x /, C-x ~, C-x $, C-x @ or C-x ! is pressed in Emacs editing mode, (or when the widget receives show_menu as an additional argument to the context argument), the corresponding completions are show in a menu instead of inserting a common prefix. When C-x /, C-x ~, C-x $, C-x @, C-x ! is pressed in Vim editing mode, (or when the widget receives enter_menu as an additional argument to the context argument), the menu completion is started with the corresponding completions instead of inserting a common prefix.

7.1.2 Widget menu-complete

widget: menu-complete $opts

This is the alias to the widget complete enter_menu:insert_unique:$opts. Please refer to the description on the widget complete.

7.1.3 Bleopt complete_ambiguous (Empty/Non-empty) (v0.3)

# default
bleopt complete_ambiguous=1

If this has a non-empty value, completions ambiguously matching to the current text are generated. If this has the empty value, ambiguous completion is disabled.

7.1.4 Bleopt complete_contract_function_names (Empty/Non-empty) (v0.3)

# default
bleopt complete_contract_function_names=1

If this has a non-empty value, function names are split by / just like the file paths, and first fragment is used for completions. If this has the empty value, the whole function names are used for completions.

7.1.5 Bleopt complete_polling_cycle (Arithmetic) (v0.2)

# default
bleopt complete_polling_cycle=50     # ble-0.3
bleopt complete_stdin_frequency=50   # ble-0.2 (obsoleted in ble-0.3)

In ble.sh, the completion is canceled when there is a user input during the completion generation to respond the user's input. This setting controls the interval of user input checks in the unit of the number of processed completions.

7.1.6 Bleopt complete_limit (Arithmetic/Empty) (v0.4)

# default
bleopt complete_limit=

This options gives a hint on the maximum acceptable size of any data structure generated during the completion process, beyond which the completion may be prematurely aborted to avoid excessive processing time. When its value is empty, the size checks are disabled. This setting is particularly intended for the normal (TAB) completion. For auto-completion, please also see complete_limit_auto below.

7.1.7 Bleopt complete_limit_auto (Arithmetic/Empty) (v0.4)

# default
bleopt complete_limit_auto=2000

This option is the same as complete_limit but is used for auto-complete. When its value is empty, the setting of complete_limit is used.

7.1.8 Bleopt complete_limit_auto_menu (Arithmetic/Empty) (v0.4)

# default
bleopt complete_limit_auto_menu=100

This option is the same as complete_limit but is used for auto-complete. When its value is empty, the setting of complete_limit is used.

7.1.9 Bleopt complete_timeout_auto (Arithmetic/Empty) (v0.4)

# default
bleopt complete_timeout_auto=5000

This setting controls the timeout for the pathname expansions performed in auto-complete. When the word contains a glob pattern that takes a long time to evaluate the pathname expansion, auto-complete based on the filename is canceled based on the timeout setting. The value specifies the timeout duration in milliseconds. When the value is empty, the timeout is disabled.

7.1.10 Bleopt complete_timeout_compvar (Arithmetic/Empty) (v0.4)

# default
bleopt complete_timeout_compvar=200

This setting controls the timeout for the pathname expansions to prepare COMP_WORDS and COMP_LINE for progcomp. When the word contains a glob pattern that takes a long time to evaluate, the pathname expansion is canceled, and a noglob expansion is used to construct COMP_WORDS and COMP_LINE. The value specifies ## the timeout duration in milliseconds. When the value is empty, the timeout is disabled.

7.1.11 Bleopt complete_allow_reduction (Empty/Non-empty) (v0.4)

# default
bleopt complete_allow_reduction=     # ble-0.4

When the generated completion does not contain the already existing text, The non-empty value of this setting allows the rewrite of the existing text. When this setting has the empty value, it allows the rewrite only when the possible completion is unique.

7.1.12 Bleopt complete_requote_threshold (Arithmetic) (v0.4)

# default
bleopt complete_requote_threshold=0

This option specifies the threshold to simplify the quotation type of the inserted word. When this option evaluates to a negative value, the simplification of the quotation is disabled. Otherwise, when the number of characters will be reduced by at least the specified value, the quotation is simplified. The default is 0, which means that the quotation is simplified unless the number of characters increases by the simplification.

7.1.13 Shopt no_empty_cmd_completion

# bashrc

# default
shopt -u no_empty_cmd_completion

When this Bash setting is enabled, the command name generation using the empty text is disabled.

7.1.14 Shopt force_fignore

# bashrc

# default
shopt -s force_fignore

When this Bash setting is enabled, all kinds of generated completions, not restricted in pathnames, are filtered by the shell variable FIGNORE. The completions that have one of the suffixes specified in the colon-separated value of FIGNORE will be removed.

7.1.15 Readline variable completion-ignore-case (v0.3)

# bashrc

# default
bind 'set completion-ignore-case off'

When this Readline setting is enabled, the cases of alphabets are ignored on completion generation.

7.1.16 Bleopt completion_skip_matched (Empty/Non-empty) (v0.3)

# bashrc

# default
bleopt complete_skip_matched=on    # ble-0.4 and later
bind 'set skip-completed-text on'
bind 'set skip-completed-text off' # ble-0.3

When a non-empty value is specified, the matching text on the right of the cursor is removed on the insertion of the completion. This setting is synchronized with the readline variable skip-completed-text.

7.1.17 Readline variable visible-stats (v0.3)

# bashrc

# default
bind 'set visible-stats off'

When this Readline setting is turned on, suffixes are added to the filename completions in the menu. The characters @, / and * are added to symbolic links, directories and executables, respectively.

7.1.18 Readline variable mark-directories (v0.3)

# bashrc

# default
bind 'set mark-directories on'

When this Readline setting is turned on, the suffix / is inserted after the insertion of directory names.

7.1.19 Readline variable mark-symlinked-directories (v0.3)

# bashrc

# default
bind 'set mark-symlinked-directories on'

When this Readline setting is turned on, the suffix / is inserted after symbolic links pointing to directories.

7.1.20 Readline variable match-hidden-files (v0.3)

# bashrc

# default
bind 'set match-hidden-files on'

When this Readline setting is turned on, the filenames starting with . is also generated as possible completions.

7.1.21 Readline variable menu-complete-display-prefix (v0.3)

# bashrc

# default
bind 'set menu-complete-display-prefix off'

When filenames of the form dir/file* is shown in the menu, the part of the directory name dir/ is omitted by default. When this Readline setting is turned on, the directory name of filename completions are not omitted.

7.1.22 Bleopt complete_source_sabbrev_ignore (Colon-separated glob) (v0.4)

# default
bleopt complete_source_sabbrev_ignore=

This specifies a colon-separated list of glob patterns of sabbrev names ignored in generating the sabbrev completion candidates.

7.1.23 Bleopt complete_source_sabbrev_opts (opts) (v0.4)

# default
bleopt complete_source_sabbrev_opts=

This is a colon-separated list of options. When the field no-empty-completion is specified, the sabbrev completion candidates are not generated when the word to complete is empty.

7.2 Menu completion (menu-complete)

When the menu is shown, C-i or TAB leads to the menu completion mode. The dedicated key bindings (such as C-TAB, and C-x /, C-x ~, C-x $, C-x @ and C-x ! in Vim editing-mode) can also be used to enter the menu completion mode. In the menu completion mode, the following bindings are available.

Key combination Widget Description
C-m, RET menu_complete/accept Accept
C-g menu_complete/cancel Cancel
__default__ menu_complete/exit-default Exit menu-complete and do default operation
C-f, right menu/forward Next completion
C-i, TAB menu/forward cyclic Next completion (cyclic)
C-b, left menu/backward Previous completion
C-S-i, S-TAB menu/backward cyclic Previous completion (cyclic)
C-n, down menu/forward-line Go to completion at next line
C-p, up menu/backward-line Go to completion at previous line
prior menu/backward-page Go to the previous page
next menu/forward-page Go to the next page
home menu/beginning-of-page Go to the beginning of the page
end menu/end-of-page-page Go to the end of the page
C-x SP menu_complete/toggle-hidden Toggle visibility of the completion menu
C-x right, C-x C-n menu_complete/switch-style + Use the next menu style
C-x left, C-x C-p menu_complete/switch-style - Use the previous menu style
C-x a menu_complete/switch-style align-nowrap Use the menu style align-nowrap
C-x c menu_complete/switch-style dense-nowrap Use the menu style dense-nowrap
C-x d menu_complete/switch-style desc Use the menu style desc
C-x l menu_complete/switch-style linewise Use the menu style linewise

7.2.1 Bleopt complete_menu_complete (Empty/Non-empty)

# default
bleopt complete_menu_complete=1

If a non-empty value is set, C-i and TAB (or the widget complete) triggers the menu completion mode when the menu is shown. If this has the empty value, a normal completion is tried again instead of entering the menu completion.

7.2.2 Bleopt complete_menu_complete_opts (Colon-separated list) (v0.4)

# default
bleopt complete_menu_complete_opts=insert-selection

This setting controls the detailed behavior of menu-complete with a colon-separated list of options: When the option insert-selection is specified, the currently selected menu item is temporarily inserted in the command line. When hidden is specified, insert-selection is automatically enabled. In addition, the completion menu is by default hidden in the menu-complete mode so that only the temporary insertion in the command line is shown.

7.2.3 Bleopt complete_menu_style (v0.3)

# default
bleopt complete_menu_style=align-nowrap

This setting controls the style of the menu. When the value dense is specified, all the possible completions are shown separated by single spaces. The completion can be broken into two lines at the end of lines. The value dense-nowrap is the version with word wrapping, i.e., it inserts newlines before the completion that does not fit into the line. When the value align is specified, completions are aligned in rows by inserting proper amount of spaces. The value align-nowrap is the version with word wrapping. When the value linewise is specified, each line show one completion. When the value desc-text is specified, each line show one completion with a description. Control characters in descriptions are replaced with some visible representation. When the value desc is specified, each line show one completion with a description that contains control characters and escape sequences.

7.2.4 Bleopt complete_menu_maxlines (Arithmetic) (v0.4)

# default
bleopt complete_menu_maxlines=-1

When this value is evaluated to be a positive integer, the maximal line number of the menu is limited to that value.

7.2.5 Bleopt menu_align_max (Arithmetic) (v0.3)

# default
bleopt menu_align_max=20      # ble-0.4
bleopt complete_menu_align=20 # ble-0.3 (obsoleted in ble-0.4)

This setting is used when blesh option complete_menu_style has the value align or align-wrap. This specifies the maximal unit width of the alignment.

7.2.6 Bleopt menu_align_min (Arithmetic) (v0.4)

# default
bleopt menu_align_min=4

This setting is used when blesh option complete_menu_style has the value align or align-wrap. This specifies the minimal unit width of the alignment.

7.2.7 Bleopt menu{,_align,_desc,_linewise,_dense}_prefix (printf format/ANSI) (v0.4)

# default
bleopt menu_prefix=
bleopt menu_align_prefix=
bleopt menu_desc_prefix=
bleopt menu_linewise_prefix=
bleopt menu_dense_prefix=

# Example
bleopt menu_align_prefix='\e[1m%d:\e[m '
bleopt menu_desc_prefix='\e[1m%d.\e[m '
bleopt menu_dense_prefix='\e[1;32m>\e[m '
bleopt menu_linewise_prefix='\e[1;36m%d:\e[m '

These settings can be used to specify the prefix of the menu items. menu_prefix specifies the default prefix for any menu-style. menu_{align,desc,linewise,dense}_prefix specify the prefixes in the respective menu-styles. The value is specified by a printf format, where the first argument is the index of the candidate. The ANSI escape sequences can also be used.

7.2.8 Bleopt menu_desc_multicolumn_width (Empty/Arithmetic) (v0.4)

# default
bleopt menu_desc_multicolumn_width=65

# Example: disable multicolumn
bleopt menu_desc_multicolumn_width=

This setting specifies the minimum column width for the multicolumn description for complete_menu_style=desc. When the empty value is specified, the multicolumn mode is disabled.

7.2.9 Bleopt complete_menu_color (Empty/Non-empty) (v0.4)

# bashrc

# default
bleopt complete_menu_color=on
bind 'set colored-stats on'

When a non-empty value is specified, the highlighting of the menu items is enabled. This setting is synchronized with the readline variable colored-stats.

7.2.10 Bleopt complete_menu_color_match (Empty/Non-empty) (v0.4)

# bashrc

# default
bleopt complete_menu_color_match=on
bind 'set colored-completion-prefix on'

When a non-empty value is specified, the part of the menu items matching with the already input text is highlighted. This setting is synchronized with the readline variable colored-completion-prefix.

7.2.11 Face menu_desc_default (v0.4)

# default
ble-face menu_desc_default=none

This face specifies the default graphic style of texts in the menu descriptions.

7.2.12 Face menu_desc_type (v0.4)

# default
ble-face menu_desc_type=ref:syntax_delimiter

This face specifies the graphic style of type indicators of the form (type) in the menu descriptions, which represents the type of the menu item.

7.2.13 Face menu_desc_quote (v0.4)

# default
ble-face menu_desc_quote=ref:syntax_quoted

This face specifies the graphic style of quoted strings in the menu descriptions.

7.2.14 Face cmdinfo_cd_cdpath (v0.4)

The face for the menu items generated based on CDPATH.

# default
ble-face cmdinfo_cd_cdpath='fg=26,bg=155'

7.3 Auto completions (auto-complete) (Bash 4.0+) (v0.3)

In Bash 4.0 and later, auto completion is enabled to suggest a possible completion automatically.

Key combination Widget Description
C-g auto_complete/cancel Cancel auto completion
S-RET, S-C-m auto_complete/insert Insert the current completion
C-f, right, C-e, end auto_complete/insert-on-end Insert on the end of line or otherwise cancel and do the default
M-f, M-right (Emacs) auto_complete/insert-word Insert the first word in completion
C-j, C-RET auto_complete/accept-line Insert the current completion and execute the command line
__defchar__ auto_complete/self-insert Insert a character
__default__ auto_complete/cancel-default Cancel auto completion and do the default

7.3.1 Bleopt complete_auto_complete (Empty/Non-empty) (v0.3)

# default
bleopt complete_auto_complete=1

When this has a non-empty value, auto completion is enabled. When this has the empty value, auto completion is disabled.

7.3.2 Bleopt complete_auto_delay (Arithmetic) (v0.3)

# default
bleopt complete_auto_delay=1

The delay to start auto completion after the user made the last input. The unit is millisecond.

7.3.3 Bleopt complete_auto_history (Empty/Non-empty) (v0.3)

# default
bleopt complete_auto_history=1

When this has a non-empty value, auto completions are generated from the history. The history entries containing the current text as a prefix are generated as a completion.

7.3.4 Bleopt complete_auto_wordbreaks (Character set) (v0.4)

# default
bleopt complete_auto_wordbreaks=$' \t\n'

This setting is used to identify the words used in M-right (auto_complete/insert-word). This specifies a set of the characters that breaks the completion into words.

7.3.5 Bleopt complete_auto_complete_opts (Colon-separated list) (v0.4)

# default
bleopt complete_auto_complete_opts=

This setting controls the detailed behavior of auto-complete with a colon-separated list of options:

  • When the option suppress-after-complete is specified, auto-complete is enabled after TAB completions. Otherwise, auto-complete is disabled after TAB completions.

7.3.6 Bleopt complete_auto_menu (Empty/Arithmetic) (v0.4)

# default
bleopt complete_auto_menu=

This setting controls the delay of auto-menu. When a non-empty string is set, auto-menu is enabled. The string is evaluated as an arithmetic expression to give the delay in milliseconds. ble.sh will automatically show the menu of completions after the idle time (for which user input does not arrive) reaches the delay.

7.3.7 Face auto_complete (v0.3)

# default
ble-face auto_complete='bg=254,fg=238'

This face is used to show the suggested strings by auto completion.

7.4 Filtering menu (menu-filter) (Bash 4.0+) (v0.3)

In Bash 4.0 and later, menu filter is enabled. When the menu is shown, the completions shown in the menu are dynamically filtered according to the newly input texts.

7.4.1 Bleopt complete_menu_filter (Empty/Non-empty) (v0.3)

# default
bleopt complete_menu_filter=1

When this setting has a non-empty value, menu filtering is enabled. When this setting has the empty value, menu filtering is disabled.

7.4.2 Face menu_filter_fixed (v0.3)

# default
ble-face menu_filter_fixed=bold

This face is used to show a fixed part of menu filtering.

7.4.3 Face menu_filter_input (v0.3)

# default
ble-face menu_filter_input='fg=16,bg=229'

This face is used to show a range of the input text which is used for menu filtering.

7.5 Static abbreviation expansion (sabbrev)

Key combination Widget Description
SP magic-space Perform various expansions including sabbrev and insert a space
/ magic-slash Perform named directory expansion (sabbrev expansion starting with ~) and insert a slash
M-', C-x ' (Emacs)
C-] (Vim)
sabbrev-expand Perform sabbrev expansion

The sabbrev expansion provides the feature like zsh-abbreviations. The sabbrev expansion can be performed by SP (widget magic-space) or M-', C-x ' (Emacs), C-] in Vim editing mode (widget sabbrev-expand).

7.5.1 Function ble-sabbrev

# hook:complete_load

ble-sabbrev [-wmil] [KEY=VALUE|KEY=]...

The function ble-sabbrev is used to define sabbrev expansions. In each argument, the left hand side of the left most = is regarded as KEY and the right hand side is regarded as VALUE. This registers an expansion from a word KEY to VALUE. When VALUE is omitted, the sabbrev expansion assigned to KEY is removed.

  • Word sabbrev: When the option -w is specified or no other types are specified, KEY matches a word before the current position.
  • Dynamic sabbrev: When the option -m is specified, the right hand side is regarded as COMMAND. On the expansion, COMMAND is executed as a shell command and the completion is started by the generated completions. The command COMMAND can store the generated completions in the array COMPREPLY or use the function ble/complete/cand/yield to register them.
  • Inline sabbrev: When the option -i is specified, KEY can match any length of substring ending at the current position.
  • Line sabbrev: When the option -l is specified, KEY matches the current line before the current position.
  • Suffix sabrev: When the option -s is specified, KEY matches a filename extension of the command name that does not match any existing command names. KEY does not include the dot . that separates the extension in the filename. VALUE is inserted before the command name during the expansion. This emulates the behavior of Zsh's suffix aliases.
# hook:complete_load

# Example
ble-sabbrev L='| less'
ble-sabbrev '\date'='date +%F'

The sabbrev expansion may also be used to approximate Zsh's named directories. Although Bash does not expands ~named_dir (with named_dir not being a user name) in the tilde expansions, ble.sh expands ~named_dir in the command line on pressing SP or / when the sabbrev expansion ~name_dir is defined.

# hook:complete_load

# Example
ble-sabbrev "~mybin=$HOME/bin"
ble-sabbrev "~local=$HOME/.local"

7.6 Dynamic abbreviation expansion (dabbrev)

The dabbrev expansion searches a completion that matches with the current word as a prefix from the words in history separated by spaces or the characters in COMP_WORDBREAKS. The key sequence C-x C-r starts the dabbrev expansion mode.

Key combination Widget Description
C-x C-r dabbrev-expand Start dabbrev expansion

In dabbrev expansion mode, the following bindings are available.

Key combination Widget Description
C-r dabbrev/next Go to next expansion
C-s dabbrev/prev Go to previous expansion
C-g dabbrev/cancel Cancel
C-m, RET dabbrev/exit Exit with the current expansion
C-j, C-RET dabbrev/exit Exit with the current expansion and perform RET
__default__ dabbrev/exit-default Exit with the current expansion and do the default

[ 日本語 | English ] ≫ Manual [§1 Intro | §2 Color | §3 Bind | §4 Edit | §5 Emacs | §6 Vim | §7 Comp | §8 Misc]

Clone this wiki locally