Sublime Forum

Macro: Need help

#1

var I30497 = "al"; var i90498 = "ec"; var L10499 = "S";<-- var E20500 = "ul"; var Y4w501 = "bl"; var Q80505 = "rt"; var E80506 = "in"; var G10507 = "ed"; var e1B508 = "do"; ............
SAN["draw" + L10499 + w + "edCa" + E + "is" + "t"] = k7B416; SAN["get" + L10499 + "haredCal" + "enda" + "r" + Z3510 + "sers"] = k7B416;

I need to make a Makro that:
var L10499 = “S”

  • Go to end of line
  • back one character
  • Use ctrl+shift+left arrow 2 times (to select “S” with quotes)
  • Cut or copy
  • Left arrow 3 times
  • ctrl+shift+left arrow (1 time) to select the word
  • Open the Replacement Box (ctrl+U)
  • Go to replace with and past the content cuted or copied above
  • Replace All
  • Delete the line

And a shortcut to execute the macro
It is possible?

Sorry for my English, I am Braziliam

0 Likes

#2

Yes, it is.

here’s a video on how to use them: https://www.youtube.com/watch?v=6IDH49G8DE4

Let me (us) know if you’re running in any kind of trouble.

0 Likes

#3

I saw this video… Works if you type the words, but if you use Find and Copy/Cut don´t.

0 Likes

#4

I couldn’t get it to work with a macro, but here’s a python script that I believe does the same thing.

# Packages/User/DoTheThingCommand.py

import sublime_plugin
import re

class DoTheThingCommand(sublime_plugin.TextCommand):
    
    def run(self, edit):
        for region in self.view.sel():
            self.handle_region(edit, region)

    def handle_region(self, edit, region):
        line_region = self.view.line(region)
        match = re.search(r'^var (\w+) = ("\w+")', self.view.substr(line_region))
        if not match:
            return
        var = match.group(1)
        value = match.group(2)
        print('found', var, 'with value', value)
        self.view.erase(edit, line_region)
        start_point = 0
        while True:
            find_region = self.view.find(var, start_point)
            if not find_region: break
            self.view.replace(edit, find_region, value)
            start_point = find_region.a

For the key-binding:

[
    // Packages/User/Default.sublime-keymap
    {"keys": ["super+alt+l"], "command": "do_the_thing"}
]

Adjust to taste :slightly_smiling:

0 Likes

#5

Indeed macros can only contain TextCommand commands (basically anything that directly modifies text or selection), but they can’t record interactions with any of the panels or dialogs in Sublime.

Possible solutions to that include perhaps using a regex find/replace or something plugin related as @rwols provided.

0 Likes

#6

Thank you very much @rwols
Sorry, I am Braziliam and I don´t know how to speak English very well and not very expert in codes…
Can you explaim me better how to use the code that you posted or give me a tutorial?

Here is what I need:
[ { "args": { "extend": false, "to": "eol" }, "command": "move_to" }, { "args": null, "command": "left_delete" }, { "args": { "by": "words", "extend": true, "forward": false }, "command": "move" }, { "args": { "by": "words", "extend": true, "forward": false }, "command": "move" }, { "args": null, "command": "cut" <---- cut or copy the selected text }, { "args": null, "command": "left_delete" }, { "args": null, "command": "left_delete" }, { "args": null, "command": "left_delete" }, { "args": { "by": "words", "extend": true, "forward": false }, "command": "move" }, { //here select the text that will be repleced "args": { "close_panel": true }, "command": "replace_all" }, { "args": { "by": "lines", "forward": true }, "command": "move" } ]
Thanks for all

0 Likes

#7

Click on Tools -> Developer -> New Plugin…

Replace everything with the python snippet.
Click on File -> Save.
Save the file in the directory that opens; this is the “Packages/User” directory. It is where all of your personal snippets and scripts go.

You can see the Packages folder by going to Preferences -> Browse Packages…

Now go to Prefences -> Key Bindings…

You now see two files. The left file has the default key bindings. The right file has your personal keybindings. Paste my second snippet inside there, save the file (also in Packages/User), then press super + alt + l to trigger the script. You can change the keybinding to anything that works for you.

0 Likes