Home Download Buy Blog Forum Support

Easiest way to input a number in a multi-selection

Easiest way to input a number in a multi-selection

Postby edanm on Thu May 06, 2010 1:34 pm

I very commonly want to insert a number when using multiple selections. Many times, I'd like that number to increase between selections.

For example, I have 3 lines which I'd like to number:
First Line
Second Line
Third Line

I'd like to add a numbering in front of each, to get:
1) First Line
2) Second Line
3) Third Line

So my question is, after multi-selecting the three lines, how can I add a number in front of each?
And how do I control the details of the numbers? (i.e., the starting number, the amount of increase between the numbers, etc.).

Most editors have some kind of way of doing this when in "column-select" mode, I think that Sublime should have something even better because of the unique way it works with multiple selections.
edanm
 
Posts: 131
Joined: Thu Mar 04, 2010 11:05 pm

Re: Easiest way to input a number in a multi-selection

Postby n00ge on Thu May 06, 2010 7:05 pm

Well, you don't need to select all 3 lines. Just use ctrl+alt+ up or down to select up and down. But, if you do select multiple lines, just hit ctrl+shift+l and it'll break your selection up into multiple lines. Then hit home/end and you can type. Ctrl+click will allow you to select multiple points as well.

Once you have multiple selections going though, you can do whatever you want in there. Type, navigate w/ keys like up, down, left, right, home, end, page up, page down, whatever you want.
http://twitter.com/n00ge
Sublime Text 3 dev builds | OS X 10.8.2 & Ubuntu 13.04
n00ge
 
Posts: 140
Joined: Mon Oct 05, 2009 1:23 am

Re: Easiest way to input a number in a multi-selection

Postby Anomareh on Thu May 06, 2010 7:29 pm

Don't think he has a problem with multiple selections. I find myself running into similar problems but it doesn't really have a clear answer. It's most likely best suited to be tackled by a plugin. Bind a popup that takes a starting number and step and have it insert the proper numbers.

From what I understand he wants to have a few selections and he wants to add an incrementing number in each. For example if you have 3 selections there is no easy to add a 1 at the beginning of the first, 2 at the beginning of the second, etc.. If you hit 1 there's going to be a 1 at the beginning of every selection.
Anomareh
 
Posts: 222
Joined: Thu Oct 01, 2009 7:32 pm

Re: Easiest way to input a number in a multi-selection

Postby gpfsmurf on Thu May 06, 2010 8:03 pm

I don't think there's currently a built-in way to do this, but I believe there was a way to do it with either the MiniPy or PowerUser plugin.
gpfsmurf
 
Posts: 211
Joined: Mon Jun 23, 2008 6:31 pm

Re: Easiest way to input a number in a multi-selection

Postby Anomareh on Thu May 06, 2010 9:44 pm

I decided to finally mess around with plugins and I made one that does this. It feels really ugly though. I don't have any Python experience so this is just what I pieced together while quickly skipping through the Python docs. I was wondering if anyone with more experienced could answer a few questions about it for me.

Code: Select all
import sublime, sublimeplugin

class insertNumbers(sublimeplugin.TextCommand):
   def run(self, view, args):
      self.view = view;
      window = view.window()
      
      window.showInputPanel('Enter a starting number and step.', '1 1', self._onDone, None, None)
      
   def _onDone(self, input):
      input = input.split(' ')
      
      for region in self.view.sel():
         if region.empty():
            self.view.insert(region.a, input[0])
         else:
            self.view.replace(region, input[0])
            
         input[0] = int(input[0]) + int(input[1])
         input[0] = str(input[0])

The first issue I had was passing integers as arguments to commands. I'm assuming commands can only take strings for some reason? That then led to the really ugly typecasting I ended up having to do. Is there a better way of doing what I did? (I hope there is :s)

Secondly, is there a better way to pass reference to the view the command was called on? I just stored it in the class and that seemed to work, but I was thinking there might be a better way of doing this?

If there's anything else that can be done cleaner please say so. My first time ever messing with Python and Sublime plugins so I'd like to start right. Thanks.


Oh and if anyone wants to mess with this plugin, bind insertNumbers to a key, it will open an input box at the bottom of the screen. The first number is the number to start with, and the second number is the step. For example, 1 1 will result in 1, 2, 3, 4.., 1 2 will result in 1, 3, 5, 7.., etc..
Anomareh
 
Posts: 222
Joined: Thu Oct 01, 2009 7:32 pm

Re: Easiest way to input a number in a multi-selection

Postby edanm on Thu May 06, 2010 10:33 pm

I was actually planning on creating my own plugin to add this functionality, but I didn't know if there was something already available.

@Anomareh - from a first glance, it looks good. I'm not on my dev computer so I don't even have Sublime here, I'll check it out when I get.


Here's the thinking I had for the plugin:
I'd like an input panel, like you have, but which lets you enter lots of different types of input.
One thing for example, should be a range (e.g. 0-5, etc.).
One other input type would be a comma-separater list ("blue, green, brown"). This would make sense when you've got multiple selections which you don't want to break up, but do want to input something else.

I'll probably write this plugin sometime next week.
Does anyone have any other good ideas for what kinds of input it should accept?
edanm
 
Posts: 131
Joined: Thu Mar 04, 2010 11:05 pm

Re: Easiest way to input a number in a multi-selection

Postby Anomareh on Thu May 06, 2010 10:44 pm

Really? There isn't a cleaner solution to the type casting? I can't imagine it's something Python, but why are so many commands restricted to strings? Just being able to use integers would make it a lot cleaner.

Most of the time I'm just editing something or prefixing / suffixing something so what I made pretty much covers anything I'd ever need. The other situations where you need to paste words is already in Sublime enough for me with column pasting. When I need to paste a column of words it's usually in a case where I'm copying variable names or something of the like. You can just make a selection of each word and it will paste into a column just fine.
Anomareh
 
Posts: 222
Joined: Thu Oct 01, 2009 7:32 pm

Re: Easiest way to input a number in a multi-selection

Postby guillermooo on Fri May 07, 2010 6:41 am

You can number a sequence of regions with the PowershellUtils plugin too:

Code: Select all
(|) One
(|) Two
(|) Three

# Note: | denotes the caret
# open "filter through PoSh"
# type: ++$a<ENTER>
# you should get:
   
(1) One
(2) Two
(3) Three


You can also do stuff like this:

Code: Select all
0..6|%{ "$($_+1) $([dayofweek]$_)" }


Beware of unintentional operations on your filesystem, though! Powershell is pretty powerful.
guillermooo
 
Posts: 729
Joined: Thu Jul 23, 2009 9:06 am

Re: Easiest way to input a number in a multi-selection

Postby guillermooo on Fri May 07, 2010 6:55 am

Another variant:
Code: Select all
"one two three" -split " "|%{++$a; "($a) $_"}
guillermooo
 
Posts: 729
Joined: Thu Jul 23, 2009 9:06 am

Re: Easiest way to input a number in a multi-selection

Postby guillermooo on Fri May 07, 2010 12:40 pm

It's worth noting that execution of user scripts is disabled by default in Powershell for security reasons.

Do this from a terminal with admin privileges if you haven't yet:

Code: Select all
set-executionpolicy remotesigned
guillermooo
 
Posts: 729
Joined: Thu Jul 23, 2009 9:06 am

Next

Return to Technical Support

Who is online

Users browsing this forum: Majestic-12 [Bot] and 27 guests