Move and select to first letter(a-zA-Z)

  • Usually, we want to move/select to the first letter(a-zA-Z), not the first character.
  • there are some prefix text
    • most for syntax: such as in markdown: header: #### or -

the title I want to change(cursor here)

  • in the above example, I want to move to the first letter and select the whole title

Try this in your init script (if you use init.coffee instead of init.js, you’ll have to translate it to CS)

atom.commands.add('atom-text-editor', {
  'move-to-first-letter': () => {
    const editor = atom.workspace.getActiveTextEditor()
    if (!editor) { return }
    const cursor = editor.getLastCursor()
    const line = editor.lineTextForBufferRow(cursor.getBufferRow())
    const match = /[a-zA-Z]/.exec(line)
    if (!match) { return }
    cursor.setBufferPosition([cursor.getBufferRow(), match.index])
  }
}

You can extend this however, such as working with selections instead to select the first word, etc. The API documentation should provide enough information on this.

1 Like

Thanks @Aerijo
I changed your code to CS and add the command to select to the first letter of line

atom.commands.add 'atom-text-editor', 'custom:move-to-first-letter': ->
    editor = atom.workspace.getActiveTextEditor()
    if !editor
      return 
    cursor = editor.getLastCursor()
    line = editor.lineTextForBufferRow(cursor.getBufferRow())
    match = /[a-zA-Z]/.exec(line)
    if !match 
      return
    cursor.setBufferPosition([cursor.getBufferRow(), match.index])

atom.commands.add 'atom-text-editor', 'custom:select-to-first-letter-of-line': ->
    editor = atom.workspace.getActiveTextEditor()
    if !editor
      return 
    cursor = editor.getLastCursor()
    selections = editor.getSelections()
    line = editor.lineTextForBufferRow(cursor.getBufferRow())
    match = /[a-zA-Z]/.exec(line)
    if !match 
      return
    firstLetterPos = [cursor.getBufferRow(), match.index]
    selections[0].setBufferRange([firstLetterPos, cursor.getBufferPosition()])
  • here is the keymap.cson
'atom-text-editor':
  'ctrl-cmd-left': 'custom:select-to-first-letter-of-line'