How do I select a line on OSX?

Solution 1:

I always use the following:

  • cmd - Left arrow to get to the beginning of the line
  • shift + cmd + right arrow to mark the line
  • cmd - C (or cmd - X) to copy (or cut) the marked line into the clipboard

Then I can go wherever I like and paste the line with cmd - v.

But as the MacOS is strongly fixed to a Graphical UserInterface using it without a mouse is possible but sometimes - like in your case - involves one keypress more than one would like.

Solution 2:

Most apps (Terminal, TextEdit, Safari's URL/search bar, etc) that accept text input honor standard key bindings (not sure what standard... ANSI, ASCII?) that include

control + a: beginning of line

control + e: end of line

So, control + shift + a or e to select a line depending on your position.

Also:

control + shift + k: kill to end-of-line

control + shift + y: yank (from buffer)

A complete list can be generated with the bindkey command in Terminal, though not all of them work across all apps.

Solution 3:

You can create ~/Library/KeyBindings/ and save a property list like this as DefaultKeyBinding.dict:

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

After reopening applications, for example ⌥L should select a line in most native text views. It doesn't work in Eclipse, Terminal, or Xcode though. For more information, see the Cocoa Text System article or my website.

Another option is to use KeyRemap4MacBook and save something like this as private.xml:

<?xml version="1.0"?>
<root>
<item>
<name>test</name>
<identifier>test</identifier>
<not>TERMINAL</not>
<not>EMACS</not>
<not>VIRTUALMACHINE</not>
<not>ECLIPSE</not>
<autogen>__KeyToKey__ KeyCode::L, VK_OPTION | ModifierFlag::NONE, KeyCode::A, ModifierFlag::CONTROL_L, KeyCode::E, ModifierFlag::CONTROL_L | ModifierFlag::SHIFT_L, KeyCode::CURSOR_RIGHT, ModifierFlag::SHIFT_L</autogen>
</item>
<item>
<name>test2</name>
<identifier>test2</identifier>
<only>ECLIPSE</only>
<autogen>__KeyToKey__ KeyCode::L, VK_OPTION | ModifierFlag::NONE, KeyCode::CURSOR_LEFT, ModifierFlag::COMMAND_L, KeyCode::CURSOR_RIGHT, ModifierFlag::COMMAND_L | ModifierFlag::SHIFT_L, KeyCode::CURSOR_RIGHT, ModifierFlag::SHIFT_L</autogen>
</item>
</root>

In most applications ⌃A moves to the start of an unwrapped line and ⌘← moves to the start of a wrapped line, but ⌃A doesn't seem to work in Eclipse.

See the source for the key code values and predefined settings.

Solution 4:

I found a solution that seems to be working across all Cocoa apps: have the home & end keys behave like non-Apple machines (go to the beginning/end of a line instead of an entire document).

A file ~/Library/KeyBindings/DefaultKeyBinding.dict needs to contain the following:

{
    /* home */
    "\UF729"  = "moveToBeginningOfLine:";
    "$\UF729" = "moveToBeginningOfLineAndModifySelection:";

    /* end */
    "\UF72B"  = "moveToEndOfLine:";
    "$\UF72B" = "moveToEndOfLineAndModifySelection:";
}

If the file or directory doesn't exist, copy the above code snippet to your...pasteboard, open Terminal and enter these two commands:

mkdir ~/Library/KeyBindings
pbpaste > ~/Library/KeyBindings/DefaultKeyBinding.dict

Restarting the app may be required for it to behave as expected.

Solution 5:

I am surprised that nobody mentioned this one

Shift + CMD + left arrow (or right arrow if you are at the beginning)