Virajsinh

Word Count Plugin Sublime 3

Jun 30th, 2021 (edited)
1,296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.86 KB | None | 0 0
  1. # Tool -> Developer -> New Plug in
  2. # Save => C:\Users\YOUR_USER_NAME\AppData\Roaming\Sublime Text 3\Packages\User
  3. import sublime, sublime_plugin, re
  4.  
  5. class CountWordsInSelectionCommand(sublime_plugin.EventListener):
  6.  
  7.     def on_selection_modified(self, view):
  8.         '''
  9.        listen to event 'on_selection_modified' and count words in all
  10.        selected regions when invoked.
  11.        '''
  12.  
  13.         # clear status bar if nothing is selected
  14.         if len(view.sel()) == 1 and view.sel()[0].size() == 0:
  15.             view.set_status("words_in_selection", "")
  16.             return
  17.  
  18.         count = 0
  19.         for region in view.sel():
  20.             for i in range(region.begin(), region.end()):
  21.                 if view.classify(i) & sublime.CLASS_WORD_START:
  22.                     count += 1
  23.  
  24.         view.set_status("words_in_selection", "{} words".format(count))
Add Comment
Please, Sign In to add comment