在sublime text 2命令中访问HOME路径

时间:2012-08-29 09:11:36

标签: sublimetext2

我希望有一个简单的sublime命令来打开我的主文件夹中的特定(点配置)文件。我可以使用变量或其他魔法,例如$ {packages},但对于用户的主文件夹?

目前我有(Default.sublime-commands)

{
    "caption": "Edit my config",
    "command": "open_file",
    "args": {
        "file": "/Users/MyName/.myconfig"
    }
}

但想要摆脱硬编码的用户名。

不幸的是,我无法在sublime的api“文档”中找到任何内容。

1 个答案:

答案 0 :(得分:1)

可以使用这样的自定义命令来完成:

import sublime_plugin, getpass

class OpenCustomFileCommand(sublime_plugin.WindowCommand):
  def run(self, file_name):
    if("{username}" in file_name):
      file_name = file_name.replace("{username}", getpass.getuser())
    self.window.open_file(file_name)

和以下(Default.sublime-commands):

{
  "caption": "Edit my config",
  "command": "open_custom_file",
  "args": { "file_name": "/Users/{username}/.myconfig" }
}

当然,您可以使用自己的替换扩展 OpenCustomFileCommand。

P.S。命令必须存储在ST2的Packages目录中,即文件 open_custom_file.py