keyboard-shortcuts - 移动到文本文件特定部分的快捷方式
问题描述
我想创建一个键盘快捷键(例如CTRL+ T),它会在出现固定文本(例如&todo
.
例子:
foo
bar
&todo
fix bug #783
blah
blah2
按CTRL+T会自动将光标移动到以 开头的行fix ...
。
目前我正在这样做:
- CTRL F
- 输入
&todo
,ENTER - ESCAPE(关闭
Search
底部面板) - HOME
- DOWN ARROW(移动到下一行)
但这需要太多的动作。
如何在单个快捷键中做到这一点?
解决方案
最好的解决方案是使用插件来做到这一点。
下面的插件可以满足您的要求。它将找到当前光标位置下方的下一个出现pattern
(即&todo
标记),将光标移动到其下方的行,并在窗口中居中该位置。如果在pattern
当前光标位置下方未找到 ,它将从缓冲区顶部再次搜索,提供环绕功能。
将以下 Python 代码复制并粘贴到缓冲区中,并将其保存在 Sublime Text 配置User
文件夹中GoToPattern.py
。
import sublime
import sublime_plugin
class GotoPatternCommand(sublime_plugin.TextCommand):
def run(self, edit, pattern):
sels = self.view.sel()
# Optional flags; see API.
flags = sublime.LITERAL | sublime.IGNORECASE
start_pos = sels[0].end() if len(sels) > 0 else 0
find_pos = self.view.find(pattern, start_pos, flags)
if not find_pos and start_pos > 0:
# Begin search again at the top of the buffer; wrap around
# feature, i.e. do not stop the search at the buffer's end.
find_pos = self.view.find(pattern, 0, flags)
if not find_pos:
sublime.status_message("'{}' not found".format(pattern))
return
sels.clear()
sels.add(find_pos.begin())
self.view.show_at_center(find_pos.begin())
row, col = self.view.rowcol(find_pos.begin())
self.view.run_command("goto_line", {"line": row + 2})
# Uncomment for: cursor to the end of the line.
# self.view.run_command("move_to", {"to": "eol"})
添加键绑定:
// The pattern arg, i.e. "&todo", can be changed to anything you want
// and other key bindings can also be added to use different patterns.
{"keys": ["???"], "command": "goto_pattern", "args": {"pattern": "&todo"}}
Default.sublime-commands
如果需要,请添加命令面板条目:
{"caption": "GoToPattern: &todo", "command": "goto_pattern", "args": {"pattern": "&todo"}},
这些链接可能对您有用ST v. 2 API和ST v. 3 API。
PS 你知道 Sublime Text 有书签吗?[以防万一你没有。]
推荐阅读
- xpages - 在服务器端处理时从扩展库显示对话框
- arrays - 二维阵列沿单轴的卷积
- oracle - 在这个简单的过程中,我的语法有什么问题?
- github - 如何在不使用 Jekyll 的情况下使 github 页面对搜索引擎可见?
- apache-spark - 当火花可以抛出内存异常时
- c++ - 如何删除这种二维数组?
- java - Android:如何在应用程序中使用外部 MySQL 数据库
- ios - 在 iOS Flutter 中构建发布时出现此错误
- android - Android - 在视图调整大小时保持 recylerview 选定的项目
- python - 使用 LXML 在 Python 中查找字符串的 XPath