Sublime Forum

Snippet/Plugin for text manipulation (find, copy selected)

#1

I would be very grateful if someone could help me to create a snippet or plugin.
These are my needs:

  1. Find a string and go to the end of that line.
  2. Start selection and find a previous string, maintaining selection.
  3. Save the selection to a file, overwriting an existing one.
    Unfortunately, my skills are very poor.
0 Likes

#2

here you have a “start” from there I hope you can get what you desire. Is very easy to read, just take a look to http://www.sublimetext.com/docs/3/api_reference.html in case of doubt, you can trigger following command like…

/User/Default.sublime-keymap

import functools, sublime, sublime_plugin
class open_file_with_custom_find_command(sublime_plugin.WindowCommand):
def run(self):
window = sublime.active_window()
window.run_command(‘hide_panel’);
window.show_input_panel(‘Search For 1:’, ‘’, functools.partial(self.on_done1), None, None)

		def on_done1(self, search_for1):
			search_for1 = search_for1.strip()
			if search_for1:
				window = sublime.active_window()
				window.run_command('hide_panel');
				window.show_input_panel('Search For 2:', '', functools.partial(self.on_done2, search_for1), None, None)

		def on_done2(self, search_for1, search_for2):
			window = sublime.active_window()
			view = window.active_view()
			search_for2 = search_for2.strip()
			if search_for1 and search_for2:
				regions1 = view.find_all(search_for1, sublime.IGNORECASE)
				regions2 = view.find_all(search_for2, sublime.IGNORECASE)
				if regions1 and regions2:
					for item1 in reversed(regions1):
						sel_start = view.line(item1).end()
						for item2 in reversed(regions2):
							if item2.end() < sel_start:
								print(substr)
								substr = view.substr(sublime.Region(sel_start, item2.begin()))
								if substr:
									_view = window.new_file()
									_view.run_command('insert', {'characters': substr})
									break

asf{ibsbi{pfs
phisbf
´hsbfaó
bfasi
sbfa
pibfasphi
bfasó
bfas
ójbfa
ójbfasójbfasój
bfas

I fucking hate markdown, this is crap, let me put this on an external file

0 Likes

#3

You can use this

. https://dl.dropboxusercontent.com/u/9303546/SublimeText/commands/open_file_with_custom_find_command.py

as /User/open_file_with_custom_find_command.py

and the following

. https://dl.dropboxusercontent.com/u/9303546/SublimeText/commands/Default.sublime-keymap

as /User/Default.sublime-keymap to trigger the command with F11 (just an example…)

0 Likes

#4

Thank you very very much. I’ll adapt, I’ll test and I’ll report.

0 Likes

#5

Dear Tito,
Your command opens many many files after selecting every piece of text surrounded by the search strings (the source file contains a lot of similar examples surrounded by them). For me, it would be better to hardcode the strings and the file to overwrite (they are always the same). Apart from that, your code works perfectly (every extracted text was right) and it was a good proof of Sublime Text stability because of so many windows opened.
Best wishes.

0 Likes

#6

okei, if you need help after looking and trying for a bit with this page http://www.sublimetext.com/docs/3/api_reference.html let us know

0 Likes

#7

I am a complete ignorant in Python. Then, I always use existing tools and I adapt them to my needs. I’ll try and I’ll report if I am successful.
Thank you again.

0 Likes