10

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.

3
  • 1
    What program are you editing text in? Apr 19, 2013 at 22:41
  • 1
    Mostly in Eclipse in then terminal, but I was hoping to get a general solution for every application if possible. Apr 19, 2013 at 22:50
  • 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.
    – sdjuan
    Apr 20, 2013 at 6:50

8 Answers 8

12

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.

3
  • 1
    Almost right: shift + cmd - right arrow to mark the line, NOT shift + cmd - down arrow. Aug 3, 2015 at 5:26
  • 1
    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 ;) Aug 3, 2015 at 5:33
  • 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. Feb 23, 2022 at 15:49
6

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.

3
  • 1
    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.
    – Lri
    Apr 22, 2013 at 0:36
  • I tested everything, before posting... except I did it at a command prompt. doh. Apr 22, 2013 at 2:49
  • This is nice but I'm still looking for something that works globally. Apr 16, 2014 at 17:34
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.

2

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.

1

I am surprised that nobody mentioned this one

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

1
  • No, @heiglandreas mentioned (not explicitely, but he did) it in his answer. See the second bullet. May 3, 2018 at 3:15
1

One of the advantages of macOS over(WIN/Linux) is the global emacs style keybindings in almost every text box(an exception is Microsoft Office for Mac which redefine those keybindings annoyingly...)

Regarding to this question, I always use C-a to reach head of line, C-S-e to select until end of line, then 'CMD-c` to copy the.

If you happen to have a line (which end with a \n) that crosses many literal line, I would normally do the following:

1) C-a to go to start of the line

2) C-n to go the the start of the next literal line

3) C-b to go back one character

4) C-S-a to select until head of line

5) CMD-c to copy the content

Might be a complex solution, but should work in most text boxes in macOS.

0

control + shift + aef

(type aef while holding Control and Shift)

This gives the same result as triple-clicking the current line.

Note this includes the trailing EOL in the selection. If that's not what you want, leave out the f.

As a bonus, after f, press n repeatedly to select multiple lines.

Tested in Xcode and TextEdit.

0

It depends what you mean by a "line". In TextEdit for example, long lines are automatically soft-wrapped inside the window.

To select the part of a long line where it's wrapped to a single row: Hold shift + command, press left-arrow then right-arrow.

To select an entire line, between line breaks (aka a paragraph):

  • If the cursor is at the end of the line:
    1. Optionally, to include the newline at the end of the line, press right-arrow.
    2. Press shift + option + up-arrow.
  • Otherwise:
    1. If the cursor is in the middle of the line, press shift + option + up-arrow.
    2. Press shift + option + down-arrow.
    3. Optionally, press shift + option + down-arrow again to include the newline, if the following line is blank. If not (or even if so), release option and press right-arrow.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .