Skip to content

Instantly share code, notes, and snippets.

@derdesign
Created March 21, 2013 03:07
Show Gist options
  • Save derdesign/5210400 to your computer and use it in GitHub Desktop.
Save derdesign/5210400 to your computer and use it in GitHub Desktop.
Create vim project files that open automatically with MacVim.
#!/usr/bin/env node
var fs = require('fs');
var util = require('util');
var pathmod = require('path');
var EXTENSION = 'vimproj';
var EXT_REGEX = new RegExp('\\.' + EXTENSION + '$');
var TEMPLATE = '\
#!/usr/bin/env node\n\
process.chdir("{PATH}");\n\
require("child_process").spawn("mvim", ["."]);\n\
process.exit();';
function main() {
var args = process.argv.slice(2);
switch (args.length) {
case 2:
var cwd = process.cwd();
var path = pathmod.resolve(cwd, args[0]);
var proj, projfile, target = pathmod.resolve(cwd, args[1]);
// Get project file and target path
if (EXT_REGEX.test(target)) {
proj = pathmod.dirname(target);
projfile = target;
} else {
proj = target;
projfile = util.format("%s/%s.%s", proj, pathmod.basename(path), EXTENSION);
}
// Bail if project path doesn't exist
if (!fs.existsSync(path)) {
console.log("Project path does not exist");
process.exit();
}
// Bail if target path does not exist
if (!fs.existsSync(proj)) {
console.log("Output path does not exist");
process.exit();
}
// Bail if output project file exists
if (fs.existsSync(projfile)) {
console.log("Project file already exists: " + projfile);
process.exit();
}
var buf = TEMPLATE.replace("{PATH}", path);
console.log(util.format("Writing %s", projfile));
fs.writeFileSync(projfile, buf, 'utf8');
fs.chmodSync(projfile, 0755);
break;
default:
console.log("\nUsage: vimproject <path> <output-path>\n");
process.exit();
break;
}
}
main();
@derdesign
Copy link
Author

Installation Instructions

  1. Copy the script to /usr/local/bin or any directory on your $PATH
  2. Open Terminal.app and on the Seetings page, set the Shell exit behaviour to "Close if the shell exited cleanly"

After you create your first project, right click on it and set it to open with Terminal.app. This will automatically open MacVim when you double click on the directories you specify.

Usage:

The following creates project.vimproj on the current directory

vimproject /path/to/your/project .

The following creates myproject.vimproj:

vimproject /path/to/your/project /output/myproject.vimproj

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment