Reverse all line of text in Sublime Text

You best bet would definitely to take Leonid's advice and use a different tool, but if you are curious as to how one might do that in Sublime you have two options.


First go to Tools->New Plugin and paste the following code into the file:

import sublime, sublime_plugin

class ReverseCharactersCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        for region in self.view.sel():
            stringContents = self.view.substr(region)
            self.view.replace(edit, region, stringContents[::-1])

Following that select the different sections of the document that you want reversed and run the follow command from the console

view.run_command("reverse_characters")

Here is an image of that workflow.

enter image description here

The import section of that code is the:

stringContents[::-1]

Which is an idiomatic way of reversing a string in Python.


Alternatively you could go checkout this follow git repository and which has the same code and a convenient command palette options specified for you :)

https://github.com/MattSeen/ST_ReverseCharacters


In vanilla sublime:

  1. Select the text to reverse
  2. Open the Find menu (Ctrl+F)
    • Ensure "In selection" and "Regular expression" options are enabled
  3. Search for . and click "Find all" (+Enter)
    • Now each character of the selection is highlighted with its own cursor.
  4. Click Edit > Permute selections > Reverse

It's not elegant but it is straightforward and repeatable. If you already have the cursors, all you need is step 4.


if someone need to do the following operation.

12345
67890
abcde
  |
  to
  |
  v

abcde
67890
12345

click Edit---->Permute lines--->Reverse and it will reverse all lines you selected in a file.