Navigating the Terminal Prompt

I have talked a lot about the Terminal recently. One the most basic elements of a command shell is the prompt line, where you enter the command. Sometimes you have to move the cursor around the prompt line and there are more efficient ways of doing this than hitting the left arrow multiple times.

These key commands are for the default Terminal configuration with the bash shell.

Recalling previous commands

Often you want to retry or re-use a previous command. Hitting the up arrow will recall the previous command, leaving the cursor at the end of the line, so you can either hit return to execute the command or edit it. You can hit the up arrow multiple times to go back further in your command history. You can also hit the down arrow to move forward again.

Instead of hitting the up arrow several times, you can also hit ctrl-R and start typing a command you used before. This will search through the history backwards and recall the latest command you used starting with what you typed. So ctrl-R and then typing cd will recall the last cd command you typed. When you get ‘stuck’ in the search mode you can leave it with ctrl-G.

Moving the cursor

Once you have recalled (or typed) a command and want to edit it, you will have to move the cursor.

The left and right arrows move the cursor left and right. You can use (option) ⌥-left (or right) to move word by word. You can use ctrl-A to jump to the beginning of the line and ctrl-E to jump to the end.

ctrl-XX (hold ctrl and press ‘x’ twice) will jump to the beginning of the line and then back to the current position the second time you use it.

If you recalled a wrong command, then you can clear the entire part left of the cursor with ctrl-U and the part right of the cursor with ctrl-K. There does not seem to be a keystroke that just clears the entire current line, but most of the time your cursor will be at the end of the line, so ctrl-U will do the trick.

And finally, you can option-click with the mouse pointer on a character in the command line to move the cursor there.

Selecting text

When you hold the option key, the mouse pointer will turn from the text insertion pointer to a cross. This is because the option key also switches text selection to rectangular. This can be useful when copying text from the table like output like ls -l or df.

As in any Mac application, you can double-click to select a word in the Terminal. However, for macOS a ‘word’ ends at special characters, including the /. This is annoying when you want to select a file path. If you double-click while holding command-shift Terminal will select the entire file path. This works for URLs, too.

Usually, you select some text to copy and paste it into the command prompt. You can take a short cut here, too. Terminal has a ‘Paste Selection’ command in the ‘Edit’ menu (command-shift-V). This way you save the ‘copy’ step.

And (as a recap from the post on marks) command-shift-A will select the output of the previous command.

Tab completes me

When you are typing commands, file names or paths, then you can use the tab key ⇥ to save keystrokes and typos.

For example you can type cd ~/Doc⇥ and it will complete to cd ~/Documents/. You can hit tab over and over at different parts of the command:

$ cd ~/Li⇥
$ cd ~/Library/Appl⇥
$ cd ~/Library/Application\ Su⇥
$ cd ~/Library/Application\ Support/

When there are multiple options to complete, bash will play an alarm sound, if you then press tab for the second time, it will list all options:

$ cd ~/D⇥<beep>⇥
Desktop/   Documents/ Downloads/

Using tab-completion not only saves keystrokes, and time, but also reduces the potential for typos and errors. Tab-completion will also automatically escape problematic characters such as spaces:

$ cd /Library/Appl⇥
$ cd /Library/Application\ Support/

Note: usually bash is case-sensitive. However, since the macOS filesystem (HFS+) is (for now) case-insensitive, you may want to switch tab-completion to be case-insensitive, too. The case-sensitivity of the file system may change with the upcoming APFS file system.

There is another form of completion which you invoke with pressing the esc(ape) key twice. This will replace bash ‘globbing’ characters such as ?, * and ~. For example:

$ cd ~/Doc*[esc][esc]
$ cd /Users/armin/Documents

Note that this replaces the ~ and the *. When there are multiple possible expansions, there will be an alert sound. You can press esc twice again to see all possible expansions:

$ cd ~/D*[esc][esc]<beep>[esc][esc]
Desktop/   Documents/ Downloads/

Third-party installer packages may not be installable by the macOS 10.12.4 OS installer

Rich Trouton has found that a custom NetInstall set with your own packages will not install the custom packages. Only Apple’s own packages will install. Even when you sign your own packages with an Apple Developer certificate they will not install.

This also affects custom packages added to COSXIP (Create OS X Installer Package).

AutoDMG uses a different workflow and can still add third party packages.

It is unclear whether this is unintentional or not. However, Mac Admins will have to make plans for deployment workflows without custom imaging or system installation.

Source: Third-party installer packages may not be installable by the macOS 10.12.4 OS installer | Der Flounder

New options for macOS Recovery

Mike Boylan on Twitter points to this new Apple Support article on re-installing your Mac. The option for restoring from internet recovery have changed:

Reinstall the latest macOS that was installed on your Mac, without upgrading to a later version.1
Upgrade to the latest macOS that is compatible with your Mac.2
Requires macOS Sierra 10.12.4 or later. Reinstall the macOS that came with your Mac, or the version closest to it that is still available.

News Summary: Week of 2017-03-24

Back when I was a Server Sales Engineer I would make a weekly summary for other Field Engineers and customers on server and admin related news.

Thought I might try this tradition, again. Welcome to the first Scripting OS X Weekly News roundup!

On this Blog

I published my second book ‘Property Lists, Preferences and Profiles for Apple Adminstrators’! (PR3 for short) Get the details in the pre-sales announcement and download the book here!

I also posted an update to the Packaging book last week. It got a new cover to fit with PR3 and some internal changes and corrections. I also added an extra page with the version history for all the update details. If you have purchased the book already, iBooks should have notified you of the update. If you have been ignoring that red batch on the iBooks app, go check it out!

