Quick tip for developers who use OS X

1145 points by gargarplex ↗ HN
OSX Terminal: hold option and click a position in the current line to move your cursor to that position. #yearslost

405 comments

[ 6.0 ms ] story [ 483 ms ] thread
I did not know this. Thanks! Very helpful tip.
Works in iTerm too.
Not for me. Option-clicking produces no result for me on iTerm 1.0.0.20130319.
I tested it in iTerm 2 Build 1.0.0.20131228, where it works. It must be a recent addition.
Works fine in iTerm2 1.0.0.20140112
Nope, this doesn't work in iTerm.
Which version of iTerm are you using?
The problem here is the "click" part of it.
Every once in a while it's much faster to just reach for the mouse than to use keyboard shortcuts.
Two more tips:

* Ctrl-a to go to beginning of line

* Ctrl-e to go to end of line

A lot of the Emacs shortcuts work in OS X (most of the system) as well as on Linux (bash and other users of readline).
I use Vim and was unaware that this shortcut existed. Are there any other Emacs shortcuts which are useful in OSX?
Ctrl-K kill to end of line,

so Ctrl-A Ctrl-K will clear a given field

And you can Ctrl-Y to paste the line you killed. This is a separate copy/paste buffer from the normal Cmd-C one, which can be nice.
If you do "set -o vi" you can also use vi shortcuts, such as ESC-j,k to go up/down the history, 0 to go to the beginning of the line, etc.
You cannot use 'set -o vi' to turn Cocoa text fields into vi-mode.
(comment deleted)
The basic Emacs key-bindings should work in all Cocoa apps. For instance, the URL bar of your web browser, while text editing in just about any editor (maybe even in Office, but I have not tired in a while), etc. Basically, anywhere you have a cursor, the basic line-editing key bindings should work.

So, in practical terms, this means you have 1 + n clipboards[1]. The main global GUI clipboard (using the command key), and then an app-specific clipboard (using the control key with Emacs bindings). As a developer, it is extremely helpful to have two clipboards in TextMate, for instance.

[1] Technically, the clipboard in OS X maintains a history (not sure what the limit is), but the OS X GUI only exposes the last cut/copied item.

A couple more:

* ⌘ + ` (tilde key) to switch between different windows of your current application

* ⌘ + l (lowercase L) in most web browsers to highlight the address bar

I'm always telling people about ⌘ + `, it makes navigating windows on OSX so easy.
The former is the one thing I missed the most from my OSX days...until I got a tiling WW setup (i3).

Woohoo, love me some Linux, let choice reign ;-)

to piggyback off of this... (not sure if this is in the thread already)

⌘ + ` to switch between different windows of current application

⌘ + SHIFT + ` to switch between different windows of current application (reverse order)

same for the general application switcher:

⌘ + TAB to switch between different windows of current application

⌘ + SHIFT + TAB to switch between different windows of current application (reverse order)

