Move to beginning of line


#1

In Atom (1.0.3, OS X) there is editor:move-to-end-of-screen-line (cmd-right on Mac) and editor:move-to-end-of-line (ctrl-e). The latter moves to the actual end of line in the file, the former obeys soft wrap.

For the opposite direction, there only seems to be editor:move-to-first-character-of-line. This should actually be called ...-of-screen-line, because that’s what it does.

Question: What’s the command to move the cursor to the hard beginning of line in the file, independently of soft wrap?


#2

It seems (on OS X at least) that there are 3 separate keybindings pointing to editor:move-to-first-character-of-line: cmd + left, fn + left (home) and ctrl + a. But none currently mapped to editor:move-to-beginning-of-screen-line, which is a command that does exist - it’s just doesn’t look to be connected up to anything at the moment (not sure if this is purposeful or an oversight?)

I’ve just mapped my ctrl + a keybinding to the …screen-line command and it performs just as expected.

Pressing any of the editor:move-to-first-character-of-line keybindings a second time also works to send the cursor to the beginning of the line also, though obviously its much nicer to have a one press option (and some form of symmetry to the commands).


#3

Oooh, editor:move-to-beginning-of-line actually exists (and works). I was looking in Settings -> Keybindings, and it wasn’t listed there. I think it would be good if the list of commands there was actually complete…

Next issue: editor:select-to-end-of-line only selects to the end of the screen line (same for beginning). So the naming is inconsistent here.

How do I select to the hard beginning/end of line?


#4

The list of keybindings doesn’t show all commands because it’s a list of keybindings, and not every command has a keybinding :smile: You can use the command palette (ctrl-shift-p) to search through all the commands.

Searching for select line in the command palette results in three commands: editor: select to end of line, editor: select to beginning of line and editor: select to first character of line, so it doesn’t look like there’s a select to hard end of line.

You can create your own commands, though :smile: this should give you what you want:

atom.commands.add 'atom-text-editor', 'editor:select-to-end-of-buffer-line', ->
  textEditor = atom.workspace.getActiveTextEditor()
  cursorPosition = textEditor.getCursorBufferPosition()
  endColumn = textEditor.lineTextForBufferRow(cursorPosition.row).length
  endPosition = cursorPosition.translate([0, endColumn - cursorPosition.column])
  textEditor.selectToBufferPosition(endPosition)

# I will also throw in a editor:select-to-beginning-of-buffer-line
# as that one also doesn't exist
atom.commands.add 'atom-text-editor', 'editor:select-to-beginning-of-buffer-line', ->
  textEditor = atom.workspace.getActiveTextEditor()
  cursorPosition = textEditor.getCursorBufferPosition()
  endColumn = textEditor.indentationForBufferRow(cursorPosition.row)
  if textEditor.getSoftTabs() then endColumn *= textEditor.getTabLength()
  endPosition = cursorPosition.translate([0, endColumn - cursorPosition.column])
  textEditor.selectToBufferPosition(endPosition)

Put it in your init.coffee, restart Atom and then the editor:select-to-end-of-buffer-line command should be available.


#5

Thanks for the effort, but that doesn’t work with multiple cursors…it only does it for the last-added cursor.


#6

You can loop over each cursor:

# remove: cursorPosition = textEditor.getCursorBufferPosition()
selectedRanges = for cursorPosition in textEditor.getCursorBufferPositions()
  ...
  # remove: textEditor.selectToBufferPosition(endPosition)
  [cursorPosition, endPosition]
textEditor.setSelectedBufferRanges(selectedRanges)

#7

Thanks again! Now it’s working. (for anyone reading this later: it has to be textEditor.getCursorBufferPositions())


#8

Oops, you’re right. I updated my previous post.

And here’s the final code:

atom.commands.add 'atom-text-editor', 'editor:select-to-end-of-buffer-line', ->
  textEditor = atom.workspace.getActiveTextEditor()
  selectedRanges = for cursorPosition in textEditor.getCursorBufferPositions()
    endColumn = textEditor.lineTextForBufferRow(cursorPosition.row).length
    endPosition = cursorPosition.translate([0, endColumn - cursorPosition.column])
    [ cursorPosition, endPosition ]
  textEditor.setSelectedBufferRanges(selectedRanges)

# I will also throw in a editor:select-to-beginning-of-buffer-line
# as that one also doesn't exist
atom.commands.add 'atom-text-editor', 'editor:select-to-beginning-of-buffer-line', ->
  textEditor = atom.workspace.getActiveTextEditor()
  selectedRanges = for cursorPosition in textEditor.getCursorBufferPositions()
    endColumn = textEditor.indentationForBufferRow(cursorPosition.row)
    if textEditor.getSoftTabs() then endColumn *= textEditor.getTabLength()
    endPosition = cursorPosition.translate([0, endColumn - cursorPosition.column])
    [ cursorPosition, endPosition ]
  textEditor.setSelectedBufferRanges(selectedRanges)