Sublime Text: Add "Permute Lines -> Shuffle" shortcut key - sublimetext3

I wondered if anyone could help me out.
In sublime text, when I want to shuffle some lines (for example if I had a list of colour names and wanted them in random order). I've been using Ctrl+Shift+P, and then writing shuffle to get the "Permute lines: Shuffle" command. This is fairly quick but I'd love to have a shortcut for it as I use it very often. I know there's a file I can change but I don't know how to write the command.
Many thanks in advance!

Items that appear in the command palette are stored in sublime-commands files. If you use the View Package File command from the command palette and enter sublime-commands as the filter text, the list of all files in all packages that add commands to the command palette will be displayed.
The first part of the filename shows you what package is contributing the command, and commands that are part of core Sublime are in the Default/ package, so choosing the file Default/Default.sublime-commands will show you the commands Sublime ships with (note that some packages include a file named Default.sublime-commands, so make sure you're picking the Default/ version).
If you look in that file and search for the command that you see in the command palette, you'll find this (reformatted here to not be all one line):
{
"caption": "Permute Lines: Shuffle",
"command": "permute_lines",
"args": {"operation": "shuffle"}
},
This shows you the command and args you need to apply in a key binding.
For commands that also appear in the menu (or are bound to other keys and you want to remap them) you can also open the Sublime console with View > Show Console in the menu and enter sublime.log_commands(True). Now when you choose a menu item or press a key, the command that is being executed will be logged for you. The logging remains in effect until you enter sublime.log_commands(False) in the console or restart Sublime.
In this case doing that and then choosing Edit > Permute Lines > Shuffle will log this in the console:
command: permute_lines {"operation": "shuffle"}
This shows the same command and arguments that are required (if any).

Related

tmux pin to avoid scrolling

Often when I run a command that writes to stdout, and that command fails, I have to scroll up (using uncomfortable key-bindings) looking for the place where I pressed Enter, to see what the first error was (out of hundreds others, across many screens of text). This is both annoying and time-consuming. I wish there was a feature which allowed me to pin my current terminal to the place where I am now, then start the command, see only the first lines of the output (as many as fits below my cursor) and let the rest of the output be written but not displayed. In other words I would like a feature to allow me automatically scroll up to the place where I gave the command, to see the first lines of the output (where usually the origin of the failure is displayed).
I searched for it but I didn't find it. Do you know if such feature exists? Or have an idea how to implement it with some tricks or workarounds?
If you have a unique shell prompt you could bind a key to jump between shell prompts, for example something like this will make C-b S jump to the previous shell prompt then S subsequent ones:
bind S copy-mode \; send -X search-backward 'nicholas#myhost:'
bind -Tcopy-mode S send -X search-backward 'nicholas#myhost:'
Or similarly you could search for error strings if they have a recognisable prefix. If you install the tmux 3.1 release candidate, you can search for regular expressions.
Alternatively, you could use capture-pane to load the entire history into an editor with key bindings you prefer, for example:
$ tmux capturep -S- -E- -p|vim -
Or pipe to grep or whatever. Note you will need to use a temporary file for this to work with emacs.
Or try to get into the habit of teeing commands with lots of output to a file to start with.

How to list all commands in Sublime Text 3

I'd like to get a list of all available commands in sublime text 3 (built-in and from packages)
What I'm trying to do:
Create shortcuts
I'm trying to create a shortcut for a package command but I don't know the name of the command. I can find the command and use it using alt + shift + p but then when trying to add the shortcut to my .sublime-keymap file, I'm not sure that to put on the "command": "?" bit. I'd be great if I could just list all commands and grep for what I'm looking for then just copy paste the formal command name into the keymap file.
Explore
I'd like to explore all the commands that are available (built-in and from packages) to understand Sublime Text capabilities. Rather than searching for commands within sublime or reading tutorials online, I'd like to ask my editor:
What can you do?
rather than
Can you do this?
There is a fairly complete list of the core commands in Sublime available via the Community Documentation, in particular in the command list section. This however doesn't help you to learn about commands that third party packages and plugins may have added, however.
In your question you mention knowing how to get at a command but not knowing what it might be for the purposes of using it elsewhere. If you're in the situation of knowing some way to invoke a command (key, command palette, menu) and wondering what the command is, Sublime has you covered.
If you open the Sublime console with Ctrl+` or View > Show Console, you can enter the following command:
sublime.log_commands(True)
Now whenever you do anything, Sublime logs the command that it's executing the console, along with any arguments that it might take. For example, if you turn on logging and press each of the arrow keys in turn, the console will display this:
command: move {"by": "lines", "forward": false}
command: move {"by": "lines", "forward": true}
command: move {"by": "characters", "forward": false}
command: move {"by": "characters", "forward": true}
Using this facility you can figure out what commands various actions take, so that you can use them elsewhere. This is also a handy technique for diagnosing things like keyboard shortcuts that don't seem to do what you think they should do, for example. Run the same command with False instead of True (or restart Sublime) to turn the logging off.
If you're really interested in the gritty internal details of every possible command, something like the following is possible. This implements a command labelled list_all_commands which, when you run it, will list all of the available commands of all types into a new scratch buffer.
Note that not all implemented commands are necessarily meant for external use; plugins sometimes define helper commands for their own use. This means that although this tells you all of the commands that exist, it doesn't mean that all of them are meant for you to play with.
Additionally, although this lists roughly the arguments that the run method on the command class takes (which is what Sublime executes to run the command), some commands may have obscure argument lists.
import sublime
import sublime_plugin
import inspect
from sublime_plugin import application_command_classes
from sublime_plugin import window_command_classes
from sublime_plugin import text_command_classes
class ListAllCommandsCommand(sublime_plugin.WindowCommand):
def run(self):
self.view = self.window.new_file()
self.view.set_scratch(True)
self.view.set_name("Command List")
self.list_category("Application Commands", application_command_classes)
self.list_category("Window Commands", window_command_classes)
self.list_category("Text Commands", text_command_classes)
def append(self, line):
self.view.run_command("append", {"characters": line + "\n"})
def list_category(self, title, command_list):
self.append(title)
self.append(len(title)*"=")
for command in command_list:
self.append("{cmd} {args}".format(
cmd=self.get_name(command),
args=str(inspect.signature(command.run))))
self.append("")
def get_name(self, cls):
clsname = cls.__name__
name = clsname[0].lower()
last_upper = False
for c in clsname[1:]:
if c.isupper() and not last_upper:
name += '_'
name += c.lower()
else:
name += c
last_upper = c.isupper()
if name.endswith("_command"):
name = name[0:-8]
return name

How to get line numbers of selected text in vim

Is it possible to get the line numbers of selected text to pass to an external command?
Context: I'd like to integrate pyfmt into vim. Ideally, I'd like to be able to select some text and type some shortcut to have the selected text reformatted by pyfmt.
So far I've found that running !pyfmt -i % will format the whole file. pyfmt also supports a --lines START-END option. I'd like to be able to pass the line numbers of the beginning and end of the selected text to pyfmt so that only what I want to reformat gets reformatted. Is this possible?
Select the lines you want to format (preferably linewise, using capital V to enter visual mode), and then, without leaving visual mode, type :!pyfmt -i.
This will not give you the line numbers. Instead, it will filter the selected lines through the command and replace them with the output.
I will provide my case as follow and I think it can be customized to your case, easily.
I have a Vim-Plug plugin Plug 'tpope/vim-commentary', and it has a command: (l,r are line numbers)
:l,rCommentary
When you visual-selected lines in Vim and then press : you will get:
:'<,'>
Based on this observation the command I need is: (visual mode mapping)
vnoremap <silent> my_shortcut :Commentary<CR>gv
i.e. I just need :Commentary since when it execute : the '<,'> is added for you.
To understand <silent>: https://stackoverflow.com/a/962118/5290519

SublimeText: How to find the command for "Next Build Result" to map it to a different key?

Background: I am working on an SML file on SublimeText3 with build system setup.
After a build, i can successfully jump to the first error using the F4 key. I want to add another key mapping for the same "Next Result" command eg:Cmd+N in Vintage mode.
What should i add as in my keybindings file to achieve this?
What documentation,file did you refer/look around to find the proper answer for question 1? What was your thought process in brief to figure it out ?
edit: changed the required keybinding from <leader>cn to Cmd+N to make things easier
For future reference, you can type sublime.log_commands(True) into the Sublime console and then the command name of every action you do will get printed to the console. Once you get the command name you need, you can stop the command printing with sublime.log_commands(False)
You can find the command being called on a default key-binding in the default key bingings file. This file is opened using the menu:
Preferences > Key Bindings - Default
Inside this file you must find the entry corresponding to the keys you are looking for, in this case f4 key is mapped to next_result command as you can see in the key binding:
{ "keys": ["f4"], "command": "next_result" }

Bind shortcut to command palette command?

I just installed a plugin called CodeSniffer (http://soulbroken.co.uk/code/sublimephpcs), and I want to link one of it's commands from the command palette to a keyboard shortcut because I use it so often.
Is there any easy way to do this? Or will I just need to ask the developer what the name of the command is (in the command palette it is 'PHP CodeSniffer: Clear sniffer marks')?
Thanks
It's actually very easy to find the name of a command but it requires a few steps.
Open Sublime Text's built-in console (control+`)
Type in sublime.log_commands(True)
Trigger the command from the command palette
The name of the command will be logged to the console. Then open your user keybindings and create a new keybinding like this:
{ "keys": ["YOUR_SEQUENCE"], "command": "YOUR_COMMAND" }
I provided a similar answer here: Keymap Sublime Text 2 File Type?
Another way is to crack open the .sublime-commands files.
Let's say you've installed Sublime Package Control (which you really want to do!) and then open it up in the command palette (⌘⇧p on os x) and install the Search Stack Overflow package. You'll now have two new commands in the command palette, the "Stackoverflow: Search Selection" and "Stackoverflow: Search from Input" commands.
OK, open the .sublime-commands file for the package. You need to find it first. If you're hardcore you do View > Show Console, and enter print(sublime.packages_path())
Otherwise it should be here
Windows: %APPDATA%\Sublime Text 2\Packages
OS X: ~/Library/Application Support/Sublime Text 2/Packages
Linux: ~/.Sublime Text 2/Packages
Portable Installation: Sublime Text 2/Data/Packages
and then "Search Stack Overflow/Default.sublime-commands"
This is the file that make the commands show up in the command palette in the first place.
It's another JSON file with entries like these
{
"caption": "Stackoverflow: Search from Input",
"command": "stackoverflow_search_from_input"
}
see, that's the command name right there: stackoverflow_search_from_input
Now just open the user key bindings JSON file and add the key binding like #BoundinCode said.

Resources