Skip to content

Automatically add closing pairs when typing in Bash's prompt

License

Notifications You must be signed in to change notification settings

nkakouros-original/bash-autopairs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 

Repository files navigation

bash-autopairs

A Bash plugin to automatically add closing pairs when typing in Bash's prompt.

This is useful for people who:

  • are tired of typing the closing parts in commands like: command_here "$(other_command "${array[2]}")",
  • may forget to add them.

Limitations

  • The plugin will disable the blink-matching-paren readline option as it seems to hijack the closing characters.
  • If BASH_AUTOPAIR_BACKSPACE is set, the plugin will disable the bind-tty-special-chars option as it prevents Backspace from being mapped.

These limitations cannot be overcome to the best of the author's knowledge.

Installation

Download and source the autopairs.sh file from within your .bashrc file. For instance:

# .bashrc file
source ~/.config/bash/autopairs.sh

To enable Backspace, add the line:

export BASH_AUTOPAIR_BACKSPACE=1

Usage

Once installed, the "plugin" will automatically insert closing pairs whenever you type the opening part of any of the below parts:

""
''
``
()
[]
{}

Escaped characters won't be auto paired to let you compose literals with ease. Try typing:

sed "s/\"\'//g"

It will also delete pairs using C-h and Baskspace.

For instance, try typing and then deleting the following:

echo "$(echo "${var[@]}")"

Moreover, it will insert double spaces when adding a space inside brackets and parentheses. For instance, typing [ followed by a space will result in:

[ | ]  # where | is the cursor

This is compatible with the magic-space readline option. Backspace/Control-h will delete both spaces and result in []. If, while at [ | ], you press ], the cursor will jump over the space and land you into [ ]|. For instance, try typing the below:

if [[ "$my_var" == 1 ]]
#  press `]` here  ^ (after you typed `1`)

Author

Nikolaos Kakouros

License

GPLv3