Selecting a whole line of text, including the newline

codakeyboardtext-editortext;textedit

Kind of a tricky one to describe. Here goes though. Open up your favourite text editor on OSX (well, Textedit, Coda 2, a Chrome textarea at least). Type a long line of text, followed by a short line. Place your cursor at the end, like this:

I am a long, long, line of text.
I am short.|

If I want to delete the line "I am short" and the line break, in Windows I'd Shift+Up, then Ctrl+Shift+End. (Similarly, Ctrl+Shift+Home would select both lines in their entirety.)

On OSX, I can Shift+Up to select the second half of the first line fine, but Cmd+Shift+Right doesn't do anything. Cmd+Shift+Left does, however, select both lines like on Windows. You can repeat the same behaviour with using Ctrl+a/e for home/end.

Why doesn't Cmd+Shift+Right work? Can I make it work? Is there a similar command which would work?

I know I can do Cmd+Shift+Left, Shift+Left and it achieves the same thing with the same number of pushes, but Shift+Up etc is pretty ingrained in my muscle memory. I still use a Windows PC on a regular basis too, so would prefer to use the same general method, and Ctrl+Shift+Home, Shift+Left on a PC is a bit more cumbersome as Home and Left aren't in the same block of keys.

Best Answer

What if you want to select 78 here (after having pressed ⇧↑)?

Or if you want to select both 12 and 78, on Windows you couldn't press shift+home and shift+end.

Methods like moveToEndOfLineAndModifySelection: are unanchored in most applications, which means that they always extend selections. They are actually anchored in Xcode, TextMate, Sublime Text, and BBEdit, but I don't know any way to change the default behavior.

If you just want to make selecting and deleting lines easier, you can create ~/Library/KeyBindings/ and save a property list like this as DefaultKeyBinding.dict:

{
    "~l" = selectParagraph:;
    "~z" = (selectParagraph:, deleteBackward:);
    "~x" = (selectParagraph:, cut:);
    "~c" = (selectParagraph:, copy:);
}

After reopening applications, ⌥L should select a line. See http://www.hcs.harvard.edu/~jrus/site/cocoa-text.html and http://lri.me/keybindings.html for more information.