Sublime Forum

Repeat Macro n Times

#1

Is there a way to quickly repeat a macro a specified number of times. I attempted to record a macro and use the repeating functionality of Vintage, but it’s not working quite like vim does yet.

Would the best way to go about this if it doesn’t exist to write a plugin to do it?

0 Likes

How to repeat a macro n amount of times or until the end of the file?
#2

I wrote this little plugin to repeat macros. I guess I should move this over to the plugin development forum.

Only issue is that, while it works, it is really very slow for any large number of repetitions, and it locks up Sublime while it is running. Haven’t really explored how else to make it happen. I am a neophyte python programmer, and just downloaded/purchased Sublime a few days ago.

import sublime, sublime_plugin

class repeat_macro(sublime_plugin.WindowCommand):
	def run(self):
		self.window.show_input_panel("Repeat Macro Count:", 
			"", self.on_done, None, None)
		pass

	def on_done(self, text):
		try:
			loopcount = int(text)
			if self.window.active_view():
				for x in xrange(1,loopcount):
					for cmds in sublime.get_macro():
						self.window.active_view().run_command(
							cmds'command'], cmds'args'])
			pass
		except ValueError:
			pass
0 Likes

#3

btw, you should be able to just run the ‘run_macro’ command n times, rather than having to do it the long way via get_macro(). It’ll be faster to do it this way, too, but depending on what the bottleneck is this may make no difference.

Repeating a macro n times is generally done to apply an operation to multiple lines. You can do this more directly in Sublime Text: select the lines of interest, then split the selection into lines (ctrl+shift+l or cmd+shift+l depending on platform), and then either do the edits directly, or just replay the macro once, and it’ll apply to all lines.

1 Like

#4

Now, that’s a useful tip that should be stored somewhere!

0 Likes

#5

Hi @lkraven

I saved the plugin code to Packages/User/repeat_macro.py. When I showed the console, I got this error:

def on_done(self, text):
                       ^

IndentationError: unindent does not match any outer indentation level

I am not a Python programmer so, I tried to fix it by removing the indentation. Then I got this error:

cmds'command'], cmds'args'])
            ^

SyntaxError: invalid syntax

How do I fix this error? Can someone help me? I need this plugin for my text manipulation task: http://forum.sublimetext.com/t/how-to-repeat-a-macro-n-amount-of-times-or-until-the-end-of-the-file/

After the plugin code has been fixed, how do I run it to repeat my macro?

Thanks

0 Likes