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

Section about "Integration with other tools" (fzf, fd, find, git, ..) #448

Closed
6 tasks done
sharkdp opened this issue Dec 14, 2018 · 16 comments
Closed
6 tasks done

Section about "Integration with other tools" (fzf, fd, find, git, ..) #448

sharkdp opened this issue Dec 14, 2018 · 16 comments
Labels
documentation help wanted Extra attention is needed
Milestone

Comments

@sharkdp
Copy link
Owner

sharkdp commented Dec 14, 2018

We should add a "Integration with other tools" section to the README to document some of the ways in which bat can be combined with other tools:

  • Use bat as a --preview program with fzf.
  • Execute bat on the search results of fd/find via fd pattern -X bat
  • Combine it with git show to view older versions of files: git show v0.6.0:src/main.rs | bat -l rs
  • Use it with xclip to copy a file to the console
  • Continuously monitor a growing file with tail -f some-file.ext | bat -l ext --paging=never with syntax highlighting.
  • Combine with git diff --name-only (see bat --diff #940)
  • etc.
@erusev
Copy link

erusev commented Dec 16, 2018

When I do fd pattern -x bat, the result looks like a cat result:

  • no highlighting
  • no line numbers
  • etc.

Is this expected?

@sharkdp
Copy link
Owner Author

sharkdp commented Dec 16, 2018

That is expected, yes. You need to use -X/--exec-batch instead of -x/--exec. This option in fd is new and I haven't even released a new version that includes this feature 😄. Sorry for the confusion.

The difference between the two is that -x will basically execute bat file1, bat file2, .. bat fileN whereas -X executes bat file1 file2 ... fileN. The former (-x) does that by executing many jobs in parallel in the background without being directly connected to the terminal (otherwise, you would just get an interleaved output mess). This is why bat does not detect an interactive terminal and will switch off colorization etc. by default. The latter -X executes just a single job at once and can therefore directly pipe the output to the terminal.

To use bat with the -x option, you would have to force a colorized output (fd pattern -x bat --color=always).

@erusev
Copy link

erusev commented Dec 16, 2018

Awesome, thanks for the thoughtful response and for your work on bat and fd :)

@sharkdp
Copy link
Owner Author

sharkdp commented Feb 11, 2019

Added four of the five points listed above. If somebody has more ideas, please let me know.

@sharkdp sharkdp added the help wanted Extra attention is needed label Feb 11, 2019
@Boot-Error
Copy link

  • Use bat as a --preview program with fzf.

This doesn't work. I guess the reason being bat by default pipes out plain text.

@sharkdp
Copy link
Owner Author

sharkdp commented Feb 25, 2019

@Boot-Error It works just fine if you use --color=always to force bat to print colorized output. You can also explicitly enable the "decorations" via --decorations=always. Note that you have to pass the --ansi option to fzf in order to properly interpret bats output.

Here is what I use:

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

There are also a few cool things that we can do with bats --line-range and --highlight-line options in combination with fzf (or sk), like showing a syntax-highlighted preview of ripgreps search results while interactively modifying the search pattern.

@ReggieMarr
Copy link

@Boot-Error It works just fine if you use --color=always to force bat to print colorized output. You can also explicitly enable the "decorations" via --decorations=always. Note that you have to pass the --ansi option to fzf in order to properly interpret bats output.

Here is what I use:

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

There are also a few cool things that we can do with bats --line-range and --highlight-line options in combination with fzf (or sk), like showing a syntax-highlighted preview of ripgreps search results while interactively modifying the search pattern.

This is something that I use all the time! I am looking at finding a way to interactively highlighting (changing the background colour) of several lines around the line matching the fuzzy searched for string. However since I am just piping ripgrep, skim and bat into each other and they each rust projects shouldn't I be able to do this within a rust project by using each crate?

ripgrep and skim seem to be straightforward enough on their own but I havent been able to figure out how to call bat from a rust project.

@sharkdp
Copy link
Owner Author

sharkdp commented Apr 4, 2019

