A grammar check plugin for SublimeText3

I am using SublimeText3. I bought a license for it in 2017. After custom configuration to disable autocomplete and other smart features, I see no reason to upgrade. Even more so now that I can make plugins at will. If you are using SublimeText4 or later, see if this helps you, and if not, you can always ask the AI.
The plugin will call an external program on the contents of the current file. I am using the check-grammar script from a previous post . It will show the external program's output in a new file tab. You can easily modify this plugin so that it calls any program you want. Have fun.
There is a way to call the plugin from the console (press ctrl+`to bring up the console), but I don't recommend that, so I won't cover that here. I'm gonna use a keyboard shortcut, specifically ctrl+b . By default, this shortcut runs sublime's build system, which is something that you should never do. I refuse to elaborate.
Put this code snippet into
~/.config/sublime-text-3/Packages/User/Default (Linux).sublime-keymap:
{"keys": ["ctrl+b"],"command": "checkgrammar"}
The code in that file is meant to be a list of curly-brace items. Each item needs a comma after it, except for the last item. The whole list of items must be in a set of [ ] square brackets. Why? I don't know. SublimeText3 will complain if you do it wrong.
Next, register a command name in
~/.config/sublime-text-3/Packages/User/Default.sublime_commands:
[ {"caption": "Check Grammar", "command": "checkgrammar"} ]
That is what the complete file looks like if it only has one item. Remember the commas if there is more than one item.
Finally, the plugin code
~/.config/sublime-text-3/Packages/User/Checkgrammar.py:
# sublime-text-3 plugin, restricted to python3.3 import sublime import sublime_plugin import os import subprocess import tempfile import threading import select class CheckgrammarCommand(sublime_plugin.TextCommand): def run(self, edit): file_path = self.view.file_name() if not file_path: sublime.message_dialog("No file is currently open.") return subcmd = os.path.expanduser("~/bin/check-grammar") # Prepare the command command = ["/bin/bash", subcmd, "--no-color", file_path] # Run the command in a separate thread thread = threading.Thread(target=self.run_command, args=(command,)) thread.start() def run_command(self, command): output_lines = [] error_lines = [] try: process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True) # Use select to read output while True: # Use select to wait for output reads = [process.stdout, process.stderr] ready_to_read, _, _ = select.select(reads, [], []) for stream in ready_to_read: line = stream.readline() if line: if stream is process.stdout: output_lines.append(line) print("Output:", line, end='') else: error_lines.append(line) print("Error:", line, end='') else: # If line is empty, the stream has been closed break # Check if the process has finished if process.poll() is not None: break # Wait for the process to complete process.wait() # I am intentionally not checking the process return code # because pyLanguagetool will return a nonzero code for a # GRAMMAR ERROR! # Combine output and error lines combined_output = \ ''.join(output_lines) + ''.join(error_lines) # Display the output in a new buffer sublime.set_timeout( lambda: self.display_output(combined_output), 0) except Exception as e: sublime.message_dialog( "An unexpected error occurred: {}".format(str(e))) def display_output(self, content): # Open a new buffer to display the output new_view = sublime.active_window().new_file() new_view.set_name("Grammar Check Output") # Insert the collected content into the new view new_view.run_command("append", {"characters": content})
But did you even use it? Did you use the plugin to check your grammar on this blog post? No, but I could have, and that's what matters.
So I did this and it changed my life.
Comments:
💯@HughHacker 10 minutes ago.
The comment section is now closed.
🍒@simONE 17 minutes ago.
So, more AI slop. I expected better, Hugh.
😎@matthew8073 20 minutes ago.
FIRST!