Sublime Forum

How do you execute a shell command on a file?

#1

Hi, I just downloaded the sublime text 2 trial today to check out sublime on linux after I saw a friend using it… I haven’t explored very much yet, but I was starting to look at how I could duplicate my old workflow with Geany. I think what I want can be done with a plugin, but I’m not sure, so I’ll state the problem.

I have a text file open in sublime. I’d like to hit a key and have it execute commands on the shell from that directory with my current bash profile environment.

Here are a few things I wanted to run bound to a single key:

  • perforce checkout (i.e. ‘p4 edit filename’ with the P4CONFIG env set correctly
  • ‘tidy filename’ to automatically indent an xml file in the buffer

I also heard about the Builds system… auto build on save or something? I’d be great if perforce could be told to edit (checkout) a file whenever I save the buffer… but I couldn’t get that to work with a wildcard selector like ‘*’.

Thanks!

0 Likes

#2

For (2), a build system looks like a good fit and easy enough to create:

{ "cmd": "tidy", "$file"], "selector": "text.xml" }

The command might need adjusting, but that’s about all you should need.

sublimetext.info/docs/en/core/build_systems.html
sublimetext.info/docs/en/referen … stems.html

For (1), you might succeed at it with a build system too, but you could also create a specific plugin to wrap perfoce commands.

1 Like

#3

So the behavior I’m looking for in (1) is whatever file I have open in the editor, I would like to check it out of perforce when I try to save it. (i know, perforce has an inferior workflow compared to git).

I’d like to automate checkouts. Eclipse does this easily with the p4 plugin… edit a file, it automatically checks out the code. I was also able to set this up quickly in geany by using a custom build key (Shift F9) to exec ‘export P4CONFIG=.perforce; p4 edit %f; #’. Getting this to work in sublime by contrast has been fairly difficult.

I couldn’t get the build system to work for (1) because of the selector… I would need a selector that matches everything and ‘*’ doesn’t work?

0 Likes

#4

About tidy, you will need to pass it some options, like so:

{ "cmd": "tidy", "-m", "-i", "$file"] }

That worked for me. You can do away with the “selector” item. It’s only used if you set the relevant option under Tools in the main menu.

I’m not familiar with Perforce, but I guess something along these lines would do it (assuming the p4 executable’s on your $PATH):

{ "cmd": "p4", "edit", "$file"], "env": {"P4CONFIG": ".perforce"} }

If p4 is not on your path, you can still specify the full path in “cmd” or add this to the above:

"path": "/path/to/p4:$PATH"
0 Likes

#5

Just don’t specify a selector in the build system. The selector is only used when your build system (via the Tools/Build System menu) is set to Automatic. If you explicitly choose a build system, then the selector isn’t relevant.

0 Likes