如何打开文件并从命令行在Sublime上选择/突出显示几行? [英] How to open a file and select/highlight several lines on Sublime from the command line?

查看:108
本文介绍了如何打开文件并从命令行在Sublime上选择/突出显示几行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道subl myfile.txt:5将在第5行打开 myfile.txt。但是,我希望能够从命令行打开突出显示或选中的第5,9,15行的文件。我知道添加–command应该可以使我做到这一点,但是怎么做呢?该命令将是什么?

I know subl myfile.txt:5 would open "myfile.txt" on line 5. I however want to be able to, from the command line, open a file with say lines 5,9,15 highlighted or selected. I know adding –command should enable me to do that, but how? What would the command be?

推荐答案

据我所知,没有内置命令可以做到这一点,但是可以很容易地做到这一点。创建一个。

There's no built-in command that I know of that can do this, but one can easily create one.

(从技术上讲,可以使用默认包中的书签功能以及内置的将选择范围扩展到行功能来完成。)显示出专门为此目的在ST中编写命令会更好,更可靠。)

(Technically, it could be done using the bookmarks functionality from the Default package, and the built-in "Expand Selection to Line" functionality. However, experience shows that it would be better and more reliable to write a command in ST specifically for this purpose.)

在ST中:


  • 从工具菜单->开发人员->新插件...

  • 全选并替换以下内容

import sublime
import sublime_plugin


class SelectSpecificLinesCommand(sublime_plugin.TextCommand):
    def run(self, edit, lines):
        self.view.sel().clear()
        for line in lines:
            position = self.view.text_point(int(line) - 1, 0) # internally, line numbers start from 0
            self.view.sel().add(self.view.line(position))




  • 将其保存在ST建议的文件夹中( Packages / User / ),类似于 select_lines.py (文件扩展名很重要)。

  • subl myfile.txt

  • subl-命令 select_specific_lines {\ lines\:[5,9,15]} (这种转义JSON字符串引号的方式从Windows命令提示符和Linux的Bash)

    • save it, in the folder ST recommends (Packages/User/) as something like select_lines.py (file extension is important).
    • subl myfile.txt
    • subl --command "select_specific_lines { \"lines\": [5, 9, 15] }" (this style of escaping the quotes for JSON strings works from the Windows Command Prompt and Linux's Bash)
    • 为什么我在单独的行上指定命令/调用 subl ?由于以下两个注意事项:

      Why did I specify the command on a separate line / call to subl? Because of these 2 caveats:


      1. ST必须已经在运行,否则可能无法执行命令行上指定的命令,因为插件避风港

      2. 该命令可以在文件加载之前执行。

      可以说,第2点可能仍然会通过 subl 的多次调用而发生,但希望它的可能性较小。为了更好地执行命令行命令,ST错误跟踪器上存在一个未解决的问题: https:// github .com / SublimeTextIssues / Core / issues / 1457

      Arguably, point 2 could still happen with multiple invocations of subl, but hopefully it is less likely. There is an open issue on the ST bug tracker for better command line command handling: https://github.com/SublimeTextIssues/Core/issues/1457.

      这篇关于如何打开文件并从命令行在Sublime上选择/突出显示几行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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