Sublime Forum

Insert tabs (or spaces) until a specific column

#1

Having got a ruler at column 65, so I could align all my short comments, I realised it would be handy to have a keybinding that just took my cursor to (in this case) column 65, rather than hitting tab,tab,tab,tab,tab,tab,tab,tab,tab,tab,tab,tab, or worse, space,space,space,space,space,space,space,space,space,space, until I got to the desired location.

Having hunted around for half an hour there doesn’t seem to be anything, the closest I’ve found is goto anything (which I can’t get it to go to a spec. column, row yes) . Or maybe a goto_row_col, which is a plugin I think.

Any ideas anyone? This would be really really handy !

Cheers, Lozminda

1 Like

#2

I discovered this post in my search

But no-one has answered this either, so I’m not the only one with these bizzare ideas…

Ps Could some one point me in the direction of some documentation re “commands” and their “args”. There’s all this “psuedo” coding language but can I find any kind of documentation? maybe I’m missing something simple… Was thinking about the command “move_to” but can I find any info…? Thank you to any one that answers. :smiley:

0 Likes

#3

The commands correspond to either the base package or plugin. The args, are defined for each command. You can just check the corresponding python to know what are the possible arguments.

As for your request, it is quite simple: save the following in your user package directory

import sublime, sublime_plugin

class GotoColCommand(sublime_plugin.TextCommand):

    def run(self, edit, col=120):
        for s in self.view.sel() :
            _ , c_col = self.view.rowcol(s.a)
            nb_space = col - c_col
            if nb_space > 0:
                self.view.insert(edit, s.a, ' '*nb_space)

And add a keybind :

{ "keys": ["ctrl+shift+tab"], "command": "goto_col", "args": {"col": 100} },
0 Likes

Indent to a column
#4

@Clams, thanks for your reply awesome !

I’ll document what I did for any other noobs (like myself) out there, because is wasn’t sure how the command “goto_col” was linked to the plugin. (The answer is the filename)

So the above code snippet (containing class GotoColCommand etc) I saved in my user directory (which on my system is home/.config/sublime-text-3/Packages/User. Am running linux).
I saved it under the name of “goto_col.py”. The ‘.py’ is important, it’s so ST3 recognises the file as a plugin. The name can be anything. The name of the command is parsed so that capitals are changed to lowercase with an uderscore before so GotoCol becomes goto_col
I then saved the keybinding in my keybindings.

Just to hammer it home,if my class had been “ColumnsGoWhizzCommand” my keybinding would have been:

{ “keys”: [“ctrl+shift+tab”], “command”: “columns_go_whizz”, “args”: {“col”: 64} },

I also changed it so that it goes to column 64 !

If I’ve made any mistakes please let me know, I’ll correct.

I have a final question, say I didn’t want to insert any characters (spaces or tabs) but still goto column 64, how could I change the plugin?
Is that possible ?
I could use this in a macro, say if I was in the middle of a line of text.
On hitting the keybinding for the macro, it goes to EOL, then does the goto_col command.
That would avoid inserting x amount of spaces into an otherwise ok piece of text.
Also (as in my OP I’m talking about having all my short comments lined up nicely) if I have a comment at col 64, being able to use the goto_col to go to that comment (without inserting more characters)
I know my OP did ask for insertion or space and tabs but I though it was my only option, but I’ve realised the short fall in my thinking and posting.

Any further thoughts much appreciated and thanks for the plugin !

Lozminda :+1::call_me_hand::vulcan_salute::+1::grinning:

0 Likes

#5

The name of the file is not important at all; only that it’s a .py file and that it is in the root level of a package. The name of the command comes from the name of the class that defines it.

This is vital because plugins can have any number of commands in them. For example use View Package File from the command palette and open Default/ui.py; this file contains three commands (none of which is named ui) plus some support code.

This video also covers this topic:

0 Likes

#6

Just for completion with the EOL functionality, so when crtl+shift+g is pressed: the cursor goes to the end of the line, then inserts spaces to column 64 (whatever the length of the line). I used the following key binding:

  { "keys": ["ctrl+shift+g"],
  	"command":"multicommand",
  	"args":{
  	"commands":[
  		{"command": "move_to", "args": {"to": "eol", "extend": false} },
		{"command": "goto_col", "args": {"col": 64} }
	    ]
  	}
  },

the Multicommand package needs to be installed.
Ps sorry about the indenting, it’s set to two, but the keybindings file is set to four, and not sure how to change that setting…

1 Like

#9

In that particular video, jump to about 4:35; the filename I saved was sample.py but the command is named example.

0 Likes

#10

Have edited, have no idea whether what’s in bold is true… I’m guessing so ?
You do indeed say you can call the file anything you want (my bad, apologies) but there’s no mention of how the python class name and the “command” that’s used in the keybinding are linked. It’s not explicitly stated I don’t think. I checked your time stamp ,for example, but it only says you can call the file whatever you want. To be honest I didn’t look at anyone else’s videos, so am loyal, but stupid ! :stuck_out_tongue_winking_eye:

0 Likes

#11

Actually running a command is in a different video as a small example (and more in depth like where the command name comes from is scheduled for an upcoming video in the series).

0 Likes

#12

Thanks for your help. Just a thought, would the whole installing a python plugin (from say this forum or other sources) and it’s various pitfalls and how to, be worth one of your faster , more condensed videos ?
Thanks again, good night/afternoon (it’s late here!) Lozminda :+1:

0 Likes

#14

Elastic Tabstops

@Lozminda, I believe the functionality you’re looking for is called “Elastic Tabstops”:

http://nickgravgaard.com/elastic-tabstops/

There’s a ST package for this:

https://packagecontrol.io/packages/ElasticTabstops

0 Likes

#15

I ended up writing a plug in for this called AlignComment on github. I tried adding it to the package manager but it was never included by default so will need installing from a copy.
It aligns comments added to an existing line to a user defined column and also aligns stand alone comments throughout the code to the following text indentation. It has been written for VHDL comments but should work with standard language comment - just not exhaustively tested. Feel free to try this and see if it works for you.

0 Likes

#16

@Cornishman & @tajmone. Thanks for your input. I’ve got some package troubles possibly, so I’m going to leave off installing anything else until I get those issues resolved. I’ll get back to this later. I’m going to tackle my larger fish. Thanks again ! :robot: :space_invader: :+1: :shark:

0 Likes