
By Whitson Gordon Become a Command Line Ninja With These Time-Saving Shortcuts
Image remixed from an original by xkcd. Power users love the command line for its ability to perform complicated tasks with just a few keystrokes. But for beginners who don't know the shortcuts and type everything out longhand, it can seem like it takes forever (I know this because I'm just past beginner myself, and I still didn't know most of these shortcuts). These commands apply primarily to *NIX-based command lines, which, out of the box, are included on systems like OS X and Linux. To use them on Windows, install Cygwin. (We highly recommend ditching Windows' crappy command line for Cygwin.) From simple keyboard shortcuts to built-in shorthand to commands you can build and customize yourself, the Terminal has a ton of ways you can speed up your command line work, and here we're going to show you some of the basics (as well some customizable features that you can use to shorten pretty much any command you want). If you're just getting started with the command line, we recommend you also check out our command line primer for beginners. Heck, even if you're not a beginner, skim through it—you're sure to find a few things you didn't know. We'll be touching on a few of those subjects and building on them here, too. Basic Keyboard ShortcutsWe went through a few basic keyboard shortcuts in our beginner's guide, but there are quite a few in there that, while not exactly intuitive, are incredibly useful just for navigating the shell. They also come in handy when you want to refer back to previous commands.
Terminal ShorthandWhether you make a lot of typos, run a lot of the same commands over and over again, or you're dealing with long, annoying file paths, the Terminal has quite a bit of built-in shorthand to keep you from typing everything out yourself. Here are a few great examples. File PathsWhen you're working with files in the Terminal, navigating the folder structure of your system can take forever. Constantly typing long filenames is never fun, so here are a few tricks that'll get those 5-folder-deep paths into the Terminal with just a few taps of your mouse or keyboard. Dragging Files Into the Terminal WindowIf you have a file buried deep within your hard drive, typing out its full path can take forever. Instead of doing that, you can just open up the folder in Nautilus, Finder, Dolphin, or whatever file manager you use and drag the file right into the Terminal window. It will add its file path to the current command. File Path ShorthandWe discussed these briefly in our beginner's guide, but they bear repeating. Say you're
That way, you don't have to retype the entire file path (or even hit the up arrow and delete a folder name—it's literally just a few keystrokes). This also works if you're typing out longer paths. Say you're in
and get there instantly. Another good shortcut is the dash (
Thus, if you're working in, say, your documents folder ( Lastly, if you want to go back to your home directory, there's no need to add any arguments to the Using Your HistoryAccessing recently used commands (or running a small variation of recently used commands) is something Terminal users often need to do. Unfortunately, it's also one of the most tedious parts of the Terminal—who wants to retype the exact same command they just ran but with one minor difference? It makes every typo seem like a punishment. Luckily, there's some pretty nice shorthand, most of which uses the handy bang symbol (!).
This will run your last used command with root privileges.
If you just want to see what the last
This will print that command and add it to the end of your history. If you decide you then want to run it, you can just type If you want to run a different command that you ran last, but with the same argument, there's a shortcut for that too. For example, say you had just created a folder using:
To then
The Another common problem is mistyping the command you want to run. Say you wanted to run
Instead of retyping the whole thing, you could just run:
This will find the first instance of
You'll get a list that looks something like this:
You can then pick a command out from that list—say I want to run
Lastly, if you want to keep certain commands out of your history, just put a space before them—i.e. space+ ExpansionsWhen you're working with variations of a file—like backups or different file types—it can get tedious typing out the same commands with small tweaks. Using the brace symbols ( Say you want to rename just part of a filename. Instead of typing out
This runs the command with the same arguments, only with the parts inside the brace changed—the first part corresponding to the first argument, the second part corresponding to the second argument. The most common example of this is when you're backing up a file that you're making changes to. For example, if you are tweaking your
Putting nothing before the comma will just append
Moving the comma to the other end of the brace will remove The braces can also work when moving or creating multiple files at once. For example, if you wanted to create three numbered directories, you could just run:
This will create three folders: myfolder1, myfolder2, and myfolder3. Making Your Own Shorthand
To create a custom shortcut (called an
Now, whenever you type
This gives you a more verbose list of files than
This will make your Desktop the working directory with just a few keystrokes. You can, of course, modify this for pretty much any folder that you access regularly.
This moves your working directory one folder up in half the keystrokes.
This is an example of an alias that opens up a file for editing. If you have any files you find yourself constantly editing, this is a good one to keep around—just throw the path to your oft-used file in the quotes and edit the keyword to something that makes sense.
With this, installing programs is much quicker in Ubuntu. You can just type
This will update all your packages in Ubuntu.
This is a neat function we've featured before that will essentially run These are just a few examples of the keyboard shortcuts, shorthand, and custom aliases that can make your life easier. There are really a ton out there, and depending on what you're using the Terminal for so often, different ones will prove useful to you. Of course, since you can make your own using | January 26th, 2011 Top Stories |
No comments:
Post a Comment