Fish-like autosuggestions in bash shell

3 min read
Fish-like autosuggestions in bash shell
Photo by Gabriel Heinzer / Unsplash

In my previous post I introduced you to fish shell. I suggest you to read it before continue here, especially the part about autosuggestions.

If you prefer to use bash and have the same features like autosuggestions, here is a detailed guide how to set it up. In this post I will show you how to install and configure fish-like autosuggestions in bash itself in Ubuntu Linux.

Lets begin!

Installation of Ble.sh

Bash Line Editor is a full-featured line editor written in pure Bash! Syntax highlighting, auto suggestions, vim modes, etc. are available in Bash interactive sessions!

Here you can find more ways for installation, but I think the easiest one is by  Downloading the nightly build with curl

$ curl -L https://github.com/akinomyoga/ble.sh/releases/download/nightly/ble-nightly.tar.xz | tar xJf -
$ mkdir -p ~/.local/share/blesh
$ mv -f ble-nightly*/* ~/.local/share/blesh/
$ echo 'source ~/.local/share/blesh/ble.sh' >> ~/.bashrc
$ source ~/.bashrc

Configuration

Here, some of the settings for ~/.blerc are picked up. You can find useful settings also in Q&A, Recipes and contrib repository. The complete list of setting items can be found in the template blerc. For detailed explanations please refer to Manual.

To disable some of the settings use:

$ nano ~/.blerc

Here you can add your custom styles:

# custom styles
ble-face -s syntax_default fg=253
ble-face -s auto_complete fg=242,bg=none
ble-face -s filename_directory underline,fg=252
ble-face -s command_builtin fg=144
ble-face -s command_file fg=144
ble-face -s command_function fg=144
ble-face -s command_alias fg=144
ble-face -s syntax_error fg=1,bg=none
ble-face -s argument_option fg=253
ble-face -s filename_url fg=253
ble-face -s syntax_delimiter fg=73
ble-face -s region_insert fg=253,bg=none
ble-face -s syntax_history_expansion fg=253,bg=none

To check current configuration run $ ble-face and $ ble-color-show to see all colors.

Also, if you want to disable a specific feature or add more customizations, add one or more of this lines in your ~/.blerc:

# Disable syntax highlighting
bleopt highlight_syntax=

# Disable highlighting based on filenames
bleopt highlight_filename=

# Disable highlighting based on variable types
bleopt highlight_variable=

# Disable auto-complete (Note: auto-complete is enabled by default in bash-4.0+)
bleopt complete_auto_complete=
# Tip: you may instead specify the delay of auto-complete in millisecond
bleopt complete_auto_delay=300

# Disable auto-complete based on the command history
bleopt complete_auto_history=

# Disable ambiguous completion
bleopt complete_ambiguous=

# Disable menu-complete by TAB
bleopt complete_menu_complete=

# Disable menu filtering (Note: auto-complete is enabled by default in bash-4.0+)
bleopt complete_menu_filter=

# Disable EOF marker like "[ble: EOF]"
bleopt prompt_eol_mark=''
# Tip: you may instead specify another string:
bleopt prompt_eol_mark='⏎'

# Disable error exit marker like "[ble: exit %d]"
bleopt exec_errexit_mark=
# Tip: you may instead specify another string:
bleopt exec_errexit_mark=$'\e[91m[error %d]\e[m'

# Disable elapsed-time marker like "[ble: elapsed 1.203s (CPU 0.4%)]"
bleopt exec_elapsed_mark=
# Tip: you may instead specify another string
bleopt exec_elapsed_mark=$'\e[94m[%ss (%s %%)]\e[m'
# Tip: you may instead change the threshold of showing the mark
bleopt exec_elapsed_enabled='sys+usr>=10*60*1000' # e.g. ten minutes for total CPU usage

Check more of these templates here.

Finally set bash as default shell if you're currently using other:
$ chsh -s $(which bash)

You must log out and log back in to see this change.

Install OMB (optional)

Oh My Bash is an open source, community-driven framework for managing your bash configuration. If you want to have more freedom, customization and ability to add themes, plugins and many more things to your bash shell - OMB is your solution.

Installation is simple

Just copy-paste and run this command:

$ bash -c "$(curl -fsSL https://raw.githubusercontent.com/ohmybash/oh-my-bash/master/tools/install.sh)"

Configuration

After installation, Oh My Bash replaces ~/.bashrc with it's own version. The original .bashrc is backed up with the name ~/.bashrc.omb-TIMESTAMP.

To include old ~/.bashrc run this command:

echo 'source ~/.bashrc.omb-backup-*' >> ~/.bashrc

Let's tweak some of the OMB settings:

$ nano ~/.bashrc

First I restore the custom theme that we created when installing ble.sh by leaving OSH_THEME variable to be equal to empty string like that:

OSH_THEME=""

If you want another theme, view full list here!

Secondly I add this line at the end of the file to show full file paths in the prompt:

unset PROMPT_DIRTRIM

# without showing full paths
user@machine:.../Files/Photos$

# after
user@machine:/media/user/Files/Photos$

Finally I just add some plugins like 'nvm', 'git', 'kubectl', 'npm', etc. Check the full list here.

And you're done!

Harduex blog


Follow