Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fzf] Bat is awesome, but #357

Closed
andreyorst opened this issue Oct 14, 2018 · 17 comments
Closed

[fzf] Bat is awesome, but #357

andreyorst opened this issue Oct 14, 2018 · 17 comments
Labels
question Further information is requested

Comments

@andreyorst
Copy link

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?

@sharkdp
Copy link
Owner

sharkdp commented Oct 14, 2018

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.

@sharkdp sharkdp added the question Further information is requested label Oct 14, 2018
@andreyorst
Copy link
Author

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!

@andreyorst
Copy link
Author

Now we're going somewhere!
image

@sharkdp
Copy link
Owner

sharkdp commented Oct 14, 2018

Awesome 👍

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 ;-)

@andreyorst
Copy link
Author

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

@sharkdp
Copy link
Owner

sharkdp commented Oct 14, 2018

Fantastic! 😍

@andreyorst
Copy link
Author

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.

@sharkdp
Copy link
Owner

sharkdp commented Oct 14, 2018

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: termux/termux-packages@16de139 😄

@andreyorst
Copy link
Author

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

@MoezGholami
Copy link

MoezGholami commented Nov 16, 2019

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?

@eth-p
Copy link
Collaborator

eth-p commented Nov 16, 2019

@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.

@dandavison
Copy link
Contributor

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

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

image

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

@MoezGholami
Copy link

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

@dagadbm
Copy link

dagadbm commented Nov 20, 2019

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'

@ahmedelgabri
Copy link
Contributor

@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'"

@dagadbm
Copy link

dagadbm commented Nov 21, 2019

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

@sharkdp
Copy link
Owner

sharkdp commented Nov 24, 2019

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.

d1egoaz added a commit to d1egoaz/dotfiles that referenced this issue Jun 21, 2020
Albatrosicks pushed a commit to Albatrosicks/dotfiles that referenced this issue Dec 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

7 participants