메뉴 여닫기
개인 메뉴 토글
로그인하지 않음
만약 지금 편집한다면 당신의 IP 주소가 공개될 수 있습니다.

Sublime plugin 생성 방법

데브카페
(차이) ← 이전 판 | 최신판 (차이) | 다음 판 → (차이)

서브라임 플러그인 만드는법

  • 서브라임 플러그인 프로그램 생성 원칙
    • 클래스 이름은 반드시 Command로 끝나야 합니다.(~Command)
    • 올바른 예: class MyExampleCommand(sublime_plugin.TextCommand)
    • 잘못된 예: class MyExample(sublime_plugin.TextCommand)

서브라임 플러그인 생성 절차

  1. Sublime Text 메뉴에서 Preferences > Browse Packages... 선택
    - 이렇게 하면 Sublime Text의 패키지 디렉토리가 파일 탐색기/파인더에 열립니다.
  2. 패키지 디렉토리 내에 새 폴더 생성 (예: MyPlugin)
  3. 이 폴더 안에 Python 파일 생성 (예: my_plugin.py)
  4. 플러그인 클래스 예제(위 코드를 my_plugin.py 파일에 저장)
import sublime
import sublime_plugin
class MyExampleCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        self.view.insert(edit, 0, "Hello, World!")

플러그인 등록 및 실행 방법

  1. (저장만 하면) Sublime Text는 자동으로 플러그인을 로드합니다
  2. 플러그인 실행 방법
    1. 콘솔에서 실행: view.run_command('my_example')
    2. 키 바인딩으로 실행:
      - Preferences > Key Bindings 열기
      - 사용자 키 바인딩 파일에 추가:
{
    "keys": ["ctrl+alt+shift+h"],
    "command": "my_example"
}


플러그인 : 자동 증가 번호 추가하기(선택한 라인)

  • LineNumbersAdd.py
import sublime
import sublime_plugin    

class LineNumbersAddCommand(sublime_plugin.TextCommand):
	def run(self, edit, suffix=""):
		selections = self.view.sel()
		totalOffset = 0
		for selection in selections:
			lines = self.view.split_by_newlines(selection)
			for i, line in enumerate(lines):
				line_number_string = str(i+1)+suffix
				offset = len(line_number_string)
				# print offset, "just"
				self.view.insert(edit, line.begin() + totalOffset, line_number_string)
				totalOffset += offset

플러그인 : 라인모드에서 자동 증가 번호 추가하기(선택한 라인)

  • LineModeNumbersAdd.py
import sublime
import sublime_plugin 

class IncrementLineEditCommand(sublime_plugin.TextConmand):
def run(self, edit):
selections = self.view.sel()
if not selections:
+7
return
padding = 1 if len(selections) ‹ 10 else 2
# for index, region in enumerate (selections, start-1)
for i in range(len(selections)):
# self,view. replace(edit, region, str(index))
self.view.replace(edit, selections[1],str(i+1).zfill(padding))
# sublime,status_message(f"{len(selections)} affected")

Comments