193

In Sublime Text 2, is it possible to instantly select every other (or odd/even) line and place multiple cursors on those lines?

Thanks.

1
  • 7
    Don't you want to accept an answer? Jul 24, 2014 at 15:29

5 Answers 5

481
  1. Find: Ctrl+F
  2. If regular expressions are not already enabled, enable them: Alt+R
  3. Type in the expression .*\n.*\n
  4. Find all: Alt+Enter
  5. Press left arrow to get rid of the selections, leaving just the cursors:
  6. You now have a cursor at the start of every odd-numbered line. If you wanted even-numbered lines, press down:
  7. Depending on the file, there might be one cursor missing right down the bottom of the file. Using the mouse (damn!) scroll to the bottom, hold down Ctrl, and click where the missing cursor should be to add it in.
4
  • 96
    You can also use (.*\n){10} for every 10 lines
    – Joe Daley
    Nov 1, 2013 at 1:05
  • 36
    As a new improvement : (.*(\n|$)){2}
    – zessx
    Sep 24, 2014 at 12:07
  • 3
    nice! @zessx's (.*(\n|$)){2} solution includes the last line
    – woojoo666
    Oct 27, 2015 at 16:39
  • 3
    Just to note, in Sublime 2 running on OS X 10.11.5, the shortcut for RegEx search is Alt + Command + R. You can also enable RegEx search by pressing Command + F and then clicking the icon that looks like this: [ .* ] Jun 16, 2016 at 21:25
106

You can do it easily :

  • Select all your lines, or the whole document Ctrl+A
  • Add multiple selectors : Ctrl+Shift+L (and in Mac: Command + Shift + L)

EDIT :

3
  • 14
    though not the right answer for this question, this is what i wanted. thanks for sharing Jul 24, 2013 at 5:21
  • Great and simple hint!
    – filip
    Aug 30, 2013 at 17:46
  • 6
    This solution is what I needed but not what the OP wants.
    – shriek
    Jun 12, 2014 at 3:02
15

I was searching for a way to select alternate lines in sublime.

Thanks to Joe Daley for a very good answer. Though I realised that, if you use regex it would not select the last line in the file if there is no new-line at the end of the file.

I wanted to improve that answer but I don't seem to have enough reputation at the moment to comment on the answer above.

You can use the following search string with the regex turned on, and then press alt+enter. Followed by a left arrow. This would put a cursor each on alternate lines (same steps as explained by Joe Daley)

^.*\n.*$
1
  • This comment from 2014 just saved my morning. Thanks.
    – Dobhaweim
    May 16 at 8:57
7

You can try with a plugin: Tools/New Plugin...

import sublime_plugin


class ExpandSelectionToOtherLinesCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        self.view.window().run_command("expand_selection", {"to": "line"})
        start_region = self.view.sel()[0]
        self.view.window().run_command("select_all")
        self.view.sel().subtract(start_region)

Save this file in your Packages/User.

Then, add the key binding for that plugin:

{ "keys": ["super+alt+l"], "command": "expand_selection_to_other_lines" }

This command will select all other lines. When you have other lines selected, you can use Split selection into lines command (Ctrl+Shift+L, Cmd+Shift+L on Mac).

If you want to have everythnig in a single shortcut, you can modify the plugin like this:

import sublime_plugin


class ExpandSelectionToOtherLinesCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        self.view.window().run_command("expand_selection", {"to": "line"})
        start_region = self.view.sel()[0]
        self.view.window().run_command("select_all")
        self.view.sel().subtract(start_region)
        self.view.window().run_command("split_selection_into_lines")
        self.view.window().run_command("move", {"by": "characters", "forward": False})

The last line is only to remove selection, leaving multiple cursors at the beginning of selected lines.

2
  • 1
    Wow this looks promising - I'm gonna try this as soon as I can! Mar 24, 2013 at 17:47
  • Thank you very much! I'm fairly new to the plugins. Should be fun. Begin able to select every other line would be just fantastic. Cheers! Mar 24, 2013 at 17:48
0

I also wrote a plugin to do that, but I'm too lazy to publish it, so you can just create a SelectEveryNthLine.py file in Packages/User and put this code inside:

import sublime
import sublime_plugin

class SelectEveryNthLine( sublime_plugin.TextCommand ):
    def run( self, edit ):

        def on_done( n ):
            n = int( n )

            selections = self.view.sel()

            # if there is nothing selected, then select everything
            if len( selections ) == 1 and selections[0].size() == 0:
                selections.add( sublime.Region( 0, self.view.size() ) )

            new_selections = []

            for sel in selections:
                for i in range( self.view.rowcol( sel.begin() )[0], self.view.rowcol( sel.end() )[0] + 1, n ):
                    line_region = self.view.line( self.view.text_point( i, 0 ) )
                    if not sel.contains( line_region ):
                        continue
                    new_selections.append( line_region )
            selections.clear()
            for sel in new_selections:
                selections.add( sel )

        # firstly ask for the N
        sublime.active_window().show_input_panel( "Enter N to select ever N-th line:", "", on_done, None, None )

You can also create file SelectEveryNthLine.sublime-commands and put following code inside, to make it accessible from Command Palette:

[
    {
        "caption": "Select every N-th line",
        "command": "select_every_nth_line"
    }
]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.