Home Download Buy Blog Forum Support

How do you execute a shell command on a file?

How do you execute a shell command on a file?

Postby coldnebo on Fri Jul 01, 2011 9:56 pm

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!
coldnebo
 
Posts: 10
Joined: Fri Jul 01, 2011 9:11 pm

Re: How do you execute a shell command on a file?

Postby guillermooo on Sat Jul 02, 2011 7:16 am

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

Code: Select all
{
    "cmd": ["tidy", "$file"],
    "selector": "text.xml"
}


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

http://sublimetext.info/docs/en/core/build_systems.html
http://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.
guillermooo
 
Posts: 729
Joined: Thu Jul 23, 2009 9:06 am

Re: How do you execute a shell command on a file?

Postby coldnebo on Tue Jul 05, 2011 6:56 pm

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?
coldnebo
 
Posts: 10
Joined: Fri Jul 01, 2011 9:11 pm

Re: How do you execute a shell command on a file?

Postby guillermooo on Tue Jul 05, 2011 7:26 pm

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

Code: Select all
{
   "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):

Code: Select all
{
   "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:

Code: Select all
"path": "/path/to/p4:$PATH"
guillermooo
 
Posts: 729
Joined: Thu Jul 23, 2009 9:06 am

Re: How do you execute a shell command on a file?

Postby jps on Wed Jul 06, 2011 8:15 am

coldnebo wrote: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?


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.
jps
Site Admin
 
Posts: 3071
Joined: Wed Mar 19, 2008 12:33 pm


Return to Technical Support

Who is online

Users browsing this forum: No registered users and 27 guests