Learn these macOS Terminal shortcuts so you can spend less time typing
Many people stay away from macOS’s Terminal because of the tedium of typing in long, precise commands. But you can save lots of time and type less by using the command history functions built into the Terminal shell. The shell keeps a record of the commands you run, and you can access this history with a few simple keystrokes to run commands again or edit them, so you don’t have to retype them.
Terminal saves the command history in last-in, first-out order, which means that the first command in the history list is the last command issued. If you want to run a command that you’ve already typed—say you want to rerun the last command you just typed—all you have to do is press the up-arrow key, and the shell displays the last run command at the prompt.
If you want to run a command you ran earlier, press the up-arrow key several times. As you do this, the shell cycles through your last commands, displaying each one at the prompt. If you decide that you don’t want to use a command in the history list, just press the down-arrow key until you get back to an empty prompt, and then enter a new command.
Rewriting history
Using the command history, you can rerun previous commands, but you can also modify commands and run them with different arguments. One of the simplest ways to do this is to use the !!
command to run the previous command, appending new information to it. Let’s say you want to list the contents of a directory, like this:
ls ~/Library/Preferences/ByHost
After reading the list, you decide you want to save this list to a file. You can run the following:
!! > list.txt
Terminal replaces the !! shortcut with the last command you ran, so !! > list.txt
is the same as typing this command:
ls ~/Library/Preferences/ByHost > list.txt
Edit the commands
In addition to moving up and down the command history by pressing the up- and down-arrow keys, displaying each command at the prompt in Terminal, you can also edit the commands that appear or add arguments to them. This saves time when you need to run a command that is very similar but not exactly the same.
Say you want to copy a file; you type a command like this:
cp /Users/kirk/Pictures/P101068.jpg /Users/Shared
Terminal says the file doesn’t exist, but you’re sure it does. When you go to the directory and check, you see that there is indeed a file with a similar name, but you left out a zero. Using the command history, you can press the up arrow to display the command again. Use the left arrow to move the cursor to the location where you need to add the zero, type 0
and then press enter. (You don’t need to move the cursor back to the end of the line.)
You can use command editing to change commands and run them on different files. For example, if you have several files in your Pictures directory, you can use the up arrow to redisplay the previous command, change the file name, and run the command again on a different file.
Change arguments and options
Use the same trick to change a command’s arguments or options. In the previous example, I copied pictures into my Users/Shared directory. I could easily press the up arrow to redisplay the command and then alter the command to copy one of the files to a different location. There’s no need to retype the entire line.
By the same token, say you list the files in a directory and then decide you want to use the -l
option to display a long list. Press the up arrow to redisplay the command, and then move the cursor to the left to add -l
. For example, ls -l /etc/periodic
.
Oops, I forgot sudo
There are many commands and areas of your computer you can’t access without root user privileges. If you have administrator rights for your computer, you can use the sudo
command to prompt you for a password.
But it’s annoying to type a long command only to have Terminal dourly reply “Permission denied.” If you forget to prefix a command with sudo
, just type sudo !!
. This command tells the shell to execute the previous command again, this time prefaced by sudo
. Enter your password at the prompt and then press enter, and the command will run. There’s no need to type it all over again.
Editor’s note: This article is an excerpt adapted from The Mac OS X Command Line: Unix Under the Hood, by Kirk McElhearn (2004; reprinted by permission of Sybex).
For all the latest Technology News Click Here
For the latest news and updates, follow us on Google News.