Use the following keys to do fun things when typing a command in bash (mostly keybindings):
krypted March 21st, 2016
The cd command has lots of fun little shortcuts. One I use frequently is the -. The ~ always takes you to your home directory, but using cd – will take you to the last directory you were in. For example, if you do the following on a Mac:
Then you do .. (which is a shortcut for the directory above the one you’re in):
Then pwd will show that you’re in /Users. But, if you cd to – again:
Now you’re back in your home folder. The – expands to OLDPWD. Quick tip. Nothing more to see here.
krypted July 20th, 2015
Sometimes when we’re doing work, we end up changing an SSH key. You then access the host using something like this:
When accessing the host you then get a warning similar to the following if the key changed:
@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that a host key has just been changed.
The fingerprint for the ECDSA key sent by the remote host is
Please contact your system administrator.
Add correct host key in /home/remi/.ssh/known_hosts to get rid of this message.
Offending ECDSA key in /home/remi/.ssh/known_hosts:1
In case you know the host identification has changed, you can safely discard this warning.
To fix you can edit the ~/.ssh/known_hosts file and remove the offending line (usually the number in the Offending ECDSA line) or just regenerate the key using ssh-keygen, which will automatically edit the known_hosts file for you:
ssh-keygen -R 10.10.10.10
krypted September 12th, 2013
Not, this article is not about 1984. Nor do I believe there is anything but a revisionist history. Instead, this article is about the history command in OS X (and *nix). The history command is a funny beast. Viewing the manual page for history in OS X nets you a whole lotta’ nothin’ because it’s just going to show you the standard BSD General Commands Manual. But there’s a lot more there than most people use. Let’s take the simplest invocation of the history command. Simply run the command with no options and you’ll get a list of your previously run bash commands:
This would output something that looks like the following:
3 cd ~/Desktop
4 cat asciipr0n
Now, you can clear all of this out in one of a few different ways. The first is to delete the .bash_history (or the history file of whatever shell you like). This would leave you with an interesting line in the resultant history:
l rm ~/.bash_history
Probably not what you were after. Another option would be to nuke the whole history file (as I did on a host accidentally with a misconstrued variable expansion, trying to write a history into a script):
A less nuke and pave method here, would be to selectively rewrite history, by choosing a line that you’d like to remove using the -d option:
history -d 4
Three are other options for history as well, mostly dealing with substitutions, but I am clearly not the person to be writing about these given that I just took ’em the wrong direction. They are -anrwps for future reference.
Finally, since you likely want a clean screen, do clear to get a nice clean screen:
Now that we’re finished discussing altering your history, let’s look at using it to make your life faster. One of my most commonly tools at the command line is to use !$. !$ in any command expands to be the last position of your last run command. Take as an example you want to check the permissions of a file on the desktop:
ls -al ~/Desktop/asciipr0n
Now let’s say you want to change the permissions of that object, just use !$ since the last command had it as that only positional parameter and viola:
chmod 700 !$
Where does this come from? Well, any time you use a ! in a command, you are doing a history substitution, or expanding that variable into some kind of event designator, which is the part in your history you’re calling up. The $ is designated as first position (which yes, is a move my daughter did at her last dance recital). !# is another of these, which calls up the whole line typed so far. For example, let’s say you have a file called cat. Well, if you run cat and then use !# (provided you’re in the working directory of said file) you’d show the contents on the screen:
Now, view your history after running a couple of these and you’ll notice that the event designators aren’t displayed in your history. Instead, they were expanded at runtime and are therefore displayed as the expanded expression. Let’s do something a tad more complicated. Let’s echo out the contents of your line 4 command from the beginning of this article:
Now, if your line 4 were the same as my line 4 you’d certainly be disappointed. You see, you lost the formatting, so it’s probably not gonna’ look that pretty. If you were on line 11 and you wanted to do that same thing, you could just run !-7 and you’d go 7 lines back:
But the output would still be all jacked. Now, let’s say you ran a command and realized that jeez you forgot to sudo first. Well, !! is here for ya’. Simply run sudo !! and it will expand your last command right after the sudo:
The ! designator also allows you to grab the most recent command that starts with a set of letters. for example, let’s say I wanted to output the contents of my earlier echo command, and I wanted to show just the second position there:
That’s actually not gonna’ look pretty either. But that’s aside from the point. There are other designators and even modifiers to the designators as well, which allow for substitution. But again, I’m gonna’ have to go back and review my skills with those as I wouldn’t want to have you accidentally nuking your history because you expanded -c into some expression and didn’t realize that some of these will actually leave the history command as your last run command… :-/
krypted May 27th, 2013
One of the new features that was introduced in OS X Lion is automatic application termination. This feature stops any applications that haven’t been used for awhile and then when you start the app back up, it fires up using the saved application state. But some processes shouldn’t be stopped. I’ve recently run into 2 cases where I needed to disable automatic termination. To do so is pretty straight forward:
defaults write -g NSDisableAutomaticTermination -bool TRUE
Once run, read the key back from the global defaults domain to verify it was run correctly:
defaults read -g NSDisableAutomaticTermination
The output should just be a 1. Provided it’s correct, now test that applications no longer automatically terminate. Provided they don’t then the setting can be left, or if your issue persists then you can just delete the key:
defaults delete NSDisableAutomaticTermination
While you shouldn’t have to restart for changes to activate, I do. Basically, at the relaunch of the app the changes should be active. Applications with network sockets should never stop taking resources; however, applications without network sockets that might call on them from time to time could be impacted. You can also look at doing per-application, as an example with Terminal:
defaults write com.apple.terminal NSDisableAutomaticTermination -bool true
Or Apple Configurator:
defaults write com.apple.terminal NSDisableAutomaticTermination -bool true
krypted June 3rd, 2012
Terminal is a great application. And we usually use Terminal for editing scripts and invoking things. But what about invoking Terminal from, well, Terminal. For starters, let’s look at opening a Terminal session to the root of the boot volume (aka /):
open -a Terminal /
The -a option, when used with the open command, allows you to define which application that the item defined in the following position will open in. For example, you could open an XML file in Xcode
open -a Xcode /usr/share/postgresql/pg_hba.conf.sample
You could then open Terminal by passing other commands into the command. For example, to open a new Terminal window to the current working directory:
open -a Terminal `pwd`
Of course, you could accomplish the same thing with:
open -a Terminal .
Or pass the output of other commands through the open command. For example, the following command opens a new file in TextEdit that contains the output of an ls command:
ls | open -f
Adding -g to any of this leaves the new window in the background rather than bringing it to the foreground, which is the default behavior. Finally, open can also be used to open URLs, but I’ve covered that sort of use for open in the past.
krypted December 21st, 2011
Posted In: Mac OS X
Terminal screens can use backgrounds in OS X Lion. To configure these settings, open Terminal and choose Preferences from the Terminal menu. Then click on the Window tab.
Use the Image drop down to select Choose. This brings up a browse dialog box that you can use to choose an image. Browse to the image and then click on Open.
Choose images that are pretty much all dark or all light as your font should be the opposite color.
krypted August 9th, 2011
Posted In: Mac OS X
The Terminal application defaults have a problem passing colors with Ubuntu and other types of Linux machines with properly formed .bashrc files. This is because those systems do not know how to interpret the Lion xterm-color256 terminal declaration.
The fix is to change this setting to xterm-color. This needs to be done for each Terminal default. Click on each (Basic, Grass, Homebrew, etc) and then click on the Advanced tab. From there, just set the Declare terminal as: to xterm-color and close.
This can also be done through the command line. These settings are stored in the com.apple.Terminal.plist per user, in their ~/Library/Preferences. The key for each is in TerminalType, which should read:
TerminalType = "xterm-color";
This small issue pales in comparison (especially because it is easily remedied) to how great the ability to take Terminal into full screen mode, have it state fully open and show the last run commands, assign background images and other little cool tricks are. There are also lots of new commands in Lion, such as tmutil that you’re sure to love if you haven’t made the upgrade yet!
krypted August 7th, 2011
Posted In: Mac OS X
Command-Option-F will send terminal into full screen in OS X Lion (or most any other app for that matter). You can also use the double-arrow button in the top right corner of an application’s title bar to make it full screen.
Command-Option-F (or switching to another app or Window that isn’t full screen) will end your full screen session. For any app, you can have one window that is full screen and others that aren’t full screen. Mission Control then shows all the full screen apps at the top of the screen and those that aren’t full screen towards the bottom. I thought I really needed multiple Terminal windows, but using the screen I haven’t found that I really notice not having two windows. And I have been trying hard to multi-task less in life. Seems like Apple supports such a change in workflow…
krypted July 22nd, 2011
Fast User Switching, when enabled, allows users to leave one session open and hop to another user account. Great for training, testing and impressing friends (ok, so maybe it won’t impress your friends, but the thumb trick is getting old). To enable Fast User Switching, open the Accounts System Preference pane and click on Login Options. Then check the box for Show fast user switching menu. By default you’ll then see your user name in the menu bar.
To do this from the command line:
defaults write /Library/Preferences/.GlobalPreferences MultipleSessionEnabled -bool 'YES'
To then disable it from the command line:
defaults write /Library/Preferences/.GlobalPreferences MultipleSessionEnabled -bool 'NO'
What’s really cool though, is once enabled, you can switch users with a script as well, using the command line options available with CGSession, located in the user.menu item at /System/Library/CoreServices/Menu Extras/User.menu/Contents/Resources/CGSession.
/System/Library/CoreServices/Menu Extras/User.menu/Contents/Resources/CGSession -switchToUserID 501
Or to simply go to a login screen:
/System/Library/CoreServices/Menu Extras/User.menu/Contents/Resources/CGSession -suspend
krypted June 7th, 2011