 Today, I want to talk a little bit about the bat command. Bat is essentially a rewrite. It's a straight clone of cat with some extra functionality. Bat is essentially cat rewritten in the Rust programming language and it includes some extra features that most people kind of like such as line numbers and syntax highlighting because typically when you cat the contents of a file to standard output, you don't get line numbers, you don't get syntax highlighting. So think of bat as being really an improved version of cat. Now in recent years, bat has become a very popular replacement for cat. Most of you guys have probably heard of bat, but I don't think most of you have probably heard of this extras package, bat dash extras. And what bat extras are, there's some extra scripts that integrate bat into various other command line utilities. And you can see you've got essentially six extra commands that you can now use once you've installed bat dash extras. And those include bat grip, bat man, bat pipe, bat watch, bat diff and pretty bat. To install bat, you could get clone this particular repository. And after you get clone the bat extras repository, there's a build shell script that you could run. For many Linux distributions, you might find it actually in the repositories. I know on Arch Linux, you can find bat dash extras in the AUR, the Arch user repository. As far as dependencies, you really don't have much dependencies, even though all of these scripts do list some dependencies, almost all of them are probably already installed on your system. The one exception is rip grip. You may have to install rip grip if it is not already installed on your system because that is a hard requirement for bat grip. So let me show you bat and some of these bat extras in action just very briefly. Let me open a terminal here. I'm going to zoom way in. I'm going to clear the screen. So let's start with just catting my dot bash RC. So that's cat, right? It just prints the contents out the standard output. There's no line numbers. There's no syntax highlighting. And you know, that works. There's nothing wrong with it. But let me just show you bat on the dot bash RC. Now this is much nicer. For one thing, we get the file name here at the top. That's a nice addition. And then we get the line numbers and we get some columns here. So the line numbers are separated and makes it very easy to read. And you get syntax highlighting in the appropriate language. In this case, it knows it's a bash script. So it's giving us the bash syntax highlighting. You can actually specify the language of a file as far as, you know, if for whatever reason bat does not interpret the language correctly, you can specify name of language. You can actually do bat. I guess dash language. Yeah. For example, Lua, I'm going to do dash, dash language space Lua. In this case, dot bash RC. Now the dot bash RC, of course, is not written in Lua. So the syntax highlighting is all crazy, right? Because it doesn't recognize these comments and bash because Lua does not use the pound sign for comments. It actually uses two dashes, I believe. So that is the dash, dash language flag. If I switched over to Python, actually, Python doesn't look bad in the shell script. Yeah, not too bad on that. So that is just the standard use of bat. Now let's talk about some of the bat extras such as bat grip. So if I wanted to bat grip and I'm going to do my first name because I know my first name exists on one of the lines in my dot bash RC and you can see bat grip, it doesn't quite work exactly like grip. Remember it's using rip grip under the hood, but you can see we don't get just the line return that contains the string Derrick because if I up arrow and I just do grip Derrick on the dot bash RC, you know, the way grip works, it just spins out the line that contains that string. We really don't want the rest of it and certainly when you're scripting, you know, using tools like grip. That's what you want. But here, interactively, if I'm at the shell and I just want to see the line that contains Derrick, sometimes it's nice to have some context. So it gives me a couple of lines before and a couple of lines after the string that it found. Now you can adjust the lines before and the lines after that it displays. So for example, if I do dash B for before zero and dash capital A for after and also do zero, I'm saying the lines before zero. I don't want to see any lines before the string. I also don't want to see any lines after the string and now it just returns the actual single line that contains the string Derrick if I up arrow and I do 10 lines before and 10 lines after. You can see now I get a much bigger section of this fall returned. So that's really just the basics of bat grip. There's not much to it. If you want to actually see more about bat grip, you can do a man on bat grip and it does have a man page. Let me zoom out because it's got a table here that's going to look all wonky with me zoomed in, which you can see. It gives you some of the flags that are available using bat grip. Now it doesn't have nearly as many flags as your standard grip command like from the GNU Core Utils. Remember bat grip is really using rip grip and most of the rip grip flags are here but some don't quite work. You've got some notes. These are the flags that that work and these are the flags that may work or they may not. I'll let you read that man page if you're interested more in bat grip. I will say I think bat grip of the six bat extras bat grip is probably the one I would use the most. Really of these six scripts that are packaged in bat extras really I think only two of them are something that you would use on a regular basis and that is bat grip and the next one bat man. So let's go ahead and check out bat man. So if I do bat man and let's read the man page on man. So bat man on man. So I get the man man page except it displays it using bat. So we get some syntax highlighting because it recognizes that this is written in trough which is the language that man pages are written in. It's kind of a like a markdown kind of language. Earlier we ran man on bat grip. Well I could do bat man on bat grip and you know I get the the bat grip man page again. Now you will notice that when I do bat man on man and then I up arrow and in my case if I do man on man my standard man command was already using bat as the man pager. The man pager being the program that displays your man pages that's because and all of my shell configs let's go ahead I'll vim into my bash rc and you can see one of the top lines here export man pager equals and this command here which involves bat. So I was already forcing the use of man to always invoke bat as the man pager because by default let me quit out of this the man pager on most Linux systems is going to be the less command that's l-e-s-s I could actually type that correctly. So let me show you this in action. Let me actually switch over to the bash because I'm in fish right now and let me go ahead and export man pager to equal less which is typically what the man pager defaults to on your Linux system. So now when I do a man on man this is just your standard less command right just reading it with less and now let me go ahead and source my bash rc which has export the man pager equals the bat command right now when I do a man on man it actually is going to use bat so I actually don't need bat man I was already kind of using bat anyway as my man pager so but if you didn't want to export this man pager variable having bat-extras installed on your system eliminates the need for all that because you could always just use batman for your man pages. As a matter of fact you could just alias man to equal batman and problem solved and if I go back to their github batgrip batman those are the two most useful ones the only other two I might use on a somewhat occasional basis would be bat watch which watches four changes in a file so if you're already batting a file you know catting a file essentially and it's the being displayed in the terminal if that file changes for any reason the changes will automatically update in the terminal so that's kind of nice bat diff of course is just about comparing two files you know running a diff on two files it's up now with bat diff you'll have the niceties of bat you know with the line numbers and the syntax highlighting and all of that so let me show you bat watch first and then I'll show you bat diff so let's clear the screen so let me open up a second terminal and I'm going to zoom in here now let's go ahead and do a bat watch on the dot bash rc and I'm just going to focus on these last few lines here because my bat bash rc is such a lengthy file but line 294 here where it's basically setting the starship prompt uh take a look at that and what I'm going to do is I'm going to go ahead and open my dot bash rc inside vim here and let me go ahead and go to the last couple of lines here where I'm setting the starship prompt matter of fact why don't I just change the come in here and I'm going to change this I'll change that to the rocket ship prompt and let me write that now that I've written that if I go back over to this terminal where we were batting that out right you can see the bat watch command updated that for us how cool is that if I you to undo over here and write that and you know bat watch is still going on over here in just a couple of seconds it should update for us and it does so that is essentially bat watch not much to it but a neat command I probably wouldn't use that too often but it could be useful under certain circumstances so let's talk about bat diff so bat diff works just like the standard diff command it'll take two files file one and file two and it will give you the differences the lines that are different between the two files so let me show you this in action I have a test directory here on my system by ls I have file one let's actually see what is in file one let's bat it and you can see it's just a sequence of numbers one through ten well why don't I bat file one and then I'm gonna redirect that into a new file file two basically it's just a copy is all I did there but you know that's the way you could catafile into another file you can also bat a file into a new file as well now let me go ahead I'm gonna vim into file two if we're gonna do a diff we need some different lines so this line I'm gonna change from five to 55 and I'll do the line below it as well this will be 666 because why not right and now let's do bat diff on file one and file two and now it's a standard diff right except we do get really nice line numberings that are divided by these columns so that makes it really easy to read and we also get syntax highlighting as well for the diffs and you can see we have the three minus symbols in front of file one three plus symbols in front of file two basically telling you what you can do to file one to make it exactly like file two and to make file one look exactly like file two well you have to subtract these two lines right that's why they've got the minus and their colored red and you would have to then add these two lines which are colored in green and have the plus symbols so bat diff again very much like the standard diff command it's just it looks a little prettier and going back to the github page the two commands that I think are the least useful will be the bat pipe command and the pretty bat command at least for me you guys might find them a little more useful than than what i'll find them useful pretty pipe basically add some extra functionality where the bat command can interpret what kind of file or if you're using it on a directory what your what it should do basically so let me cd back into my home directory and i'm going to run bat pipe so bat pipe is it's a just a standard command it's the bat command but using bat pipe depending on what you run it on it'll do different things i'm going to run it on dot config which is a directory and when you run it on a directory bat pipe knows it's a directory and will automatically default to using the exa command which is a ls replacement also written in rust or if you didn't have exa installed it would just default to just doing an ls on that directory so that is bat pipe yeah you can run it on anything if i ran it on i don't know my yad.com i see that file i don't know what it would do it doesn't do anything i do know if you ran it on a zip file it would run g unzip or if you ran it on a tar file a tar ball a tar dot gz or tar dot xz it would run a tar command for you so that's that's kind of what bat pipe does and then finally you have pretty bat and i i just run pretty bat on my bash rc it just looks like bat on the bash rc because really there's not much to it what it does is does a pretty print it pretty prints the source code and highlights it with bat and it says see the documents for pretty bat actually let's go to the docs because it will probably explain what it does better than me because i can't really show you what it does but it basically it does some syntax highlighting and code formatting is what it does and it uses these particular code formatting tools depending on the language of the file that you happen to be running pretty bat on and it gives you you know that that code formatting essentially so that is pretty bad but you know you've got six tools here as part of the bat extras package two of them i think are very useful and because these two are so useful batman and bat grip i think it's worth having installed you know bat pipe bat watch bat diff pretty bat i probably would never use them but again i think bat grip and batman are so useful i'm really happy that i've got it installed i'm gonna start installing it on all of my machines going forward because i i think it's a pretty neat little project now before i go i need to thank a few special people i need to thank the producers over the show gave james maxim my homie's too bald matt mimit michael paul roald west armor dragon bash potato chuck commander angry george lee marchdom ethos nate ur yon paul peace archon vador polytech realities for less red profit roland tools devler william zenibot these guys they're my highest tier patrons over on patreon without these guys this episode about bat and bat extras would not have been possible the show is also brought to you by each and every one of these fine ladies and gentlemen all these names you're seeing on the screen right now these are all my supporters over on patreon i don't have any corporate sponsors i'm sponsored by you guys the community if you like my work want to see more videos about linux and free and open source software subscribe to distro tube over on patreon peace guys