Drafts Action Directory

Select Word (Key)

Posted by Tethys, Last update 3 months ago

Select Word

A custom key action that selects the word nearest the editing caret.

Add desired shortcut for external keyboard use.

Install

Steps

  • script

    var text = editor.getText()
    var selRange = editor.getSelectedRange()
    
    var n = text.length
    
    var i = selRange[0]
    //var delim = /^[^\w]$/
    var delim = /^[\s]$/
    
    while (i > 0) {
      if (delim.test(text[i - 1]))
        break
    
      --i
    }
    
    var j = selRange[0]+selRange[1]
    
    while (j < n) {
      if (delim.test(text[j]))
        break
      ++j
    }
    
    if (j-i)
      editor.setSelectedRange(i, j-i)
    /*else if (i)
      editor.setSelectedRange(i-1, 1)
    else 
      editor.setSelectedRange(0, 1)*/

Options

  • After Success Nothing
    Notification Error
    Log Level Error

Comments

Actions available in the Action Directory are uploaded by community members. Use appropriate caution reviewing downloaded actions before use.