Sublime Forum

Let curser focus on a certain string or region with commands

#1

How to make curser focus on a certain string or region with commands?
When trigger ‘python-complete-local-symbols’ after inputing def, “string def function():
pass” appears on the view and word ‘function’ is selected. I want to develope a plugin with such function, it is easy to put"string def function(): pass", but how to focus on ‘function’?

0 Likes

#2

You can achieve the effect you are looking for with Snippets.

 

Here is a StackOverflow Answer which details a basic implementation of snippets.

sublime-snippet files allow you to execute your snippets via auto-complete, command palette, and key-binding.

 

This Post details how to implement snippets from a plugin, rather than sublime-snippet files.

0 Likes

#3

What about if I just want to make curser focus on a certain region from a to b, or sertain existed words, nothing about inserting codes.

0 Likes

#4

 

Here is a basic plugin that will cycle through all instances of the regex pattern defined at regexQuery
 

 

import sublime, sublime_plugin

class SelectQueryCommand( sublime_plugin.TextCommand ):
	def run( self, edit ):

		#■■■  Set Position To Search  From  ■■■#

		selections = self.view.sel()

		if len( selections ) == 0:
			searchPoint = 0
		else:
			searchPoint = selections[0].b

		#■■■  Find Query From Current Selection To End Of Document  ■■■#

		regexQuery   = "\\bfunction\\b"
		searchResult = self.view.find( regexQuery, searchPoint )

		#■■■  Select Queried Region  ■■■#

		if searchResult:
			self.select_Region( searchResult )

		#■■■  If No Match Was Found, Find Query From Start Of Document  ■■■#

		else:

			searchPoint  = 0
			searchResult = self.view.find( regexQuery, searchPoint )

			if searchResult:
				self.select_Region( searchResult )

	#■■■  Select Result Region  ■■■#

	def select_Region( self, region ):
		self.view.selection.clear()
		self.view.selection.add( region )
3 Likes