How do I select a line on OSX?

23,436

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)

Share:
23,436

Related videos on Youtube

Dávid Natingga
Author by

Dávid Natingga

Updated on September 18, 2022

Comments

  • Dávid Natingga
    Dávid Natingga over 1 year

    I am editing text. A cumbersome way to select a line would be to go to the beginning of the line (cmd <-) and then select the whole line to the end (Shift cmd ->). Can I do better?

    Also shortcuts for cutting the line and storing it in a buffer would be welcomed. (cmd D) does not store the line in a buffer.

    • ForeverWintr
      ForeverWintr about 11 years
      What program are you editing text in?
    • Dávid Natingga
      Dávid Natingga about 11 years
      Mostly in Eclipse in then terminal, but I was hoping to get a general solution for every application if possible.
    • sdjuan
      sdjuan about 11 years
      on a ful keyboard in os x home then shift-end will select the whole line, but you probably don't have a full keyboard. Also in most apps positioning the cursor at the start of the line then holding shift pressing the down arrow works.
  • brevno
    brevno about 11 years
    OS X text views don't use readline, they just happen to support many of the same keybindings as readline in emacs mode. C-u, C-@, and C-w shouldn't work in vanilla text views by default, even though you can define them in DefaultKeyBinding.dict.
  • Nevin Williams
    Nevin Williams about 11 years
    I tested everything, before posting... except I did it at a command prompt. doh.
  • Louis Waweru
    Louis Waweru about 10 years
    This is nice but I'm still looking for something that works globally.
  • Kevin Le - Khnle
    Kevin Le - Khnle almost 9 years
    Almost right: shift + cmd - right arrow to mark the line, NOT shift + cmd - down arrow.
  • heiglandreas
    heiglandreas almost 9 years
    shift + cmd + down arrow marks the current line icluding the newline char, shift + cmd + right arrow marks the line excluding the newline char. So we're both right ;)
  • C0deDaedalus
    C0deDaedalus about 6 years
    No, @heiglandreas mentioned (not explicitely, but he did) it in his answer. See the second bullet.
  • Elhem Enohpi
    Elhem Enohpi about 2 years
    The question asks for a better alternative to the cumbersome command + left-arrow then shift + command + right-arrow. How is it a good answer to use exactly that? PS, heiglandreas, I don't know for 2015 you, but in 2022, shift + command + down-arrow marks to the end of the document, not line.