Move Line Up

Posted by Alexander, Last update about 1 year ago

Moves the selected line up while retaining cursor position.

Check out the complementary action Move Line Down.


  • script

    // Get info on current line
    const [current_line_position] = editor.getSelectedRange()
    const [current_line_start, current_line_length] = editor.getSelectedLineRange()
    const current_line_offset = current_line_position - current_line_start
    // Check if current line == first line of draft
    if (current_line_start > 0) {
      // Get current line's text
      let current_line_text = editor.getTextInRange(current_line_start, current_line_length)
      // Select previous line
      editor.setSelectedRange(current_line_start - 1, 0)
      // Get info on and text of previous line
      const [previous_line_start, previous_line_length] = editor.getSelectedLineRange()
      let previous_line_text = editor.getTextInRange(previous_line_start, previous_line_length)
      // If current line is last line of draft
      if (!current_line_text.endsWith('\n')) {
        // 1. Add line break to it
        current_line_text = current_line_text + '\n'
        if (previous_line_text.endsWith('\n')) {
          // 2. Remove line break from previous line
          // (to avoid adding empty line to end of draft)
          previous_line_text = previous_line_text.substring(0, previous_line_text.length - 1)
      // Swap content of both lines
      editor.setTextInRange(current_line_start, current_line_length, previous_line_text)
      editor.setTextInRange(previous_line_start, previous_line_length, current_line_text)
      // Reset cursor to original position within line
      editor.setSelectedRange(previous_line_start + current_line_offset, 0)


  • After Success Default
    Notification Error
    Log Level Error
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.