SHARE
TWEET

Reverse Line Order with Notepad++ Python Script

a guest Oct 30th, 2012 136 Never
  1. def reverseAllLines():
  2.         lines   = editor.getText().splitlines()
  3.         lines.reverse()
  4.         editor.setText(os.linesep.join(lines))
  5.  
  6. def reverseSelectedLines():
  7.         import os
  8.  
  9.         staline = editor.lineFromPosition(editor.getSelectionStart())
  10.         endline = editor.lineFromPosition(editor.getSelectionEnd())
  11.         start   = editor.positionFromLine(staline)
  12.         end             = editor.positionFromLine(endline)+editor.lineLength(endline)
  13.  
  14.         text    = editor.getTextRange(start, end)
  15.        
  16.         if text.endswith(os.linesep):
  17.                 eol = os.linesep
  18.         else:
  19.                 eol = ''
  20.  
  21.         lines   = text.splitlines()
  22.         lines.reverse()
  23.  
  24.         text    = os.linesep.join(lines)+eol
  25.  
  26.         editor.setSelection(start, end)
  27.         editor.replaceSel(text)
  28.         editor.setSelection(start, end) #keep selection after replace
  29.  
  30.  
  31. editor.beginUndoAction()
  32. if editor.getSelectionStart() == editor.getSelectionEnd():
  33.         reverseAllLines()
  34. else:
  35.         reverseSelectedLines()
  36. editor.endUndoAction()
RAW Paste Data
Top