18 ms·
Bat: Cat with syntax highlighting
- iefbr14 1y agocat <whatever> | highlight --force -O xterm256 | less -SRNI works fine for me.
- b_e_n_t_o_n 1y agoand rsync works fine for people too, I guess that's why nobody uses Dropbox
- yjftsjthsd-h 1y agoDropbox and rsync do different things. It looks like this does essentially the same thing.
- tclancy 1y agoAssuming perfect memory and you never make typos they are exactly the same. For most installs. Or distros. Probably.
- GodelNumbering 1y agoYou don't need to memorize alias bat='f(){ cat "$1" | highlight --force -O xterm256 | less -SRNI; }; f'
- lynndotpy 1y agoFor all the other issues people have pointed out, this assumes you have `highlight` installed.
- yjftsjthsd-h 1y agoAs opposed to having bat installed?
- yjftsjthsd-h 1y ago> Assuming perfect memory and you never make typos they are exactly the same. You can make an alias. > For most installs. Or distros. Probably. Comparing https://repology.org/project/bat-cat/packages https://repology.org/project/bat-cat/packages vs https://repology.org/project/highlight/packages https://repology.org/project/highlight/packages appears to show them with approximately equal availability. (Unless you're trying some other point, in which case I don't follow.)
- inopinatus 1y agoI don’t know if it still uses librsync but for a long time Dropbox was based directly upon it. Whether this proves or disproves anything in re syntax highlighting utilities I leave as an exercise.
- b_e_n_t_o_n 1y agoIt's a reference to this: https://news.ycombinator.com/item?id=8863 https://news.ycombinator.com/item?id=8863
- yjftsjthsd-h 1y agoI know. But that doesn't make it accurate. And in broader strokes, I'm not fond of that thread/comment being used to pretend that all new and shiny things are better than what came before. Sometimes the new thing is barely more than a coat of paint on the existing thing.
- deleted 1y ago[deleted]
- spookie 1y agoI call cat when I need to pipe or copy something, and just bat if I wanna read it myself. I find this as a good compromise, I like how bat formats things. Your solution would be ok with an alias as well, so thanks. Might try it just so I dont need yet another program lying around
- seedie 1y agoYou can still pipe bat output as it falls back to unformated output when it detects a non-interactive terminal. https://github.com/sharkdp/bat?tab=readme-ov-file#file-concatenation https://github.com/sharkdp/bat?tab=readme-ov-file#file-conca...
- postepowanieadm 1y agoBut it's not in rust.
- MangoToupe 1y agoI don't seem to have "highlight" on my system.
- JdeBP 1y agoThat's because it is in ports rather than in base. (-: * https://freshports.org/textproc/highlight/ https://freshports.org/textproc/highlight/ * https://ftp.netbsd.org/pub/pkgsrc/current/pkgsrc/textproc/highlight/index.html https://ftp.netbsd.org/pub/pkgsrc/current/pkgsrc/textproc/hi... * http://ports.su/textproc/highlight http://ports.su/textproc/highlight * https://ports.macports.org/port/highlight/ https://ports.macports.org/port/highlight/
- MangoToupe 1y agoWhy not just install `bat` at that point?
- freddie_mercury 1y agoThat has git integration, like bat does? That shows non-printable characters like bat does? That allows you to concatenate and page multiple files at once like bat does? That supports the --line-range option like bat does? You can pipe the output of tail -f through your alias? I guess if all you did was read the headline of the post you could assume your alias does all the same things as bat.
- feelamee 1y agolooks like you are right, but... I almost never used all of this features with bat. Maybe tail -f sometimes. Do you really need this in daily workflow?
- freddie_mercury 1y agoI use the syntax highlighting of manpages, fd, ripgrep, and git pretty regularly. I also use the fzf previewer with --range-limited pretty frequently.
- petepete 1y agoIt's clearly a joke in the vein of being able to recreate Dropbox with an FTP account, curlftpfs, and SVN/CVS.
- hnlmorg 1y agoNot the GP and I do like bat, but to answer your questions: > That shows non-printable characters like bat does? cat does actually support that via the flags with -v (you can also use -t and -e to view tab and line endings too) > That allows you to concatenate and page multiple files at once like bat does? cat is literally called “cat” because it’s intended purpose is concatenation. It’s not a pager though the GPs example did pipe to less anyway. > That supports the --line-range option like bat does? ‘tail’ and ‘head’ would be muscle memory to a lot of people and not that different in terms of number of keystrokes. But I do take your point that it’s nice to have that built into your pager. > You can pipe the output of tail -f through your alias? I couldn’t see why not. tail -f isn’t doing anything weird with the fd. ——— I’m not arguing against using bat though. I have it aliased to cat on my own machines, so I clearly and would recommend bat. But I do think some people might be surprised how far you can get with coreutils if bat wasn’t available
- MisterTea 1y agoYou don't need cat: highlight --force -O xterm256 < whatever | less -SRNI
- dicytea 1y agoSadly does not work on fish because the developers does not believe that users are intelligent enough to understand the obvious and intuitive outcome of flipping ">" (a valid operator in fish).
- em-bee 1y agoi tried that: cat .bashrc | highlight --force -O xterm256 | less -SRNI everything is green, except line numbers are black (which comes from less), but bat .bashrc shows actual syntax highlighting. so apparently, no, it doesn't work. to be fair, this is how it works: cat .bashrc | highlight --syntax shellscript -O ansi | less -R to avoid getting caught by the useless use of cat police, this does too: highlight --syntax shellscript -O ansi .bashrc | less -R however, i have to tell it which syntax to use but to its credit, highlight even has support for pike, which bat doesn't (yet) (fixed that for myself, at least) so overall, bat wins.
- em-bee 1y agooh, i feel stupid now, the reason bat worked and highlight didn't was because bat had access to the filename extension. if highlight is called with a filename argument it can detect the syntax too. so really bat and hightlight are equal, and it's not just a useless use of cat, but using cat here actually breaks the syntax detection. and it does so in bat too, obviously. so this means highlight almost wins because it has pike support already, whereas for bat i had to add it, except that it turns out that if highlight can't detect the syntax it produces nothing, and you need --force to fix that, and if it is given multiple files as arguments it writes the output to files too, which is practically never what i want so i need to fix that with --stdout. bat it is.
- ikurei 1y agoAlthough I agree with other commenters that your command can't compare to all of bat's features, many of which I appreciate... thank you for sharing this tip, I didn't know about `highlight` and I can't install `bat` at work. This will live in my .bashrc for a long time: cat() { if [[ -t 1 ]]; then command cat "$@" | highlight --force -O xterm256 else # plain cat to pipe into other things command cat "$@" fi }
- dTal 1y agoThis... doesn't work? Everything just comes out green. It's not clear to me how 'highlight' could even possibly know what syntax it's supposed to be highlighting when processing stdin, unless it ingests the whole thing until EOF and then applies some kind of fuzzy logic. If you feed it a filename as an argument, it just checks the extension.
- deleted 1y ago[deleted]
- em-bee 1y agocurious, how did you have highlight installed? i'd change the third line so you can actually get syntax highlighting: command highlight --stdout --force -O xterm256 "$@"
- b_e_n_t_o_n 1y agoThis is super nice!
- perihelions 1y agoOther threads about (IMHO very nice) tools by this developer (fd, hexyl, and hyperfine), https://news.ycombinator.com/from?site=github.com/sharkdp https://news.ycombinator.com/from?site=github.com/sharkdp
- JamesCoyne 1y agoAlso "numbat". A favorite of mine
- seemaze 1y agoshardkp makes my terminal a home. Thanks sharkdp!
- Crespyl 1y agoI've been using bat for several years and it's been a really nice drop-in replacement for cat/less/etc. Like rg, it's one of those "rewrite it in rust" projects that turned out to actually be quite well thought through.
- oncallthrow 1y ago"rewrite it in rust" gets a lot of hate, but generally the people rewriting things in Rust actually give a shit, which tends to pay dividends. I say this as someone who doesn't use Rust and doesn't particularly like it or its community.
- ajkjk 1y agodoes... it... get a lot of hate? I've only ever heard of it positively.
- DonaldPShimoda 1y agoIf you hang around certain spaces of the internet (here, Reddit, etc), any sufficiently popular post about a Rust-related project is bound to accrue some comments along the lines of "Rust is actually not that great", "rewriting is bound to gain new bugs that won't be caught", "the borrow checker is practically byzantine and not worth the trouble", "C is perfectly adequate", "more advanced type systems are less useful than writing more tests", etc. I would even say that comments of this nature constituted a good chunk of the discussion around the US government's C-to-Rust initiative (a very popular post with a lot of comments).
- lenkite 1y agoThe Rust Blowback had some reasons: People arbitrarily opening bug-tickets on various projects saying they should be Rewritten in Rust for Safety. This became a Meme.
- burntsushi 1y ago
- layer8 1y agoAs a more general-purpose tool, there is also GNU Source-highlight [0] which comes in most distributions. For example to use with less: https://www.gnu.org/software/src-highlite/source-highlight.html#Using-source_002dhighlight-with-less https://www.gnu.org/software/src-highlite/source-highlight.h... [0] https://www.gnu.org/software/src-highlite/ https://www.gnu.org/software/src-highlite/
- binaryturtle 1y agoIn Safari I get "Too Many Requests" error; in Firefox ESR I get a straight "403 Forbidden". What the heck is going on with gnu.org? Edit, with cURL it's OK… 200. That makes no sense.
- hayleox 1y agoNeat! One of those tools that I would never have thought to look for, but I'm sure I'll be glad I have.
- arjie 1y agoVery cool tool. It's perfect for the "take a quick look" use-case. I just downloaded it on MacOS and when I ran it the first time it took a really long time on a one-line JSON which disappointed me but then any subsequent run on anything was fast. I'd completely forgotten about MacOS doing that thing on first run. Uses the pager for large files automatically etc. Very nice.
- steezeburger 1y agoWhat thing does MacOS do on first run?
- cobbal 1y agoIt phones home to apple malware servers, among other things: https://lapcatsoftware.com/articles/2024/2/3.html https://lapcatsoftware.com/articles/2024/2/3.html
- soraminazuki 1y ago> Uses the pager for large files automatically etc. Very nice. If you like that, you can also make less behave like cat if the file fits in one screen. You just need to add F to the LESS environment variable. It's very convenient because so many things depend on less. https://www.man7.org/linux/man-pages/man1/less.1.html#:~:text=quit-if-one-screen https://www.man7.org/linux/man-pages/man1/less.1.html#:~:tex...
- dang 1y agoRelated. Others? bat: A cat(1) Clone with Wings - https://news.ycombinator.com/item?id=33382307 https://news.ycombinator.com/item?id=33382307 - Oct 2022 (2 comments) bat, a cat(1) clone with syntax highlighting, Git integration written in Rust - https://news.ycombinator.com/item?id=24850244 https://news.ycombinator.com/item?id=24850244 - Oct 2020 (6 comments) Bat: A cat(1) clone with wings - https://news.ycombinator.com/item?id=17887819 https://news.ycombinator.com/item?id=17887819 - Aug 2018 (12 comments) Bat: A cat(1) clone with wings - https://news.ycombinator.com/item?id=17849535 https://news.ycombinator.com/item?id=17849535 - Aug 2018 (1 comment) Bat: cat(1) clone with syntax highlighting and Git integration - https://news.ycombinator.com/item?id=16968755 https://news.ycombinator.com/item?id=16968755 - May 2018 (1 comment)