Lubos Mudrak
Lubos Mudrak

Reputation: 680

Reverse all line of text in Sublime Text

I have a file where I have multiple lines. Is there an option in Sublime Text 3 to reverse whole line ? Like

ABCDEFG

to

GFEDCBA

Upvotes: 23

Views: 13002

Answers (4)

kotoole
kotoole

Reputation: 1746

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.

Upvotes: 16

sixsixsix
sixsixsix

Reputation: 1878

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.

Upvotes: 54

MattSaw
MattSaw

Reputation: 471

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

Upvotes: 26

Leonid Shevtsov
Leonid Shevtsov

Reputation: 14179

Not inside Sublime Text, but in Linux/OSX the rev command-line utility does just that - rev file.txt reverses every line of the file.

Upvotes: 10

Related Questions