If you have the books and like them, please leave a review on iBooks store!

And then I continued with the articles on the Terminal and command line with a new post: [Terminal – the ’’ marks the spot.

If I missed anything, let me know in the comments, twitter or the MacAdmin Slack forum!

Magnet – manage windows on your desktop

This blog may focus on client management, but this is the other kind of windows management…

Magnet is a neat app that will help you resize windows to certain fractions of the screen: half, quarter or third size. You just drag a window to the edge of screen and Magnet will suggest a size to resize it to. Very useful if you want to tile multiple terminal windows or split the screen among more than two applications.

It is on sale right now for only US$0.99, 80% off the normal price.

Terminal: The ‘[‘ Marks the Spot

Using Terminal on macOS you may at some point have wondered about the small gray square brackets before the prompt. (There is also a counterpart closing bracket ‘]’ at the right side of Terminal window.)

Marks in Terminal

They appeared first in Terminal in El Capitan (10.11). They are called ‘Marks’ and simplify scrolling through the output in the Terminal window.

By default, every command that is executed automatically gets marked.

Jump to the Mark

You can quickly scroll the Terminal output to previous marks (i.e. command prompts) with ⌘↑ (Command-Up Arrow) and to next mark with ⌘↓ (Command Down-Arrow). This allow you to quickly skip through the command prompts in your window.

Select Output

You can quickly select output between Marks. ⌘⇧↑ (Command-Shift-Up) will select the output up to, but not including the previous command prompt. ⌘⇧↓ (Command-Shift-Down) will select the output up to, but not including the next command prompt. When you repeatedly hit this key combo the selection will be extended further, including the intermittent command prompts.

⌘⇧A (Command-Shift-A) or ‘Select Between Marks’ from the ‘Edit’ menu, will select to the previous mark, if you are the last (empty) command prompt, otherwise it will select to the next mark.

Clearing Output

You can also just remove the output of the previous command with ‘Clear to Previous Mark’ from the ‘Edit’ menu (⌘L). This is less destructive than clearing the entire screen or buffer.

Marks and Bookmarks

Marks are very useful. However, they are all the same, so jumping back to a specific mark in your Terminal scroll buffer will get harder and harder you have to hit ⌘↑ more and more often.

You can elevate a mark to a bookmark, which will allow you to jump directly to it. Jump to the line you want to bookmark or select some text and choose ‘Mark as Bookmark’ from the ‘Edit’ menu (⌘⌥U) or context menu.

Bookmarks have a bolder vertical bar ‘|’ at the left and right edge of the window.

You can jump to previous and next bookmarks with ⌘⌥↑ and ⌘⌥↓, or you jump directly to a bookmark from the list in the ‘Edit’ > ‘Bookmark’ menu.

The ‘Edit’ > ‘Bookmarks’ menu also has ‘Insert Bookmark’ and ‘Insert Bookmark with Name…’ items. These will mark the current command prompt as a bookmark, and give you the option of naming the bookmark, rather than having the default timestamp as the name.

Terminal will set an automatic bookmark when restoring a Terminal window (after a reboot). This setting is controlled in the ‘Resume’ area in the ‘Window’ Tab of the Profiles area of the settings.

Not Leaving Marks

If the grey brackets indicating the marks annoy you, you can hide them with ‘Hide Marks’ from the ‘View’ menu. This will only hide the marks and bookmarks, they will still be automatically (or manually) created and you can still jump to them.

You can disable automatic mark creation entirely by deselecting ‘Automatically Mark Prompt Lines’ from the ‘Edit’ > ‘Marks’ submenu. When you have disabled automatic marking, you can execute and manually mark a prompt with ⌘↩︎ (Command-Return).

With automatic marking enabled, you can execute a command without marking with ⌘⇧↩︎ (Command-Shift-Return).

You can also manually remove a mark, by scrolling or jumping to it and choosing ‘Unmark’ from the ‘Edit’ > ‘Marks’ menu or the context menu.

Marking Spots Programatically

I have added the following aliases to my .bash_profile:

alias mark="osascript -e 'if app \"Terminal\" is frontmost then tell app \"System Events\" to keystroke \"u\" using command down'"
alias bookmark="osascript -e 'if app \"Terminal\" is frontmost then tell app \"System Events\" to keystroke \"M\" using command down'"

This way I can put the mark in between two commands and just let them run:

$ system_profiler SPApplicationsDataType; mark; system_profiler SPExtensionsDataType

The downside of this approach that the aliases do not work when Terminal is in the background.

Apple acquires Workflow app

You’ve probable heard it already: Apple has bought the Workflow application. The developers will keep working at Apple and the Workflow app is now offered for free on the AppStore, although with a few interesting changes.

Workflow has always been one of those apps that would have liked to get into. However, as an admin, my workflows center on macOS and Unix. You cannot build packages on iOS. Even iBooks Author only exists on macOS. As much as I like the iPad and iOS it has been relegated to secondary device because of the focus of my work.

Still it will be interesting to see how Apple will integrate Workflow with iOS and the Apple applications. I am looking forward to it.

If you now want to catch up with what Workflow is all about, you can’t do much better than Frederico Viticci’s articles on MacStories.

Sierra update 10.12.4: Disable iCloud Desktop and Documents Sync

Babo D: Disable iCloud Desktop and Documents Sync

Very useful discovery. When you search Apple’s profile documentation page for ‘10.12.4’ you also find a new key to disable Touch ID login on the new MacBooks and an entire new payload type for SmartCard setup.

BTW: you can of course learn more about configuration profiles in my new book: ‘Property Lists, Preferences and Profiles for Apple Administrators’ available in the iBooks Store now!