 How's everybody doing? How many first-time Drupal Connors? All right, that's what I like So we're gonna talk about cracking the shell command line for Drupalers The great thing about this being the last session of the first day is that I can talk as long as I want to So usually this session goes about two three hours, so just just so you know no, I'm kidding But seriously, there's a lot of stuff that I packed in here I've cut out a lot of things over the few years. I've done this talk and updated things so There's a lot that we'll go over and the idea is just to kind of wet your appetite Show you things that are possible also to demystify a few things So but before we get started Let me just tell you a little bit about myself my name is Bob Keppford I'm a lead architect for media current and we I'll tell you a little bit more about media current, but it's a great Drupal agency that I've been at for about five years now and it's a great place to work I also publish a weekly Drupal newsletter called the weekly drop Yeah, nice. Got some subscribers and As a part of media current I do a podcast with some of my co-workers there called the Media current dropcast and that's fun. That's a fun little Semi-regular podcast that we do we just talk about Drupal. Sometimes we'll have people in the industry on we just had an interview with Westboss a few months ago And it's just we have it's it's not really that serious. We kind of have fun with it But it's it's fun. So some people like I think we have a few subscribers. Maybe two or three But anyway, so media current is the reason I'm here We are a full-service digital agency that implements world-class open-source software development Strategy and design to achieve desired goals for enterprise Organization seeking a better return on investment and obviously I do not work in sales I do work as a on a dev team. So Just bear bear with me with this with the public speaking stuff, but in all seriousness media currents are a great agency We do great work. I'm really proud of some of the work I've got to do there and if you're a developer and you're looking for a great place to work Hit us up. We have a booth and I'm gonna have the details for that Later on in the presentation and as well. We have a party tonight and there will be info in the last slide on that And if you are looking To have some help with your website Come see me Afterwards we'll go to our booth. We'd be happy to talk to you and we do great work. I might be a little prejudiced, but Anyway, so let's get started So first of all What is the shell and I'm gonna ask for some audience participation? if you Don't really feel comfortable in the command line or in a shell or any of this stuff raise your hand. I Just now realizing this might be kind of embarrassing, but it shouldn't be so it are you okay? Let's let's ask it this way. How many of you are would consider yourselves a developer? All right, there we go So all of you should be using the command line since we're doing Drupal and we all consider our pretty much all of us developers So let's let's just forget about those fears about the command line. It's not that bad So what is a shell? So a shell is just a user interface that allows you to access the operating system services. That's the simple version of what a shell is One another turn that you hear thrown around a lot more often nowadays is command line interface or CLI and Applications have their own CLI or websites have a CLI like github Or a programming language might have a CLI So that's just a command line interface a way for you to interact with that application or that service by typing text commands So it's really simple. It's one of the most simple interfaces. You can have with a computer But what are the strengths because there are definite strengths of the command line and there are definite weaknesses And let's just talk about them real quick. So one of the strengths is it's light on resources The commands usually are pretty concise It's scriptable so you can automate things whereas when you're working in a GUI That's not always possible. Sometimes it is possible, but it's always more complicated if you need to do it So those are some strengths. What about some weaknesses because there are definitely weaknesses The commands are not obvious. So you're not going to just kind of be Intuitively typing things and figure stuff out. I mean sometimes you'll get lucky But you can also really mess up your day if you just start randomly typing stuff in your command line So I do not recommend that It's really not beginner friendly although when I started using computers. That was the interface back in the DOS days Anybody remember that? So it's not I wouldn't say it's beginner friendly definitely not like a GUI is but that doesn't mean We should avoid it So why should you learn to use the shell because I do get pushed back sometimes from even my co-workers That why do you focus on this stuff? Why do you use a shell? There's a GUI app that does the same thing or whatever and this is this is 2018 Bob Come on get with the program. So I get that a lot Here's my favorite response. It's a Unix system. We work on Unix systems You never know when this knowledge will come in handy So the other thing I always like to bring up is that Unix systems have really won the war Even Microsoft allows you now to install Linux applications and command line tools on Windows which are things. I never thought I would actually say out loud So in my opinion when it comes to the work we do the Unix based systems have won the battle I mean most of us are probably using some sort of Unix based system to do our work So there's a big big plus there and I've never regretted learning a command line tool And I always like to quote myself at least once in my talk And I mean that sincerely there are many things I have learned that have been a waste of time. I Will just say we'll just say flash so CLIs are everywhere I mentioned earlier I mean this could be just pages and pages everything has a CLI if it's cool It definitely has a slick CLI with a bunch of cool stuff that it outputs. So There's your reason right there. We write PHP work with my school we work with Drupal We work with composer more and more we work with node We probably still work with sass to some extent and every JavaScript project ever imagined as a CLI or at least works with the CLI so CLIs allow us to script things and automate things and as developers if there's one thing we love to do it's automate things because we're lazy and Lazy in a good way in that we don't want to do mindless work We want to actually challenge ourselves So we want to focus on doing things that challenge us not things that just mean punching buttons over and over and over again So this is my main reason I like to focus on the command line is I like to automate and I like to customize and personalize now These are two separate things because you can automate stuff and that works well for a team But if you start personalizing things then it gets a little different because when you sit down on somebody else's computer You don't know what to do and I am definitely in that boat a lot Not that I don't know what to do, but my fingers know what to do my head has to catch up so But I will say that 99% of the time I'm on my own computer and I benefit greatly from the customizations I've made on my own machine So I want to make my computer work for me It's the main point so this is a little bit of a dated demo, but basically it gets the point across What I'm doing is if for a typical project I'll have what's called a team up session and it allows me to open up multiple terminals in different panes That can have applications running in them So I can kind of just have a dashboard to see what's going on right when I'm writing my code So I've set up a team up session here split it into multiple terminals I can look through my git commits and and look at my history I have Drupal console all set up ready to go and this is all like in a script I just have executed one command at this point It's set up my drush aliases so I can just type drush commands in different terminals And they will interact with a server or a local environment and all of this is set up I just show you this because you can go this far. I'm not suggesting everybody do this Maybe you have other things in your life that are more important But I enjoy this and this has been over the year something I've set up So I'm going to walk through a lot of this stuff in here, but not all of it But as you can see things are getting pretty serious for me in this environment. So When I realize something is really valuable. I enjoy it. I take it pretty serious When it's code related, what do we do with code that we want to preserve a history of? That we committed to What do we do with that code? Version control right and version control as we all know means git so We put it in a git and If you go to github, which is hilarious to me sometimes I just aside I I'm around quite a few newer developers and they will use git and github interchangeably, which is fun For for somebody who's been around for a while, but anyway dot files So dot files are kind of the Community's name for all the configuration that for all your command line interfaces and some of even some of your GUI apps So if you go to github and just search dot files, you'll find tons of developers Configurations and you'll learn all kinds of crazy tricks that people have come up with to automate their work Or to configure apps in ways you never knew they could be configured And so dot files are the stored configuration And they always begin with a dot or almost always So that's why they're called dot files and the reason they begin with a dot is so that they're hidden from the GUI File browser so most app most operating systems If you open like an apple if you open a finder, you're not going to see any of your dot files They'll be invisible. So they're so you can't mess with them And they always live in your home directory So tilde Oh, there's there's a debate about how to pronounce this. I don't know if I said it right, but They always live in your home directory, which is the tilde So What do I do like that's we don't really want to put our home directory in version control just fy. That's not a good idea Um, so typically what you'll see people do is they create a directory for all their configuration and then they sim link all of their Dot files to that directory and there are tools that allow you to do this pretty easily I don't use them because I started doing it before it became cool. I guess i'm a hipster. I don't know I'm probably not a hipster, but Anyway, if you have to ask I guess you're not Um, but this allows me to track them all in one One git repository and keep a version of it and I can actually publish a minor online And the other nice thing about this is when I get a brand new computer Um, I can just clone that repo and then boom. I'm ready to go like I have all my git configurations stored I've got my vim rc all set up. I've got all my home brew stuff. I'm just it's just great Um, if anybody's ever had to go through installing all the software that you had at your previous jobs computer on your new computer That's a day sometimes for some of us. So this saves it saves a lot of time The other thing I like to do is go and just steal people's ideas if you go to github There's just tons of great stuff and you can go in there and I don't recommend copying anybody else's configuration Whole sale, but it's good to look through and like oh, that's a great idea Uh, just it's open source. So just like drupal like we learn we learn from other communities We learn from wordpress wordpress learns from drupal. So same idea So a couple things. I don't know how many people are familiar with simlinks, but we'll just do a really quick Uh, a really quick primer on this. So basically a simlinks just a pointer So you store your file in location a and you actually want it to look like it's in location b So you create a pointer or a sim link to do that and the command is really simple But this is essentially how you can control where files actually live versus where the operating system can find them And so that's kind of key to this whole thing The other question that always comes up is what configuration do we want to add to our dot files? What kind of stuff do we want to configure and store? And as my dentist says to me every time I go in for a cleaning only faucet teeth you want to keep And i'm pretty sure he stole that line, but he steals stuff liberally. So including Well, I won't say it, but he's a dentist so Wow, I didn't mean to I didn't mean to offend dentist. Are we any dentists in here? Yeah, I didn't realize it till just now No, actually he's cool. Anyway, so the shell so let's get back to the shell So we've talked a little bit about what what dot files are and kind of how you would do this type of work What do we put in our dot files? Well, the first thing is what shell are you using? There are three popular shells bash being the one I use it's kind of the old faithful It's on every system pretty much And it works for me you can customize it and improve it But if you learn how to use it you can pretty much use it on any system There's some newer shells that have a lot of cool stuff built in zsh is one I'm always on the fence about whether to use it or not. I've tried it. It's really great Then there's a new even newer one called fish Which is even has even more slick stuff. So I'm sort of sure some of you might have been familiar with these But definitely take a look at them If you don't want to really build your own customizations, I recommend the latter two if you don't like the bash configuration Another thing we would store in our dot files is our environment settings. So the the idea of this is Instead of typing the full path to the command line application that you want to use let's say drush for example um I would rather just type drush So in your in your environment variables You can add these to your what's called your path And the path is just an environment burial on unix systems And it allows you to specify which directories are executable Executable programs. So instead of having to type of the full file path You can just type drush and it'll just it knows where on your system drush lives and it executes it Another cool thing and this is a really easy way to kind of get into customization Is to set up aliases in your bash rc file? And I have a few that I use here. I have actually a ton, but These are really simple ones Just if you find yourself typing the same thing over and over and over again You know why when you can just shorten it. So why do I want to keep typing history when I can just type h? Another one is l where I can just type that and instead of getting l instead of tapping to type up this whole long command ls dash l hg Which is a really pretty way to look at all your files. I just type l Another thing that is very useful is is writing your own functions and it doesn't have to be that you don't really have to learn Um bash scripting in order to do this. I mean, this is a really simple way Um, this is a function named take which basically just makes a directory And we'll make it well it'd be easier if I just show you so I just type Take and then the file path and I make what I'm trying to do is make three nested directories one two and three And what it just happened is it created all three of those directories and then put me in the actual deepest directory So that that's just to show that all I didn't have to type all that command I could just type take and then an argument and it's done So there are millions of applications of that that you can do they can be drush commands They could be like commands to back up your database and move it to a specific directory And once you learn how to do this you realize like why wasn't I doing this before? It only took me 10 minutes to learn how to write a bash function So a lot of a lot of potential there Another one is ssh and this one always cracks me up. Um, well, I shouldn't say cracks me up. Um, this one I I think I would just benefited from being around a sys administrator who would watch me type and just Go nuts. So he would say stop. We need it. We need to we need to straighten this out. So ssh How many have written or have typed ssh commands to log into a server before? How many of you remember the ip addresses and the username and the port number or do you have to go look that up? Yeah so There's a file in your uh, and I don't recommend putting this in your git uh and storing it and putting it on github I may or may not have done that at one point in my life um There's no there's no password in here, but There is ip addresses. There are port numbers. There are user names But you can you can go in here and I do this for frequently used servers I'll just make a nickname for it and that saves me from having to remember all of this stuff and type it wrong I possibly log into the wrong version of the server wrong environment And also it's nice just to have a written record of it as long as this isn't Things that are supposed to be kept secret. So Privacy and security as I said To not put these on github. You're just exposing information Uh for hackers to exploit So let's talk a little bit about git. We talked about we store everything in git, but Um git is a powerful tool and there are really nice Graphical user interfaces that look pretty and allow you to do a lot of bad things So be very careful if you're just learning git Please There are plenty of tutorials online learn the basic command line command So you kind of understand what's happening because if you jump in and you just go straight to say we'll just pick on one um github's application Yeah, most of the time it's going to be fine And I don't even have a problem with people using it. I've used it before But you do need to understand the basics of git so Spend a little bit of time and learn that you'll be very thankful for when things go sideways You won't be freaking out as much. So it's it's well worth the time investment to to spend Maybe even a day just learning more about git when you have time So listen Or sorry learn learn to use these commands. It's git is I like to say git is um Gets like a good set of steak knives Um You can do amazing things you can cut up some some great food and people will look at like wow This is so well presented, but you could also lose a finger. So be careful Learn what you're doing. Um, and just be careful so git configuration This is where it's typically stored You can do all kinds of customization to it if you're in git Git is probably one of the tools I use most every day And if I'm going to use a tool I want to configure it So I've I've set up my global Excludes file to kind of have a default git ignore. I've customized my colors a little bit I've set my default editor to vim so that it's not opening nano Um anybody no no, okay Um also another thing you can do if you don't like command line editors say let's say you use adam or vs code I think it's what the cool kids are using you can set that up here So when you want to go git commit It'll open adam or whatever your graphical editor is and that's very handy because You'll be surprised how many people have a hard time just quitting them Or nano really um And I set up some other defaults, but you get the idea you can you can set up things You can also do git aliases as well as you know the command line aliases or git aliases And and I have a lot of these that that I use because I just like Not typing So I have short names for my git commands git log And I have git lol, which basically gives me A pretty graph of my Of my log I can do git ll which adds to the num stat and number of lines changed. I can do lol a And these are all in my my dot files and I'll have a link later And then I there's one that I specifically really liked which was to see how much work done I've got done today I have an alias called git lol m which is just my commits And it just lists out all of my commits, but in order to type those commands There's no way i'm going to remember how long they are because if you look They're really ridiculous like no one ever types these if if they do they're a wizard I don't know how anybody would remember this stuff so Yeah, another one with git is ignoring files And there are basically three ways to ignore files with git You can do it per machine, which was the home directory thing I talked about that's kind of your default Per project so a dot git ignore if you look at Drupal the Drupal project has a dot git ignore and for the project Everyone that works on that project will have the same git ignore and that files actually committed to the project So if you add something there then it's ignored for everybody that works on the project But there is another way to ignore files which does not have that side effect and it's excludes So if you go to dot git slash info slash excludes in your git project You may not have that file just create that file You can actually have a per repo instance ignores file So let's say for example you use vs code and vs code Whenever you set up a project creates a dot vs code directory in the root of your project Which is wonderful Just like php storm does kind of the same thing and then What happens when you're working with a team and they don't want to add that to the git ignore for the project Well, you can just add that to your info slash excludes And then it would be excluded for you and you don't have to bug the lead dev on your team And you can just ignore it So this is there's a lot of uses for this another seldom used I think Git feature is hooks and a lot of times people use this in their deployment and we're not doing a dev ops talk this is all personal stuff on your machine and Basically what hooks are they're kind of like drupal hooks if you're familiar with those So whenever there's something happens you can hook into that action and perform Actions that you want so you can run a command every time you do a git commit Or you can run every time a command every time you do a git push So you can hook into all these different commands And so it's really useful. Um, you can check out that uh, that link there talks about customizing it Here's a couple of ideas of things I do with it There's a I use the pre-commit hook for to do codelint code linting I have that set in my editor, but sometimes something will slip through and this is this catches me So who's ever left a debug statement in code and committed it? Yes, that was that's some of the best Case scenario things I've done in my career. Um, but this helps me with that. It'll it'll catch that and there's a link there that's a simple Drupal specific pre-commit Script and I would recommend checking that off you do Drupal work Another thing which is very useful if you work on a team and you kind of need to share A history of what you've done on your team Is the post commit and I run this this is set up on all my repos So basically when I do a commit It's on my clipboard the whole commit message is on my clipboard and then when I go to the project management software We happen to be using I can just paste that in there and I void typing it twice So I just it also encourages me to write really good commit messages because I'm like well I'm going to use this multiple places so it needs to make sense and be complete sentences and we all do that right We all we nobody says added changes If you're doing that, please you will hate yourself in the future for doing that because get us forever So let's let's try to Yeah, we've all done it but anyway Here's the problem though a lot of times you forget to add these hooks because you have to add them per project And I used I used to forget this so here's where we're going to go back to the git configuration And then we're going to basically add an alias called in it so In case you don't know, I mean a lot of us Maybe just starting off with git, but if you start a new project you type get in it to make a git repo And so this is an alias that basically grabs a template file that I have which has everything I typically use with my git configuration Um So that's that's a handy thing to use and then I have a git template directory where I put all my hooks files and all that Kind of stuff and you can you can keep this simple or as advanced as you like, but it's a useful tip So that's git there are git tools that make git better um because let's be honest I I really do love git but It could be better. Um, one of the things that I like To use is tig which is a text mode interface for git. So imagine how great some of these gooey applications are that work with git um tig is like a command line version of that it allows you to Do things that the normal git cli doesn't Here's it. We'll just look at a quick example So it makes it really easy to kind of go through the history of a project and then inspect different commits I actually use this to do most of my uh change staging. So instead of just using um The git cli and add git add a lot of times I will just run a command and then I can go through this interface because I like it better So as you can see like I'm basically staging some files looking at the changes And then staging them for the next commit Another tool I like and this is just because I like pretty things is git hud And if you were like as a kid really in into airplanes like you'll know what a hud is like I was the nerd that Just went crazy over f-18s and things like that. So I love anything that can trick my mind into thinking I might be flying a jet So I have to really watch it too. Um, but the git hud basically gives you a quick glance at what branch you're on Um gives you some color in there and how many modified files. It's it's actually Pretty advanced. So and I I can't explain exactly what's going on here. Um, we don't have time But it does give you a quick way to kind of see the status of your of your git project All right, so we'll talk about vim and and I have cut out most of the vim stuff because not everybody likes vim But there are basically two Ways people look at vim They're the people that don't use vim That see vim users and they're like godlike powers over their code And they can do things that just are you looking like how did you do that with so many so few keystrokes? But for the reality the reality is most of us are here Or or have been here So that's where I was but Seriously vim is not easy to learn. So there is no we we got to quit shaming each other and stuff like you don't know How to use vim? Oh We need to really stop that because It's just a tool but at the same time There's a reason why I use it as my primary editor and the main reason is is rsi and I had issues Probably seven or eight years ago with that and realized that I had to change a lot of my ergonomic habits and then I started looking at my keyboard and and how I did stuff and I said, well, why am I doing all this typing and one of my friends Who knew me really well said like, you know, you are like in the sweet spot for being a vim user He's like, I don't know why you don't use vim. So I took him took it to heart and I started learning it but In serious as vim's been around forever. I plan on coding up into the point I can't type probably and maybe then they can have some other way to do it but I I love text editors and I used to switch all the time like every time the new one would come out Oh, I switched to that and like I realized that that was just kind of like the new hot thing I just wanted to be doing the new hot thing and like why am I doing this when I could just learn something That has tons of potential and I get investing in it over and over through the years And I'm not losing anything because it's all building on top of each other instead of me just using the new thing it's also I'm already in the shell So I'm already in the command line a lot. I can just type vimp and it'll open up my directory and I can start editing files Another cool thing it's open source um And it's available on all systems. In fact, there's actually a fork of vim called neo vim Which is really really slick if anybody's a vim user check that out. It's pretty cool Um, and it will never die. I think if we do have a nuclear holocaust, which I hope we don't there will be two things left cockroaches and vim Well, that's possible And I love using the keyboard. I I'm not a mouse guy. I don't like clicking around and stuff Obviously, it's why I'm doing this talk. So yeah So like always the question comes well can vim do whatever and the answer is always yes Just like can drupal do Anything yes, it can do it should it maybe not So that's that's that's subjective, but here are a few plugins. I'm not going to go through them real quickly But if you do use vim or if you've considered it, here's a couple or few that are pretty useful for drupal development um We'll look at really quick at vim fugitive, which is basically a rapper for git so you can um interact with git And i'm just going to show one something real quick git blame so you can everybody know about git blame So if you go to a specific line in a file, you can see who did that Not it should be git credit or something, but it's git blame for some reason The only reason you ever care about who wrote this stupid code is because it's broken. So I guess that's why Um, but anyway, there's all kinds of stuff. It does you can stage files pretty much It's a rapper for git. So you don't have to leave vim to do your commits and stuff like that um another one and I I'm I really like sublime text. It's a great editor Uh is git gutter, which basically they stole this feature the plugin writer just implemented in vim Which is another reason why I like vim is because people look at what other editors are doing like Oh, we should write a plugin for that and they do So basically where your changes are in the file most modern editors do this out of the box vim doesn't do a lot of this stuff Which is great small feature set but extendable to infinity Another thing that people always ask about is how do you do debugging? You know, I really hate php storm But I need to debug my code and I don't hate php storm for the record But if the only reason you're using php storm is for debugging I mean and you like vim. There's this client. It works really well Um, and I'm not going to go through the whole thing here, but it gives you all the features of x debug. So if you need to Um, step through your code. You need to watch a variable. You need to inspect objects all that kind of stuff Um, I like I can't imagine not being able to do debugging right, especially with droopal 8 Um, so setting up a debugger if you don't use vim, whatever whatever editor you have You need to get x debug set up. You need to do that because you will learn Stuff that you never thought you would learn just by stepping through the code that you're running on your computer And then there's this really cool project. Um, I've never met the maintainer, but he's very responsive vmrc for droopal If you use if you write droopal code and you use vim, I would look at this It may vim users tend to be a little pedantic So you might not like every decision made in here But it's worth looking at and if you want a really quick and dirty way to get droopal set up in vim, this is a good one And I'll skip that. So let's go to drush. So we've been talking about kind of general stuff Let's talk about droopal specific stuff. So Drush is a tool we should all be using if we are droopal developers One of the things that I neglect to say a lot is that it makes it really easy to get a server going If you have php installed on your system You can just run a simple command and you have a local server running without, you know, vagrant or anything like that You can download and enable modules You can back up and import databases and you can perform updates and much much much more One of the things you can watch the logs And my personal favorite is logging in as any user This is extremely useful Especially when you have a complicated site with all these different user roles in droopal And they have different permissions and you want to reproduce something so you can log in By running a simple command you can log in as that user So let's talk about drush serve. So php comes with this built-in web server That a lot of people I find a lot of people that for years. I didn't know it existed And drush makes it super easy because all you type is drush serve and it works as long as you have your site set up And your php install and stuff, but Drush aliases are a really cool tool to Has anybody ever typed drush and then they get that message and says like I don't have the access or whatever. I can't remember the message, but Yeah, you can't bootstrap droopal type drush dad as you get nothing And there are ways to go around most people will go cd to the default directory which works most of the time But when you have let's say you have Three server environments and you have your local environment and you you know, you don't want to have to type out that Whole long thing or ssh in you can set up aliases and i'm not going to go through how you set this all up It's well documented in the drush documentation But you can set up aliases and they look kind of like this You set up a file and then when your your command looks like this So you'll name your alias whatever i'm really creative. So I said your alias and then you type