Skip to content

Commit

Permalink
Add shortcut to move a line up and down
Browse files Browse the repository at this point in the history
Pressing Alt+Arrow Up / Alt+Arrow Down will move the current line
up/down. This is a nice feature to move lines without having to do a
Select-Cut-Move-Paste manipulation.
  • Loading branch information
xgouchet committed Oct 17, 2017
1 parent 7deca6c commit df36715
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions ReText/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,36 @@ def keyPressEvent(self, event):
# Insert Markdown-style line break
cursor.insertText(' ')
self.handleReturn(cursor)
elif key == Qt.Key_Down and event.modifiers() & Qt.AltModifier:
# Select the current block
cursor.movePosition(QTextCursor.StartOfBlock, QTextCursor.MoveAnchor)
cursor.movePosition(QTextCursor.NextBlock, QTextCursor.KeepAnchor)
text = cursor.selectedText()
# Remove it
cursor.removeSelectedText()
# Move to the next block
cursor.movePosition(QTextCursor.NextBlock, QTextCursor.MoveAnchor)
# Paste the previous line
cursor.insertText(text)
# Move to the pasted block
cursor.movePosition(QTextCursor.PreviousBlock, QTextCursor.MoveAnchor)
# Update cursor
self.setTextCursor(cursor)
elif key == Qt.Key_Up and event.modifiers() & Qt.AltModifier:
# Select the current block
cursor.movePosition(QTextCursor.StartOfBlock, QTextCursor.MoveAnchor)
cursor.movePosition(QTextCursor.NextBlock, QTextCursor.KeepAnchor)
text = cursor.selectedText()
# Remove it
cursor.removeSelectedText()
# Move to the previous block
cursor.movePosition(QTextCursor.PreviousBlock, QTextCursor.MoveAnchor)
# Paste the previous line
cursor.insertText(text)
# Move to the pasted block
cursor.movePosition(QTextCursor.PreviousBlock, QTextCursor.MoveAnchor)
# Update cursor
self.setTextCursor(cursor)
else:
if event.text() and self.tableModeEnabled:
cursor.beginEditBlock()
Expand Down

0 comments on commit df36715

Please sign in to comment.