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:
bat as a --preview program with fzf.bat on the search results of fd/find via fd pattern -X batgit show to view older versions of files: git show v0.6.0:src/main.rs | bat -l rsxclip to copy a file to the consoletail -f some-file.ext | bat -l ext --paging=never with syntax highlighting.git diff --name-only (see #940)When I do fd pattern -x bat, the result looks like a cat result:
Is this expected?
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 :smile:. 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).
Awesome, thanks for the thoughtful response and for your work on bat and fd :)
Added four of the five points listed above. If somebody has more ideas, please let me know.
- [ ] Use
batas a--previewprogram withfzf.
This doesn't work. I guess the reason being bat by default pipes out plain text.
@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.
@Boot-Error It works just fine if you use
--color=alwaysto forcebatto print colorized output. You can also explicitly enable the "decorations" via--decorations=always. Note that you have to pass the--ansioption tofzfin order to properly interpretbats 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-rangeand--highlight-lineoptions in combination withfzf(orsk), 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.
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 Here's a sample of how I use bat as a preview command in fzf.
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.
Thank you for sharing!
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.
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"?
@Boot-Error It works just fine if you use
--color=alwaysto forcebatto print colorized output. You can also explicitly enable the "decorations" via--decorations=always. Note that you have to pass the--ansioption tofzfin order to properly interpretbats 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-rangeand--highlight-lineoptions in combination withfzf(orsk), 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
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 {} ) '"
updated it :) thank you !