Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing HOME path in sublime text 2 command

Tags:

sublimetext2

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 I can use like ${packages}, but for the user's home folder?

Currently I 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 user name.

Unfortunately I can't find anything in the api "documentation" of sublime.

like image 477
Karsten S. Avatar asked Nov 13 '22 23:11

Karsten S.


1 Answers

It can be done using custom command like this:

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. Command must be stored within Packages directory of ST2, i.e. in file open_custom_file.py

like image 158
overlord Avatar answered Dec 09 '22 07:12

overlord