Access the HOME path in sublime text 2 command

I want to have a simple sublime command to open a specific (dot config) file in my home folder. Is there a variable or other magic that I can use, for example $ {packages}, but for the user's home folder?

I currently have (Default.sublime-commands)

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

but want to get rid of the hard coded username.

Unfortunately, I cannot find anything in the "documentation" of the sublime api.

+5
source share
1 answer

This can be done using a special command:

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)

and the following (Default.sublime commands):

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

And of course, you can extend OpenCustomFileCommand with your own replacements.

P.S. ST2, open_custom_file.py

+1

All Articles