Sublime Text - 转到行和列 [英] Sublime Text - Goto line and column

查看:139
本文介绍了Sublime Text - 转到行和列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,转到行快捷方式(在 windows/linux 中为 CTRL+G)仅允许导航到特定行.

可选地允许在逗号后指定列号会很好,例如

:30,11 转到第 30 行,第 11 列

是否有任何插件或自定义脚本可以实现此目的?

解决方案

更新 3

现在是 Sublime Text 3 的一部分,从版本号 3080 开始:

<块引用>

Goto Anything 支持 :line:col 语法和 :line

例如,您可以使用 :30:11 转到第 30 行,第 11 列.

更新 1 - 过时

我刚刚意识到您已将其标记为 sublime-text-3 而我正在使用 2.它可能对您有用,但我还没有在 3 中进行测试.

更新 2 - 过时

编辑 3:已满足 package_control 存储库的所有要求.这个包现在在应用程序的包存储库中可用( install -> GotoRowCol to install ).

我也想要这个功能.可能有更好的方法来分发它,但我并没有真正投入大量时间.我非常快速地阅读了一些插件开发教程,并使用了一些其他插件代码来修补这个东西.

选择菜单选项Tools -> New Plugin

将打开一个新的示例模板.将此粘贴到模板中:

import sublime, sublime_plugin类 PromptGotoRowColCommand(sublime_plugin.WindowCommand):def 运行(自我,自动 = True):self.window.show_input_panel('输入一行和一列','1 1',self.gotoRowCol,没有任何,没有任何)经过def gotoRowCol(self, text):尝试:(row, col) = map(str, text.split(" "))如果 self.window.active_view():self.window.active_view().run_command("goto_row_col",{行":行,列":列})除了值错误:经过类 GotoRowColCommand(sublime_plugin.TextCommand):def 运行(自我,编辑,行,列):打印(信息:输入:" + str({行":行,列":列}))# 行和列从零开始,所以减 1# 将文本转换为整数(row, col) = (int(row) - 1, int(col) - 1)如果行>-1 和 col >-1:# col 可能大于行长col = min(col, len(self.view.substr(self.view.full_line(self.view.text_point(row, 0))))-1)打印(信息:计算:" + str({行":行,列":列}))#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))别的:打印(错误:行或列小于零")#r1.01(->)

保存文件.当另存为"对话框打开时,它应该位于 Sublime Text 2\Packages\User\ 目录中.向上导航一级并创建文件夹 Sublime Text 2\Packages\GotoRowCol\ 并保存名为 GotoRowCol.py 的文件.

在同一目录下创建一个新文件 Sublime Text 2\Packages\GotoRowCol\GotoRowCol.sublime-commands 并在 sublime text 中打开 GotoRowCol.sublime-commands.将其粘贴到文件中:

<预><代码>[{"caption": "GotoRowCol",命令":prompt_goto_row_col"}]

保存文件.这应该在 sublime 文本系统中注册 GotoRowCol 插件.要使用它,请点击 ctrl + shift + p 然后输入 GotoRowCol 并点击 ENTER.在 sublime 文本窗口的底部会出现一个提示,其中预填充了两个数字,第一个是您要转到的行,第二个是列.输入您想要的值,然后按 ENTER.

我知道这是一项复杂的操作,但这是我现在拥有的并且正在为我工​​作.

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?

解决方案

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.

这篇关于Sublime Text - 转到行和列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