Drafts Action Directory

Move cursor back by word

Posted by agiletortoise, Last update 4 months ago

Move cursor position back one word at a time.

Install

Steps

  • script

    // move cursor to last word break
    let [st, len] = editor.getSelectedRange();
    
    let isWhitespace = (c) => {
    	return /\s/.test(c)
    }
    
    let f = () => {
    	let newSt = st;
    	let s = draft.content;
    	let skipSpace = false;
    
    	if (newSt == 0) { return; }
    	if (isWhitespace(s.charAt(newSt - 1))) { skipSpace = true; }
    
    	while (newSt > 0) {
    		newSt -= 1;
    		if (isWhitespace(s.charAt(newSt))) {
    			if (!skipSpace) {
    				newSt += 1;
    				break;
    			}
    		}
    		else {
    			if (skipSpace) {
    				skipSpace = false;
    			}
    		}
    	}
    	editor.setSelectedRange(newSt, 0);
    }
    
    f();
    

Options

  • After Success Nothing
    Notification Error
    Log Level None

Comments

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