Handy Command Line Shortcuts

Video loading...

  • 0:02
    Handy Command Line Shortcuts with Addison Berry
  • 0:08
    Welcome to the next video in the Command Line Basics series.
  • 0:11
    In this video, we're going to be looking at some handy
  • 0:13
    shortcuts for while you're working on the command line.
  • 0:17
    These are not commands themselves, per se,
  • 0:19
    but these are ways to move back and forth within a line of text,
  • 0:23
    or move forward or backward in the history of commands
  • 0:26
    that you've done.
  • 0:27
    And these are the kinds of tools that make working
  • 0:30
    on the command line much, much nicer.
  • 0:32
    Since this video relies on the keys I'm actually
  • 0:35
    pushing on my keyboard, I've set it up so that
  • 0:38
    you'll see the keys that I'm pushing in the lower right-hand
  • 0:41
    corner of my terminal screen as we go here.
  • 0:43
    The first thing I want to look at is just sort of reviewing
  • 0:46
    commands I've already typed.
  • 0:48
    I'd like to reuse stuff if I already typed it previously.
  • 0:50
    And if I hit up my up-arrow key, you can see,
  • 0:52
    I'll go back through all of the commands that
  • 0:55
    I've recently typed in.
  • 0:57
    Once I get to one I want to use,
  • 0:58
    I hit enter, and it'll execute it.
  • 1:00
    You can see that this actually did move me.
  • 1:04
    It's just reusing a command that I had earlier.
  • 1:07
    Now, in addition to the up-arrow key,
  • 1:09
    you can also use CTRL+P for previous.
  • 1:12
    And this is more reliable.
  • 1:14
    The up-arrow doesn't necessarily work.
  • 1:16
    Your keyboard may not have it.
  • 1:18
    But CTRL+P will always work to go to your previous commands.
  • 1:22
    You can just scroll through in that way.
  • 1:26
    Now, if I want to come back forward again in time,
  • 1:29
    then I can hit the down-arrow key.
  • 1:30
    And that will start moving me forward, rather than backwards,
  • 1:36
    for previous.
  • 1:37
    And you can see, just moving back and forth
  • 1:40
    with the up and down arrow keys.
  • 1:42
    To move forward, I can do CTRL+N for next,
  • 1:46
    as well as the down-arrow key.
  • 1:49
    Again, this one will always work.
  • 1:51
    I get to where I want, hit enter, and it'll execute it.
  • 1:56
    Now, that works all well and good when you can just
  • 1:58
    scroll back and forth.
  • 1:59
    But what if you want to find a specific command that you
  • 2:02
    know you did a little while ago?
  • 2:04
    You can use CTRL+R to actually do a search,
  • 2:06
    and then start typing in letters that would be in
  • 2:08
    the line that you're looking for.
  • 2:10
    So, if I do rm, and now if I hit CTRL+R,
  • 2:12
    I'll scroll through all of the lines I have that
  • 2:16
    have rm in them.
  • 2:17
    I can also change this, so instead of rm,
  • 2:19
    let's say I really wanted rts.
  • 2:20
    Go back, change that, type that in.
  • 2:23
    Once you find the command, if you hit enter,
  • 2:25
    it will actually execute the command that you selected.
  • 2:31
    Now, sometimes you don't necessarily want to execute it,
  • 2:33
    you just want to find the command and reuse it.
  • 2:36
    Let me do another search here with CTRL+R.
  • 2:39
    And this time I'll look for cd commands.
  • 2:42
    I can do CTRL+R to scroll through.
  • 2:46
    OK, and this is the one I want, but I want to modify it.
  • 2:48
    So, I'm going to hit the escape key instead of enter.
  • 2:51
    And now I can actually modify it.
  • 2:54
    To move around on the line to modify it,
  • 2:56
    I can use my right and left arrow keys to move back
  • 3:00
    and forth to get to the place where I want to change things.
  • 3:04
    Just like with the up and down arrow keys,
  • 3:06
    there are other modifiers.
  • 3:08
    So, I can use CTRL+F to move forward on the line.
  • 3:11
    It's the equivalent to the right-arrow key.
  • 3:14
    And I can use CTRL+B to go back,
  • 3:17
    which is the equivalent of the left-arrow key.
  • 3:19
    Quick shortcuts to move to the end of the line.
  • 3:23
    If I do CTRL+E, I move to the end.
  • 3:26
    And if I do CTRL+A, I move to the front.
  • 3:29
    It's a quick way to hop, especially if you have
  • 3:31
    a really long line.
  • 3:32
    So, CTRL+E to the end, CTRL+A to the front.
  • 3:35
    Alright, let's start modifying.
  • 3:37
    I'm going to do a CTRL+E to go to the end.
  • 3:40
    And I'm going to start removing things.
  • 3:43
    I can hit the delete key on my Mac keyboard,
  • 3:46
    and I'm going back to delete items.
  • 3:49
    But the actual keyboard shortcut that you can use,
  • 3:53
    as well, is CTRL+H.
  • 3:55
    CTRL+H will eat up characters that are
  • 3:58
    in front of the cursor.
  • 4:00
    Now, if I use CTRL+B to move into the middle here,
  • 4:03
    I can delete the other direction in front
  • 4:07
    of the cursor using CTRL+D.
  • 4:09
    So, CTRL+H will do in front of the cursor.
  • 4:12
    CTRL+D deletes after the cursor.
  • 4:15
    Another handy way to delete the stuff is to use CTRL+W,
  • 4:19
    which will remove the word prior to the cursor.
  • 4:25
    Let me go back up here and find something with more words in it.
  • 4:29
    This has got words, so you can just sort of
  • 4:32
    see how this works a little more clearly.
  • 4:34
    So, CTRL+W will just take from the cursor
  • 4:36
    to the beginning of the word.
  • 4:38
    Even if the cursor was in the middle of the word,
  • 4:39
    it would just then take just the front,
  • 4:41
    from cursor to the beginning of the word.
  • 4:43
    But again, a handy way to just sort of
  • 4:45
    chop text out very quickly.
  • 4:46
    OK, let's get even fancier with taking out huge
  • 4:50
    chunks of text here.
  • 4:52
    I'm going to go back, and just, I want to find a bigger
  • 4:55
    command that I have, like, say, this one or something like that.
  • 5:01
    I didn't actually want to execute that.
  • 5:03
    Let me go back.
  • 5:04
    There, I just want to select it on the line.
  • 5:07
    I should have hit escape, not enter.
  • 5:09
    Now I can go back, so I can CTRL+B to get sort
  • 5:13
    of to the middle of the line.
  • 5:15
    And let's say I would just want to chop everything at the front,
  • 5:18
    from the cursor to the beginning of.
  • 5:20
    I can do that using CTRL+U.
  • 5:22
    It just wipes everything away, and only keeps the cursor
  • 5:26
    and then to the end of the line for me.
  • 5:28
    And now I can just begin typing in whatever it is,
  • 5:31
    and I still have that little bit of text that I kept.
  • 5:35
    CTRL+U wipes you to the front of the line.
  • 5:40
    You can also use CTRL+K, and that will wipe out
  • 5:43
    everything to the end of the line.
  • 5:45
    If I had something, and then I wanted to just chop off the end,
  • 5:49
    you use CTRL+K, and that will get rid of that stuff.
  • 5:52
    And then I can type in something new here.
  • 5:56
    OK, we've looked at a lot of the basics
  • 5:58
    of being able to move around on the command line,
  • 6:00
    how to go back into our history,
  • 6:02
    how to search for a specific command we've done before,
  • 6:05
    and then how to move around on the line to get to
  • 6:08
    the place where you want to be, and then ways to sort
  • 6:10
    of chop text away so that you can type in what
  • 6:13
    it is that you need and reuse your commands.
  • 6:16
    All very handy stuff.
  • 6:18
    I want to show one final little trick,
  • 6:21
    which I think is very cool.
  • 6:23
    And I picked this up from Shawn Powers at linuxjournal.com.
  • 6:26
    And I figured I would add it here because I love this.
  • 6:30
    What I'm going to do is I'm going to a directory where
  • 6:32
    I don't have permissions to actually do what I want
  • 6:34
    to do here.
  • 6:35
    And I'm going to just type in my command.
  • 6:36
    I'm going to create a file called secret.txt.
  • 6:39
    And darn it, permission denied.
  • 6:42
    So, now what I need to do is add sudo.
  • 6:44
    I can go back up in my history, CTRL+A, type in sudo,
  • 6:49
    and now I can run my command.
  • 6:51
    But an even handier way to do this is if I was to
  • 6:55
    just type sudo, and let me chop that away.
  • 6:58
    Pretend like that's not there now.
  • 6:59
    I type sudo and then a bang, bang, or exclamation point,
  • 7:03
    exclamation point.
  • 7:04
    And it'll rerun the command with sudo at the front.
  • 7:07
    And you can see it actually prints out what it ran.
  • 7:09
    And now we can see it created the file.
  • 7:12
    Really, really cool tip.
  • 7:15
    I'll show it to you one more time here,
  • 7:18
    because what I'm going to do is try to remove this file now.
  • 7:22
    Again, I don't have permission,
  • 7:24
    so I'll try, but it won't let me.
  • 7:26
    But I can just type sudo, and then the bang, bang,
  • 7:30
    and it reruns the command with the text
  • 7:33
    appended to the front.
  • 7:36
    I'll go show you one more example just to sort of show
  • 7:39
    other use cases where I tend to use this myself.
  • 7:43
    Sudo is probably the most popular one.
  • 7:47
    But let me see.
  • 7:49
    I should have some files in here.
  • 7:51
    I have stuff and newstuff.
  • 7:54
    I'm going to create a diff and look at the changes
  • 7:57
    that have happened.
  • 7:58
    And then I want to create a patch file for it.
  • 8:00
    I can do the diff command.
  • 8:03
    And if you hit tab, you'll complete lines.
  • 8:09
    If I type the first few letters and hit tab,
  • 8:10
    I completed those filenames.
  • 8:12
    I run the diff, and diffs automatically
  • 8:14
    outputs to the screen.
  • 8:15
    So, I can review and see what the changes are.
  • 8:17
    But then I can do bang, bang, and then do the output to
  • 8:21
    my patch file, and it'll pull all that stuff in,
  • 8:24
    so I don't have to retype any of that.
  • 8:26
    And you can see it actually really did create
  • 8:29
    the patch file properly.
  • 8:32
    I love this tip.
  • 8:32
    I use it a lot.
  • 8:34
    And I hope that these are really useful tips for you,
  • 8:37
    in general, because I find, once I've found these
  • 8:40
    kinds of shortcuts, I found working on
  • 8:42
    the command line a lot less frustrating.
  • 8:44
    So, have fun.
Loading ...

Handy Command Line Shortcuts

Loading...

This video covers some handy tips for reusing previously run commands and moving around through a line of text in the command line interface. These shortcuts make working on the command line much faster and nicer. Trying to remember all of these is not easy to do until you've used them for a while, so we also have a handy-dandy cheat sheet which you can find in the Downloads tab and refer to whenever you are in command line ninja mode. There are always new shortcuts you can learn. Another great shortcut !$.. It's similar to !!, but instead of repeating the last command, it repeats the last argument given. This sequence of commands shows how the shortcut could be used: mkdir sites/default/files followed by chmod a+w !$. That would be the same as typing out chmod a+w sites/default/files

Note: this video was originally released July 9, 2010 on Lullabot.com.

Note: In some places the command line prompt is cut-off. The YouTube version of this video doesn't have the cut-off problem. We are working on getting this fixed, but in the meantime, check out the YouTube version instead.

Command Line Basics 12: Handy Command Line Shortcuts (youtube.com)

Downloads: 
Log in or sign up to download companion files.

Additional resources:
There are no resources for this video. If you believe there should be, please contact us.