However since I am just piping ripgrep, skim and bat into each other and they each rust projects shouldn't I be able to do this within a rust project by using each crate?

ripgrep and skim seem to be straightforward enough on their own but I havent been able to figure out how to call bat from a rust project.

See #423

However, I think that most of this can also be done with a little bit of (shell) scripting. The tools play quite nicely together, I think. If there is anything you miss (from the CLI), let us know.

@sharkdp sharkdp changed the title Section about "Integration with other tools" Section about "Integration with other tools" (fzf, fd, find, git, ..) May 15, 2019
@issmirnov
Copy link
Contributor

@sharkdp Here's a sample of how I use bat as a preview command in fzf.

https://github.com/issmirnov/dotfiles/blob/df92f79a760740a7d389605f2f0f5085ca95a713/zsh/config/fzf.zsh#L149-L161

This is a small function that searches over source code with ag, then pipes it into bat with --highlight-line and then opens the file in vim.

@sharkdp
Copy link
Owner Author

sharkdp commented Jun 7, 2019

Thank you for sharing!

@eth-p
Copy link
Collaborator

eth-p commented Jun 19, 2019

At the request of @bricewge, here's my script for searching using ripgrep with bat for output printing:

https://github.com/eth-p/bat-extras/blob/master/src/batgrep.sh

I mainly use it on MacOS, but it should also work on Linux as well.

@dagadbm
Copy link

dagadbm commented Nov 25, 2019

seems like a LOT of stuff has been commented out here.
what is exactly missing from the documentation?
i was told to go to this issue the thing i am using right now is just this:

export FZF_DEFAULT_OPS="
  --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 {})'"

I basically support using tree and bat to preview files and directories using fzf. together with some helpful bindings to toggle preview and paging up and down the preview window.

what should be the "oficial way"?

@dagadbm
Copy link

dagadbm commented Nov 25, 2019

@Boot-Error It works just fine if you use --color=always to force bat to print colorized output. You can also explicitly enable the "decorations" via --decorations=always. Note that you have to pass the --ansi option to fzf in order to properly interpret bats output.

Here is what I use:

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

There are also a few cool things that we can do with bats --line-range and --highlight-line options in combination with fzf (or sk), like showing a syntax-highlighted preview of ripgreps search results while interactively modifying the search pattern.

also this is not needed.
You do not need the --ansi and you shouldn't be using that as well as it slows up fzf quite a bit.

The --ansi is needed only if you are using fd with colors to colorize the input.
For the preview it works without the ansi (its how I am currently using it).
The only "down side" is you dont get coloring on the fuzzy list which i don't think is a big deal honestly.. I just want coloring when previewing stuff

@paraduxos
Copy link

export FZF_DEFAULT_OPS="
  --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 {}'"

Thank you, very helpful.

You missed the last )

--preview '([[ -d {} ]] && tree -C {}) || ([[ -f {} ]] && bat --style=full --color=always {}) || echo {} ) '"

@dagadbm
Copy link

dagadbm commented Apr 28, 2020

updated it :) thank you !

JesseVermeulen123 added a commit to JesseVermeulen123/bat that referenced this issue Oct 27, 2020
sharkdp pushed a commit that referenced this issue Oct 29, 2020
doubleYouTicky added a commit to doubleYouTicky/fzf.fish that referenced this issue Dec 13, 2020
It will solve "unknown file types in __fzf_preview_file".

It wasn't working because the current directory (see pwd output in __fzf_preview_file function) of the preview context is $HOME (I don't know why ... I just spent 3 hours debugging it).

To understand why I removed the colors: sharkdp/bat#448 (comment)

Thank you for your work.
@sharkdp sharkdp added this to the v0.18.0 milestone Jan 9, 2021
sharkdp added a commit that referenced this issue Feb 27, 2021
@sharkdp
Copy link
Owner Author

sharkdp commented Feb 27, 2021

I added a section about fzf + bat integration to the README.

@sharkdp sharkdp closed this as completed Feb 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

8 participants