mreq

…a blog about ubuntu, sublime text and the web

Ever needed to run a command on currently editting file/folder? I've made a simple plugin to do just that.

Save the following as run_cmd.py in your Packages/User folder.

import os
import sublime
import sublime_plugin

class RunCmd(sublime_plugin.WindowCommand):
  def run(self, cmd):
    if "$file_name" in cmd:
      view = self.window.active_view()
      cmd = cmd.replace("$file_name",view.file_name())
    if "$file_dir" in cmd:
      view = self.window.active_view()
      cmd = cmd.replace("$file_dir",os.path.split(view.file_name())[0])
    print 'Running custom command:', cmd
    os.system(cmd + " &")

Now it's super easy to run commands. Strings $file_name and $file_dir are replaced with file's name or directory, respectively.

Want to run Double Commander on file's folder? Add this to your Commands.sublime-commands:

{
  "caption": "doublecmd",
  "command": "run_cmd",
  "args": {
    "cmd": "doublecmd $file_dir"
  }
}

Need to run gitk on the current file? Ok:

{
  "caption": "Gitk: Current file",
  "command": "run_cmd",
  "args": {
    "cmd": "gitk $file_name"
  }
}

Create a tmux shell with R in CWD? I think you've got the point...

{
  "caption": "rmux",
  "command": "run_cmd",
  "args": {
    "cmd": "terminator --working-directory='$file_dir' -x /usr/bin/tmux new 'R --no-save'"
  }
}