Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 774
Lubos Mudrak Avatar asked Mar 10 '15 14:03

Lubos Mudrak


4 Answers

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.

like image 140
未来陆家嘴顶尖的投资人 Avatar answered Oct 17 '22 17:10

未来陆家嘴顶尖的投资人


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

like image 23
MattSaw Avatar answered Oct 17 '22 16:10

MattSaw


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.

like image 14
kotoole Avatar answered Oct 17 '22 16:10

kotoole


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.

like image 9
Leonid Shevtsov Avatar answered Oct 17 '22 16:10

Leonid Shevtsov