This was life changing when a co-worker showed me this last year.
Because crappy Macs have no Home/End keys (or they don't work as such).
ctrl-arrow (which breaks with Lion's Spaces default keys), or shift Home/End in Terminal.

It's not that Macs are crappy, they just have different key bindings. I regularly work with Macs, Windows, and Linux for development and I've just learned to use the different key mappings.

I don't think I'm quite as proficient in any one system because of that, though. For example, I have avoided learning the Emacs key bindings for use in Mac/Terminal. (I don't want to try to remember TWO bindings for Mac, plus I learned vim first so usually avoid Emacs.)

ctrl-arrow jumps to the next word boundary under Linux.

Yes, other systems have other key bindings, but why do common things like jumping to the begin/end of a line need a key combination? And why aren't the characters written onto the keys? Every time I have to type some code on a Mac I have to press all the keys in order to find { } [ ] etc. At leas I learned not to quit the terminal when I want to enter a @ by now. Whose brilliant idea was that?

On most macs, Fn + left/right arrow are home/end. Fn + up/down are pageup/pagedown.
Fn... urgh. I hate these crippled laptop keyboards (and tiny neck pain inducing laptop monitors).
Well, you don't have to follow that glossy mainstream. Just get yourself a new linux desktop with lots of RAM, couple of nice matte monitors, mouse and a good full size keyboard.
That's what I always do. My new PC should be delivered any minute, actually (16 GB RAM; at one intensive work session my current 8 GB where almost a bit scantly - is that the right English word?).
Not sure if I have it set up differently but mine is Cmd-Left or Cmd-Right to move to the beginning of a line...? Easier to reach than fn (I'm using a real wired BIG Apple keyboard, the RSI inducing one...)
I love how osx supports emacs key-bindings by default for any text field.

I miss this feature a lot when I switch back to windows. I find myself pressing ctrl-n in my browser's address bar, expecting to view my URL history, and instead I get a new windows.

If anyone knows of a way to get similar functionality in windows, I would love to hear about it!

Cltr + D to focus on address bar. Alt + down arrow to view history.
Most useful thing I've seen all day - thanks!
Works even when you are ssh-ed into a remote machine! Nice.
yep. I tried it myself thinking it wouldn't. wow.
... Why didn't I know this before?

Thanks for sharing!

Well, if you do not want to use the mouse, you can always:

Ctrl-e: go to the end of the line Ctrl-r: search backwards (input the text)

and you go to the point where the search begins.

Nother related tip: command + click on a file in iTerm opens it up. Test it out by doing ls -al then cmd+click on file name.
Type "set -o vi" on your bash, and you can edit your commands with the vi shortcuts.
OK, so for vim users with zsh:

bindkey -v

Now be happy, hit <ESC> and, use ^ and $ but also f<letter> t<letter> and also ,;.

You can copy/paste with dd, Y, etc...

Fyi, bash also has vi-mode (set -o vi).
bash uses GNU readline, while ZSH has it's own readline. IIRC the Emacs-/Vi-modes in bash are support from readline. (this is also why you configure some of these things via .inputrc for Bash rather than .bashrc).
set -o vi does the trick for vim. Most vi commands work, but when you type <ESC>v this will open vi to edit your command line, in case you feel any command is missing.
I've b een using vi-mode in the shell for a while,but haven't been able to get the <ESC>v behavior you described. Did you have to do anything to make that behave?
I haven't done anything, my version works under Mac OS X.
Osx terminal uses almost all of the emacs movement keys by default. Ctrl-a and ctrl-e are useful but I find the meta movement keys to be the most helpful. (usually alt or ESC)

Meta-f jump forward a word

Meta-b jump backwards a word

I'm not sure what you're suggesting here.

It's up to the process running in Terminal.app to support the key sequences sent to it. I doubt that M-f would work if your shell (bash, zsh, etc) were in Vi-mode rather than the default of Emacs-mode.

It's not emacs, it's GNU Readline (or likely some BSD readline given apple and the GPL) http://cnswww.cns.cwru.edu/php/chet/readline/rltop.html.

The shortcuts also work in every text field in the OS.

IIRC, someone mentioned to me once that bash doesn't link against libreadline, but uses libreadline (the source is directly in the Bash repo), so it's possible that it's using GNU readline, while Apple's libreadline is a BSD flavour.
Terminal may be using some kind of Readline, I dunno. But text objects in other OS X applications use emacs keybindings because NeXTSTEP has had them since time immemorial. Like 1990 or so. Doesn't have anything to do with GNU Readline so far as I know.
It's not Terminal that uses it; it's bash, and most shells that imitate bash.
> Terminal may be using some kind of Readline

Terminal.app passes the key sequences to the application running inside of the terminal. The default shell (bash) uses GNU readline, which has an emacs-mode and a vi-mode (emacs is the default).

Terminal.app does not use any Cocoa text fields, so has nothing to do with the Emacs keybindings inherited from NeXTSTEP.

It at least used to be the case that GNU Readline was GPL-licensed, instead of LGPL, as to make readline a "carrot" to encourage GPL adoption. That shouldn't be an issue for bash, which is also GPL licensed.
That's bash. Works on all platforms, all terminals, as long as bash has been compiled with readline.
A new one for me I accidentally found the other day..

If you use Spotlight to find something and then want to see the file in Finder, Cmd+click the item in the Spotlight dropdown.

Edited from Cmd+shift+click due to note in child comment :-)

isn't it just Cmd+click ?
In Mavricks (only one I can be sure of) when you hover over spotlight results and hold Cmd, you get a (quite nice) file preview/info.
(comment deleted)
Under Konsole/Yakuake (KDE) its: Contextmenu -> Open Link
You can also use Cmd + Return.
Cmd + R is universally "Reveal in Finder".
(comment deleted)
First thing I install on a Mac is Alfred. Check it out. `space` followed by name searches directories and files.
Useful when copy/pasting things to share with other people in email and such:

    $ pbpaste | pbcopy
takes the current contents of the copy/paste buffer and removes color/font/background color rich formatting and puts just plain text back into the paste buffer.

And of course pbcopy and pbpaste are also very useful on their own.

You can also use shift-option-cmd-v to directly paste without markup :)
I can never remember which combination that is, but thanks.
You can override any key combination using the Keyboard preferences.

Just make a new shortcut under the All Applications group named exactly the same as the one you want to replace. http://i.imgur.com/JqBncTy.png

I've also had to add an entry for "Paste as Quotation" because that's Mail's default use of Command-V.

Be warned, though, that these do not work inside of a tmux session without special steps being taken. I believe that they work in screen, but tmux requires some additional work.
This command has immediately become an alias in my .profile.
Quite a lot of OS X applications support pasting with matching style (Cmd+Shift+V), which does exactly the same thing.
Actually, natch's trick does just the opposite. It strips out any formatting information that may accompany the text on the clipboard.

I've used the same trick on occasion when copying text from web pages into Gmail. When you paste such text into a Gmail draft, Gmail tries to duplicate the format of the original, which is often not what you want. pbpaste | pbcopy solves the problem nicely.

Edit: But Shift-Option-Cmd-V is easier. Thanks, Janteh!

(comment deleted)
I like checking the box in preferences to use Option as Meta, and then option + left/right will jump a word at a time, and option + backspace will delete the previous word, just like in most Cocoa apps.
Lovely, thanks. Also, is it common knowledge that CMD+click on a URL opens that URL in your default browser? Useful for the "Running on http://127.0.0.1:5000/" messages.
Nice tip, thanks. However, it only works with double-click for me.
That's in iTerm, I think. In Terminal.app, it's CMD+double-click.
Tip: Real hackers don't use a mouse.

/s

... so do they prefer a track ball or TrackPoint (or some sort of Minority Report gestures-based system)?

/s

Perhaps it's simply personal preference, but I do find that limiting my mouse usage has resulted in real, tangible gains. I don't know how feasible that is in GUI-first operating systems like OS X, though.
I wish there was a way to make the mouse cursor stand out more when (and only when) it's over the terminal. Maybe it's because I use a dark terminal background, I can hardly ever see the thing.

There's a system wide setting to make the cursor huge, but I don't want a huge cursor everywhere. And there's a terminal preference to make the insertion cursor different, but that's a different cursor, not the mouse cursor.

When do I want to see the mouse cursor? It's useful for things like highlighting a git hash or a few lines of text for copy/paste.

<Ctrl> X, <Ctrl> E to edit the current line in your default editor.