Skip to content

Instantly share code, notes, and snippets.

Last active Feb 13, 2021
What would you like to do?
Print the colorized type of a command via bat

Print the colorized type of a command via bat.

This Fish function wraps the builtin type function, and colorizes its output using bat's syntax highlighting.


# Install the function
$ curl -sL --output $__fish_config_dir/functions/

# (optional) Install completions
$ curl -sL --output $__fish_config_dir/completions/
complete -c btype -a "(builtin -n)" -d "Builtin"
complete -c btype -a "(functions -n)" -d "Function"
complete -c btype -a "(__fish_complete_command)" -x
function btype --argument-names name --description 'Print the colorized type of a command'
# Require a command name.
test -n "$name"; or return 1
set --local func (builtin functions --details --verbose $name)
set --local func_path $func[1]
set --local start_line $func[3]
# When the command name is not a known function, there is nothing to enhance.
if contains -- "$func_path" "-" "n/a" "stdin"
type $name
return $status
# Show function definition in pager.
if test $start_line -gt 1
# Workaround to fix line numbering offset in bat pager.
string join -- \n (string repeat --no-newline --count (math $start_line - 1) \n) (type $name | tail -n +3) | bat --language=fish --file-name="$func_path" --line-range="$start_line:"
type $name | tail -n +3 | bat --language=fish --file-name="$func_path"
return $status
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment