Bat: [fzf] Bat is awesome, but

Created on 14 Oct 2018  路  17Comments  路  Source: sharkdp/bat

It's so awesome, but whyyyyy

Whenever bat detects a non-interactive terminal, it will fall back to printing the plain file contents.

I wanted to use bat instead of highlight in fzf, since it can use cat for previewing files, but this line kills the usage for me unfortunately. I don't use cat as standalone app at all, but as embedded one, and it's unfortunately that fzf treated as non-interactive terminal, however it supports scrolling. Or maybe I should open issue on fzf side?

question

Most helpful comment

It's so awesome, but whyyyyy

Whenever bat detects a non-interactive terminal, it will fall back to printing the plain file contents.

Because we want bat to be a drop-in replacement for cat. For example, you usually wouldn't want ANSI escape characters (that are used for colorized text in the terminal) to end up in a text file when you call bat file_a.js file_b.js > both.js.

I wanted to use bat instead of highlight in fzf, since it can use cat for previewing files, but this line kills the usage for me unfortunately.

You can do this. You just have to explicitly enable colors and/or decorations. Please study the --help text of bat. Both --color and --decorations can be set to "always".

Also, make sure to use fzfs --ansi option. I have personally set up fzf + bat like this:

export FZF_DEFAULT_OPTS="--ansi --preview-window 'right:60%' --preview 'bat --color=always --style=header,grid --line-range :300 {}'"

Please let me know if this works for you.

All 17 comments

It's so awesome, but whyyyyy

Whenever bat detects a non-interactive terminal, it will fall back to printing the plain file contents.

Because we want bat to be a drop-in replacement for cat. For example, you usually wouldn't want ANSI escape characters (that are used for colorized text in the terminal) to end up in a text file when you call bat file_a.js file_b.js > both.js.

I wanted to use bat instead of highlight in fzf, since it can use cat for previewing files, but this line kills the usage for me unfortunately.

You can do this. You just have to explicitly enable colors and/or decorations. Please study the --help text of bat. Both --color and --decorations can be set to "always".

Also, make sure to use fzfs --ansi option. I have personally set up fzf + bat like this:

export FZF_DEFAULT_OPTS="--ansi --preview-window 'right:60%' --preview 'bat --color=always --style=header,grid --line-range :300 {}'"

Please let me know if this works for you.

Oh, that's so cool! Sure it works. I'll add bat as a supported tool to my fzf plugin for kakoune then. Thank you!

Now we're going somewhere!
image

Awesome :+1:

Shameless advertising of another project of mine: You can also integrate fd with fzf (instead of using find). This is not just faster, it also adds colors to your file paths and hides files that you don't want to open in your editor (.gitignored files, for example).

Instructions are here: https://github.com/sharkdp/fd#using-fd-with-fzf

I use the fzf+fd+bat combination in my shell and in vim, and it's just great ;-)

Yeah, your fd is awesome, and it is already supported by my fzf.kak (shameless advertisement) plugin :smiley_cat:

Fantastic! :heart_eyes:

I do use fd in favor of find, however find can be found on very most systems, and fd not. For example rust packages are not yet supported in Termux, where I use Kakoune with fzf. So default tool is always the most widespread one, but if user wants to change it, it is changeable.

So default tool is always the most widespread one, but if user wants to change it, it is changeable.

Yes, of course!

For example rust packages are not yet supported in Termux, where I use Kakoune with fzf

fd is actually supported (pkg install fd) on Termux. bat has been added a few minutes ago: https://github.com/termux/termux-packages/commit/16de13911b0dde1a303ca84996579d9b6954c986 :smile:

ugh oh, seems that my agents provide outdated info... Anyway, bug thanks for your great tools!

Can we tell bat to highlight a pattern passed as command line argument? Currently, using interactive LESS style search (/), we can go through the matches and they get highlighted one by one. But if we have a command line option to get the searching pattern (e.g. bat --highlight "AB*D"), we can use bat as a search preview tool also.
(To be honest, I want to implement fif here{:target="_blank"} using bat as it is a way better syntax highlighter than highlight).
Should I open a feature-request issue?

@MoezGholami If you're looking for a way to highlight lines that match some pattern, you might be able to borrow some ideas from batgrep?

If you're specifically looking for a way to highlight a line which matches a pattern specified through a command line argument, I would definitely say to open a feature request for it.

@MoezGholami Is this similar to what you have in mind?

LESS='--pattern=pub' bat printer.rs

image

(Thanks to @ahmedelgabri for showing me --pattern.)

@dandavison Thanks man, that's exactly what I want. @eth-p and @ahmedelgabri , thank you too.

for people who are on a quest to make a preview with bat AND tree when it is a directory after painful trial and error here it is:

# this is what matters
FZF_COMMON_OPTIONS="
  --bind='?:toggle-preview'
  --bind='ctrl-u:preview-page-up'
  --bind='ctrl-d:preview-page-down'
  --preview-window 'right:60%:hidden:wrap'
  --preview '([[ -d {} ]] && tree -C {}) || ([[ -f {} ]] && bat --style=full --color=always {}) || echo {}'"

command -v fd > /dev/null && export FZF_DEFAULT_COMMAND='fd --type f --hidden --follow --exclude .git'
command -v bat > /dev/null && command -v tree > /dev/null && export FZF_DEFAULT_OPTS="$FZF_COMMON_OPTIONS"
command -v fd > /dev/null && export FZF_ALT_C_COMMAND='fd --type d --hidden --follow --exclude .git'
command -v fd > /dev/null && export FZF_CTRL_T_COMMAND='fd --type f --type d --hidden --follow --exclude .git'

@dagadbm I think this can be slightly simplified, you don't need the -f or -d checks, that's what I use.

export FZF_PREVIEW_COMMAND="bat --style=numbers,changes --wrap never --color always {} || cat {} || tree -C {}"
export FZF_CTRL_T_OPTS="--min-height 30 --preview-window down:60% --preview-window noborder --preview '($FZF_PREVIEW_COMMAND) 2> /dev/null'"

it works like that for you? i tried that before and he always printed both things.
meh who knows.

It would be great if you could post your findings/scripts/solutions in #448. We are still lacking a README section for a good bat + fzf integration.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jkaan picture jkaan  路  3Comments

tbsvttr picture tbsvttr  路  3Comments

adamtabrams picture adamtabrams  路  3Comments

scalp42 picture scalp42  路  3Comments

niedzielski picture niedzielski  路  3Comments