41

Currently, the Go to line shortcut (CTRL+G in windows/linux) only allows to navigate to a specific line.

It would be nice to optionally allow the column number to be specified after comma, e.g.

:30,11 to go to line 30, column 11

Is there any plugin or custom script to achieve this?

Allen Bargi
  • 14,674
  • 9
  • 59
  • 58
Leo Gallucci
  • 16,355
  • 12
  • 77
  • 110
  • See also ["how to go to column by its number in sublime text 3" at Super User](http://superuser.com/questions/636699/how-to-go-to-column-by-its-number-in-sublime-text-3). – Frank Tan Oct 03 '16 at 19:32

1 Answers1

65

Update 3

This is now part of Sublime Text 3 starting in build number 3080:

Goto Anything supports :line:col syntax in addition to :line

For example, you can use :30:11 to go to line 30, column 11.

Update 1 - outdated

I just realized you've tagged this as sublime-text-3 and I'm using 2. It may work for you, but I haven't tested in 3.

Update 2 - outdated

Edit 3: all requirements of the package_control repo have been met. this package is now available in the package repository in the application ( install -> GotoRowCol to install ).

I too would like this feature. There's probably a better way to distribute this but I haven't really invested a lot of time into it. I read through some plugin dev tutorial really quick, and used some other plugin code to patch this thing together.

Select the menu option Tools -> New Plugin

A new example template will open up. Paste this into the template:

import sublime, sublime_plugin


class PromptGotoRowColCommand(sublime_plugin.WindowCommand):
        def run(self, automatic = True):
                self.window.show_input_panel(
                        'Enter a row and a column',
                        '1 1',
                        self.gotoRowCol,
                        None,
                        None
                )
                pass

        def gotoRowCol(self, text):
                try:
                        (row, col) = map(str, text.split(" "))

                        if self.window.active_view():
                                self.window.active_view().run_command(
                                        "goto_row_col",
                                        {"row": row, "col": col}
                                )
                except ValueError:
                        pass


class GotoRowColCommand(sublime_plugin.TextCommand):
        def run(self, edit, row, col):
                print("INFO: Input: " + str({"row": row, "col": col}))
                # rows and columns are zero based, so subtract 1
                # convert text to int
                (row, col) = (int(row) - 1, int(col) - 1)
                if row > -1 and col > -1:
                        # col may be greater than the row length
                        col = min(col, len(self.view.substr(self.view.full_line(self.view.text_point(row, 0))))-1)
                        print("INFO: Calculated: " + str({"row": row, "col": col})) # r1.01 (->)
                        self.view.sel().clear()
                        self.view.sel().add(sublime.Region(self.view.text_point(row, col)))
                        self.view.show(self.view.text_point(row, col))
                else:
                        print("ERROR: row or col are less than zero")               # r1.01 (->)

Save the file. When the "Save As" dialog opens, it should be in the the Sublime Text 2\Packages\User\ directory. Navigate up one level to and create the folder Sublime Text 2\Packages\GotoRowCol\ and save the file with the name GotoRowCol.py.

Create a new file in the same directory Sublime Text 2\Packages\GotoRowCol\GotoRowCol.sublime-commands and open GotoRowCol.sublime-commands in sublime text. Paste this into the file:

[
    {
        "caption": "GotoRowCol",
        "command": "prompt_goto_row_col"
    }
]

Save the file. This should register the GotoRowCol plugin in the sublime text system. To use it, hit ctrl + shift + p then type GotoRowCol and hit ENTER. A prompt will show up at the bottom of the sublime text window with two number prepopulated, the first one is the row you want to go to, the second one is the column. Enter the values you desire, then hit ENTER.

I know this is a complex operation, but it's what I have right now and is working for me.

Frank Tan
  • 4,234
  • 2
  • 19
  • 29
Bill Stidham
  • 1,460
  • 13
  • 8
  • 1
    +1 very nice, create a github repo and register it in package manager channel – Ilan Frumer Jan 22 '14 at 16:38
  • Done. See `Edit 2` of my answer, ILan. – Bill Stidham Jan 23 '14 at 04:05
  • Thank Bill, you did a great job!!! I just had to add parenthesis to your `print("...")` statements to make it Sublime Text 3 compatible ;) feel free to upgrade the plugin!!! and thanks again!! – Leo Gallucci Jan 25 '14 at 00:45
  • 1
    btw, you could also add doc about setting up hotkeys for your great plugin, this worked for me: `{ "keys": ["ctrl+g"], "command": "prompt_goto_row_col", "args": {} }` – Leo Gallucci Jan 25 '14 at 01:02
  • Hey, @elgalu. I pushed a new revision to correct this issue and also to implement the ctrl+g functionality. I put instructions in the [README.mg](https://github.com/bstidham/sublimetext2-GotoRowCol) indicating the overriding of this hotkey with instructions for still utilizing the default "goto row" functionality with the plugin installed. [Link to issue view](https://github.com/bstidham/sublimetext2-GotoRowCol/issues/1) – Bill Stidham Jan 25 '14 at 03:24
  • Yes i saw! Thanks again :) You can update the answer to let know it now works in ST3 and make more people happy – Leo Gallucci Jan 25 '14 at 14:35
  • Anyway to override the default ctrl+g to this? like when it pops open i with the `:` can u make it so i can just type there? awesome btw thx – Noitidart Jan 15 '15 at 04:46