Hello,
Somebody can confirm if it’ll be possible to add some customs commands in the right menu for sublime text 4 ?
I would like to add a shortcut in the right menu to create a random string (8 cars).
Thanks in advance for your help.
Hello,
Somebody can confirm if it’ll be possible to add some customs commands in the right menu for sublime text 4 ?
I would like to add a shortcut in the right menu to create a random string (8 cars).
Thanks in advance for your help.
Thanks for your reply.
I succeed to create a short in the context menu.
[
{
"caption": "Custom",
"id": "file",
"children":
[
{ "caption": "Chaine aléatoire sur 8 caractères", "command": "random_string", "args": {"length": 8}}
]
}
]
I created a python script in the same folder to generate the random string :
import sublime
import sublime_plugin
import random
import string
class random_string(sublime_plugin.WindowCommand):
def run(self, length=8):
window = self.window
letters = string.ascii_lowercase
result_str = ''.join(random.choice(letters) for i in range(length))
print("Random string of length", length, "is:", result_str)
return result_str
I can see the message in the console, but I don’t find how to write the result inside the current opened file.
You can do that with a plugin such as the following:
import sublime
import sublime_plugin
import random
import string
class RandomStringCommand(sublime_plugin.TextCommand):
def run(self, edit, length=8):
letters = string.ascii_lowercase
result = ''.join(random.choice(letters) for i in range(length))
self.view.run_command('insert', {'characters': result})
There are a few ways to insert text, but the easiest one is to use the built in insert
command, which will do the job and make sure that text is inserted at all curors, will replace selected text as expected, etc.
Your plugin used a WindowCommand
, and while that could be used for something like this with some modifications, generally speaking you want to use a TextCommand
for commands that are specific to files (whereas WindowCommand
commands are specific to windows instead).
You can generate more random strings by setting letters
to string.ascii_lowercase + string.ascii_uppercase + string.digits
Thanks, I updated my short code
import sublime
import sublime_plugin
import random
import string
class random_string(sublime_plugin.TextCommand):
def run(self, edit, length=8):
if (length <= 0): return false
letters = string.ascii_lowercase + string.ascii_uppercase
result = random.choice(letters)
letters += string.digits
result += ''.join(random.choice(letters) for i in range(length - 1))
self.view.run_command('insert', {'characters': result})