 I believe you can answer your own data analysis questions. Do you? You should. Stick around for another episode of Code Club. I'm your host, Pat Schloss. And it's my goal to help you grow in confidence to ask and answer questions about the world around us using data. In previous episodes of Code Club, we haven't spent much time talking about how to customize your bash environment. Perhaps you've already found how you can change the background and font colors using the themes that are built into the software. If you're going to spend any amount of time looking at the screen, it's nice to customize it so it's nice to look at. Besides these aesthetic customizations, we can also customize the behavior of our command line environment to make it easier to interact with. In today's episode, we'll see how we can modify the hidden BashRC file that lives in our home directory. First, we'll customize our command line environment by creating aliases that are words that stand in for other things. For example, if there are arguments that you are always using when you run a command, you can put that into an alias. For example, by default, R will prompt you to save your session and offer to load data from a previous session unless you give it commands or arguments when you start R. Both of these practices are problematic from a reproducibility standpoint because you may lose track of what variables were created previously. But we can create an alias that always tells R to turn off those features. Second, we'll see how we can customize our prompt. I'll share with you the code used to generate a prompt that will tell us the status of our project's repository. In the exercises, I'll encourage you to make your own aliases and prompts. An important point to remember in customizing our environment is that we don't want those customizations to impact our actual analysis. This is because if you get my code and my code depends on the customization, but I don't give you those customizations, then your version of the analysis will break. So for example, imagine if I were to create an alias for sed that's called sed but runs sed-e. I might code my bash scripts in a way that assumes that I have access to the enhanced regular expressions. Now, you get a hold of my code and try to run those scripts, but it doesn't work for you. Why? Well, because you don't have the same sed that I have. I have an alias that's running something else. We always want to be mindful of how much our bash scripts depend on our aliases. Today's video won't do anything with the data from the project we've been working on over recent episodes. So even if you're only watching this video to learn more about customizing your command line environment and don't even know what a 16S RNA gene sequence is or what a bacterium is, you should get a lot out of today's video. Please take the time to follow along on your own computer and attempt the exercises. Don't worry if you're not sure how to solve them. At the end of the video, I'll provide solutions. If you haven't been following the previous episodes, welcome. Please be sure to subscribe to the channel and click the bell icon so you know when the next episode is released. Feel free to leave a comment, even if it's just to say hi. Please check out the blog post that accompanies this video where you'll find instructions on catching up, reference notes, and links to supplemental material. The link to the blog post for today's video is below in the notes. There are places on my computer that I seem to go to a lot. I'll CD to, say, my lab website directory, or I'll CD to different manuscript directories that I'm working on. Recently, I've been cd-ing into the project directory for our R&N analysis, looking at Amplicon sequence variants. For those other examples, I've actually created what's called an alias. So they don't have to write out the full path or even remember what the full path is. I can remember this alias key and type that in, and it'll then take me to that directory. So how do we do that? Well, the first thing that's important to note is that if I type ls in my home directory, I see a lot of things that look fairly familiar. If I open up my finder window for my home directory, I see those same files and directories. Now, if I do ls-a, the a is short for all. This will show me everything that's in my home directory. And wouldn't you know it? There's a lot of other stuff that I don't see by default. And the things that are getting hidden are things that start with a period. And so we call these dot files. There's on a Mac, there are two important dot files. One is bash history and one is bash RC. Now, if you're using Windows 10 with their bash interface or using Linux's bash interface, you'll only have a bash RC file. Although not all of you are using Macs, I'll go ahead and open up Adam in this working directory. And if I show you my bash profile file, you'll see that the first line is here. And I've copied this into the show notes or into the episode notes. And you can copy and paste this into your bash profile file if you're on a Mac. Again, if you're on Windows, if you're on Linux, don't worry about it. So what happens when you start up a terminal in, is on a Mac at least, is the first thing it runs is dot bash profile. Runs this as a script. If you're on Linux, on Windows, it'll run bash RC. What that means is that anything that's in these files will be run and be loaded into kind of the current working environment. You'll see here that I have information about paths. This tells bash where to look for different commands on my computer. And there's some other goodies in there as well. The bash RC is another place to store goodies. Again, it gets loaded. At least the way I've got it written at the very beginning of my bash profile file. And again, whenever, if you're on Windows or Linux, it starts up a terminal. And what you'll see here is that I've got a variety of aliases that help me get to a directory that I'm interested in going to. So for example, I have an alias for my lab's website. So if I have the prompt, I type lab site. This takes me to the directory for my lab's website. Another built-in alias is the tilde, sorry, CD tilde, that takes you back to your home, okay? So what I'd like to do is create an alias for this project. And I'll say alias and RRN. So all I have to type at the prompt then is RRN. And it'll run this command. And let me double-check that I get the right working directory. So I'll do a CD documents, floss that, and I'll do PWD, and I'm gonna copy this path. And again, if you've got your project stored somewhere else, you're gonna have a different path that you're gonna put in here. But the idea is the same, that we're gonna CD into this directory. Now, if down the road, I change the name of, or decide where I wanna submit this to, and I change XXXX to something else, my alias isn't gonna work. So I'll have to come back in and edit my alias to make sure it works. Now, I save that, and if I now type RRN, it says command not found. And that's because I need to do what's called source the file. I need to source the file or rerun that script. Alternatively, as I said, these files all get run when you restart a new instance of your terminal. So I could quit my terminal and start again, or I could type source tilde bash profile. And again, if you're on Linux or Windows, you would type bash RC here. And the tilde says this is the home directory, and in the home directory, do this. I'm not currently in my home directory. Maybe I should be, doesn't really matter. You'll see that that reruns everything that was at the very top of my bash window when I started it. I'll explain what this output is in a minute. But if I, again, go CD tilde to go to my home directory, and now I type RRN, boom, it takes me right there. I don't have to worry about CD anymore. I don't have to remember the path anymore. I don't have to worry about typos. It's just done for me. It's beautiful. It's wonderful. I really like having these aliases. The only trouble becomes remembering all the aliases or thinking, did I create an alias for that? And sure enough, I did. Again, if I look at this bash RC file, where that weather report is coming from is a little command, a little pipe that I've put in here, which goes to the WTTR.IN website or server, and it pulls down weather that's related to the University of Michigan, near the University of Michigan. And then I say return the top seven rows of that. And it then outputs it, as you can see here, as the weather and tells me that it's partly cloudy. It's about 75 to 77 degrees. There's no wind, lots of visibility, and no precipitation. I could also look at my window and see the same thing, but when you're in the winter doldrums of Michigan and you don't know how cold it is out, it's nice to have this. Anyway, so again, you can put all sorts of goodies in there. The important thing to remember is that anything that's in there is gonna be run when a new session of your terminal starts. Okay, I had told you in the introduction that R has this kind of peculiar problem with it, where I start R and say I do some math, that's good, I figured out what two plus two plus two equals, and now I wanna quit, right? And then it asks me if I wanna save my workspace image. You do not wanna save your workspace image. And again, this goes back to that problem of aliases, that if you save your workspace image, then it's going to, by default, reload it the next time around. And you might not realize that things are being held over from previous sessions to the current session, and as you're developing your analysis, you may rely on those things from previous sessions that have never been actually created in your current session. So again, you then take your code, you give it to someone else. They don't have that history of those previous sessions, and so they're kinda out of luck. It's not gonna work for them. And so the best, the safest way to do it is to tell R, no, I don't wanna save my workspace image, and also I don't want you to load any workspace images if they happen to be around or if for some reason they were created. So if I do R dash dash help, and I scroll up in here, I can see the various arguments that can be given with R. I should step back a second and say, you might be asking, Pat, why are you running R from the command line? That's, you're like an animal, who does that? Everyone uses RStudio. That's true. And if you look at the options, the preferences in RStudio, they gave you the same options, and I would encourage you there also to turn off the history and to not load previous sessions. How do we do that here? And of course, there's certain reasons that we will want to be able to run R from the command line that goes back to things like automation. We can't really automate an analysis using bash mother R from RStudio. It just doesn't work. So we've got to be able to run R from the command line. So if I look at the various options here, so it says you can save, do save the workspace at the end of the session, don't save it. So we don't want to save it, and we don't want to restore. So we want to do no restore. So no save, no restore. I can then do R hyphen hyphen no save, hyphen hyphen no restore. I think I typed in the wrong thing, restore. And again, if I do plus two, plus two, plus two, and then I do quit, it no longer asks me what I want to do, okay? So it would get pretty tedious to have to write out those arguments every time we want to load R. What would be better is to create an alias for R. Now, if we come over here and create a new alias, I'll do alias R equals that. So I'm going to create an alias R that is not really running R. Again, this is not going to cause problems for us down the road if we share data or code with other people because this is not going to affect how R is run, how it does its calculations or makes plots or anything. It only affects how it starts and quits, and so we should be fine. So we'll save that. We can do source till the period bash profile. Again, bash R C for people in Linux and Windows. Restarts that. And now if I type R, two plus two, plus two, quit, it doesn't ask me, the alias has worked. That's really nice. So again, we can think about where we use aliases to, you know, would be a good places where there are kind of commands that we run a lot, that we want shortcuts for, or where there's arguments that we tend to use a lot. There's gonna be a couple of examples of these in the exercises at the end. So what else can we do with our bash R C file? Well, I've shown you the goodies of getting the weather or other types of things. And one other thing that we might think about doing is modifying the prompt that comes with our terminal. And what I currently have is Redo, it's a breed of sheep develop in Canada. My current directory, current working directory, and then my user ID, a dollar sign, and then a space indicates that this is the prompt. There's some stuff in here that seems kind of redundant. And if you've watched previous episodes, you know that as I kind of make the screen bigger and font bigger, that I don't really have a lot of space kind of to the right side of my prompt. And to clean up some space, you know, the Redo and the peach loss, no one else uses this computer. And I know what this computer is called. So maybe I'd like to get rid of that and clean up the prompt so that again, the environment is a little bit easier to work with. Again, this is not going to impact how the analysis is done. It's going to impact my visualization, my interaction with my terminal environment. The variable, there's an environmental variable as they're called that stores what this prompt looks like. And that's called PS1. If I do echo dollar sign, PS1, we get out this gobbledygook. And this is code that tells the terminal, tells bash what to output here at the prompt. And so backslash lowercase h corresponds to the host name or the computer name. The backslash w is the working directory, which is right here. The backslash u is the username me, right? And then we see we have the dollar sign. Well, if we wanted to get rid of all that and only show the working directory, we could do PS1 equals without a space. And then in quotes, I could do backslash w space dollar sign. And that will then change my prompt to what we have here. So again, if I do CD tilde, it takes me back. And because I'm in my home directory, I now have a tilde instead of anything else. One thing I notice is that I don't have a space between my dollar sign and what I'm typing at the prompt. So maybe I'd like to put a space in there, right? And so now if I do RN, it'll take me there and it's got a space after the prompt. And you could use a percent sign or anything else you want at the prompt. There's nothing magical about a dollar sign. We could then put this into our bash RC file, do PS1 equals quote backslash w space dollar sign space. Save that and then do source. And we can see that it starts up and it's in that right directory. And if I do CD tilde, it takes, okay? So again, this is a way that we can modify our prompt to make it a little bit more customized, a little bit more interesting to us. There are other things that you can put in there, including things like the time, the date, whatever floats your boat. If you look at the episode notes in the blog post that accompanies this episode, you'll see I've got maybe six or seven different meta characters as they're called, like this backslash w or u, to tell you how you can customize the prompt to suit your needs. Now, a few years ago, I was at a workshop and I met, that's where I met Carl Broman, who I think I've mentioned in previous episodes. And I saw him do a live coding demonstration and was really enamored with his prompt. I had prompt and V. And what was so cool about his prompt was that his prompt told you the status of your GitHub repository, of your Git repository. It would tell you if you had changes that needed to be committed. If you had changes that need to be added, it would tell you what branch you were on. I thought it was really cool. And so what I'd like to do is share that with you. Now, it's a pretty complicated script that I don't fully understand myself. I can kind of figure out what it's doing. But if we go to the blog post for today's episode and scroll down, there's a section called Pat's Fancy Command Prompt. What I'd like you to do is go ahead and copy this all the way down from color prompt to include that branch information all the way down through prompt command. I'll copy that. And I will then replace PS1 with my fancy prompt. And I will save that. And I will then do source bash profile. And the prompt looks a little bit different already than what we saw when we started. When we started, all the colors were black or gray colored. And so now we've got some green and purple and cyan. What I'd like to do then is let's CD. We don't have to CD. We'll use RN. And this then takes us into our project root directory. And what you'll see is that in parentheses here, I now have master. And you'll recall that the branch we're on is the master branch. If I do get status, I'm on branch master, right? Very good. And it's green, which is the go color. I've been keeping track of some questions, research type questions in a document on my desktop. I'd really like to be able to put those into my readme file. And so that's a nano desktop questions.md. And you'll see that I've got these questions here. And why don't I go ahead and move questions into this current directory? And bam, the first thing you should notice is that master has turned to red. And that corresponds to a state where we have things that need to be added that are untracked. Go ahead and open up my project in Adam. And I've got my questions here. I'm gonna go ahead and cut those out and put those into my readme file. And I'll put these up here. And I'll go ahead and make this a second level question. And I will close that and close my questions. I don't wanna save it because I'm gonna remove questions. And again, if I do get status, I see that it's red that there's still something that needs to be added or committed that I've modified my readme file. Now I can do get add readme. And now it's turned to a yellowish greenish color, which again corresponds to having things that have been added. They're staged. They're ready to be committed, but not quite. I can then do get commit dash M, add research questions to readme. Bam, I'm right back to green. And so I really like this prompt again because it tells me where I'm at in my process of adding and committing changes to my repository. This also tells me what branch I'm in. Say I were to do get checkout issues 17, which I think was the issue from the last episode. You'll see that it no longer shows master. It shows I'm on issue 17, which is really convenient because sometimes I lose track of what branch I'm working on. And so I could say, oh, I'm on issue 17. I need to go to master. Again, get checkout master. Okay. So we're in good shape. It tells us if we do get status that we are ahead of origin master by one commit and you get push. And now these changes to my readme file have been updated. And of course we're gonna stay green. If we return to this code that we kind of blindly copied and pasted, you'll notice a couple of things that we haven't talked about in previous episodes, but hopefully make sense. So we've created a function called color my prompt. That's a pretty good descriptive title. And down here at prompt command, it then calls color my prompt. Prompt command is called before any time, at any time before a prompt is created. And as you look through this, I kind of figure out, well, it's creating some local variables that are perhaps only defined within the function. Things like user and host, current location. And there, sure enough, we see the username, the host name, the working directory. And here we see git branch, that it's getting the branch. It's then running grep on it and set on it to probably parse out the name of the branch, right? And I'll tell you that these other bits of information with like the 33 and the 01, 32M, those refer to colors. And that's way beyond where we wanna go. And you can see that it defines red, yellow, green here, and then it outputs it, right? So a lot of this should look very familiar to things we've done in the past. I would encourage you, if this interests you, to kind of play around with this a little bit. A good practice would be to make a copy of this file before you go in and change things, because if things get screwed up, you wanna go back. This is not under version control. Sometimes though, you can find that people post their .bashrc files to GitHub. So you might search around on GitHub for other people's .bashrc files to see what they're doing to kind of bling up their prompt a little bit. So maybe you have prompt envy. I have three exercises that I'd like you to work through. The first one is to create an alias for RM, the remove command, that prompts you whenever you're about to delete a file to make sure you don't delete anything that's really important. This can be done naturally using the RM space hyphen I and then the name of the file that you want to delete. And I ask you, where could this cause us problems with our other bash scripts? In the second exercise, I want you to create an alias called LSL that runs LS hyphen LTH whenever it's used. And then finally, I showed you how we could take the basic PS1 prompt and modify it to remove the host name and the username. I'd like you to do the same thing with the ColorMyPrompt function to get rid of that information so that again we have a bit more range on our command line with the prompt being a bit shorter. Go ahead, work through these exercises, pause the video, once you've got them, go ahead and press the play button again, come back and see how I've worked through these exercises. To get working on the exercises for convenience, I'm gonna go back to my home directory and I'll come back to my bash RC file. The first exercise asks us to create an alias for RM. That would be RM hyphen I, I'll save that and then again to run it to make it live, I have to do source dot bash profile. And if I, let's find something to delete. Why don't I RM my questions.md file? It says, are you sure you wanna remove that? I'll say yes and it removes it. I might do RM GitHub recovery codes. That looks important. Do I remove that? Oh no, no, no, I don't, right? So again, that's nice and that's convenient. So the second part of the question was, well, could this cause problems? And I think it could because in our bash scripts, we have a section that I call a garbage collection where we're removing extra files and we say RM space, the name of the file we want to remove because I wanna keep things automated and I've already invested the time to make sure I'm deleting the right things. I don't want this alias to get in the way and also again, the issue of portability of say you were to get a hold of my code and you don't have this alias. If I wanted to leave the alias, if perhaps I'm more worried about having the RM-i than not, then what I might wanna do is go back into my bash scripts and instead of only having RM, do RM space-f and I think that will then forces the removal without this interactivity. So I'm gonna go back sadly and remove that alias from my bash RC file. I don't want it. If you want it, go ahead and keep it, that'd be great. So another alias that we could create is LSL and that is going to equal LS-LTH. I'll save that and again, source my bash profile file. And if I do LSL, I get the long formatted output. If I do RN, LSL, I get the long, the wide format with the human readable sizes and it's sorted by time. So the most recent created thing is at the top and the oldest is at the bottom. I like that alias. I think I'll keep it because I seem to use LS-LTH an awful lot and for some reason I've never made an alias for it. All right, the third exercise then says we wanna clean up the real estate, so to speak in our prompt and looking at all of this, there are a number of places where we could perhaps clean things up, right? So I could go ahead and get rid of this line, the local user and host, but I think the easiest place to do it would be to go ahead and remove it here from where I export PS1. I'll save that and I'll tell you one place where I actually do like to have the host name is that sometimes I'm working on my university's high performance computer and I might be working on my local computer as well as on the high performance computer and I might forget where I am. And so having that computer name sometimes can actually be helpful, but I think this might be worth it to go ahead and remove the username and host name. I'll save that and I will then source bash profile and we can see that it got rid of this green part of my user ID as well as the host name and that I think that looks pretty good. I'll stick with that. Again, I would encourage you to maybe search around on GitHub for the dot bash RC files, see what other people have done in the blog posts that accompanies this video. There's a links to a couple other tutorials on modifying your prompt so you can do what interests you. And again, make it your own. Make it your own environment. It's important if you're spending any amount of time in these environments to make them comfortable, right? And to make it aesthetically pleasing to you because you're gonna be looking at it the most. It's not going to impact how the calculations are done or how the analysis is done. It's gonna impact your quality of life and so that's important. Thanks again for joining me for this week's episode of Code Club. Be sure that you spend time going through the exercises on your own to help develop your new skills with customizing your bash environment. It'd be great if you could take the ideas that we've worked through today and think about how they relate to your current projects. For example, what arguments do you always seem to use that would make for a nice alias? Tell us in the comments below what you have added to your own bash RC file. Also, feel free to ask any questions that you have in the comments below and I'll do my best to answer them in a future episode. Be sure to tell your friends about Code Club and to like this video, subscribe to the Riff Amona's channel and click on the bell icon so you know when the next Code Club video drops. Keep practicing and we'll see you next time for another episode of Code Club.