Skip to content

lahmatiy/open-in-editor

Repository files navigation

NPM version

NPM package to open a file in editor.

Supported editors:

You also can use any other editor that is able to open files from command line.

Installation

npm install open-in-editor

Usage

First of all you should create an interface with your settings.

varopenInEditor=require('open-in-editor');
vareditor=openInEditor.configure({
// options
},function(err){
console.error('Something went wrong: '+err);
});

Resulting object has a single methodopen.This method runs terminal command that opens an editor. Result of this method is a promise:

editor.open('path/to/file.js:3:10')
.then(function(){
console.log('Success!');
},function(err){
console.error('Something went wrong: '+err);
});

API

openInEditor.configure([options][, failCallback]);

Arguments:

  • optionsoptionalis used to set up a command to launch an editor. If no options set it will try to get the command fromenvironment
  • failCallbackoptionalfunction that is called when something's wrong with editor setup.

If editor setup was successfulconfiguremethod returns an interface with single methodopen.The method accepts file reference with the following format:filename[:line[:column]],wherelineandcolumntell the editor where to place cursor when file is opened.

Options

editor

Type:Stringornull
Values:'sublime','atom','code','webstorm','phpstorm','idea14ce','vim','emacs','visualstudio'
Default:null

Editor to open a file. Once value is set, we try to detect a command to launch an editor.

Supported editors:

  • sublime– Sublime Text
  • atom– Atom Editor
  • code– Visual Studio Code
  • webstorm– WebStorm
  • phpstorm- PhpStorm
  • idea14ce– IDEA 14 CE
  • vim– Vim (via Terminal, Mac OS only)
  • emacs– Emacs (via Terminal, Mac OS only)
  • visualstudio– Visual Studio

cmd

Type:Stringornull
Default:null

Command to launch an editor.

varopenInEditor=require('open-in-editor');
vareditor=openInEditor.configure({
cmd:'/path/to/editor/app'
});

Ifeditoroption is also set, an editor settings are using as default settings.

varopenInEditor=require('open-in-editor');
vareditor=openInEditor.configure({
editor:'code',
cmd:'/path/to/editor/app'// will be called as '/path/to/editor/app -r -g {filename}:{line}:{column}'
});

pattern

Type:Stringornull
Default:null

Option to specify arguments for a command. Pattern can contain placeholders to be replaced by actual values. Supported placeholders:filename,lineandcolumn.

varopenInEditor=require('open-in-editor');
vareditor=openInEditor.configure({
cmd:'code',
pattern:'-r -g {filename}:{line}:{column}'
});

If there's no{filename}placeholder in the command then{filename}:{line}:{column}is appended. That way previous example can be simplified:

varopenInEditor=require('open-in-editor');
vareditor=openInEditor.configure({
cmd:'code',
pattern:'-r -g'// the same as '-r -g {filename}:{line}:{column}'
});

line

Type:Number
Default:1

Defines the number of the first line in the editor. Usually it's1,but you can set it to0.

column

Type:Number
Default:1

Defines the number of the first column in the editor. Usually it's1,but you can set it to0.

Environment

If noeditororcmdoption is specified, we try to get the command to launch an editor using environment settings. Following values can be used (in descending priority):

  • process.env.OPEN_FILE
  • process.env.VISUAL
  • process.env.EDITOR

First value found is used. If it'sprocess.env.VISUALorprocess.env.EDITOR,it's used directly ascmdoption. Butprocess.env.OPEN_FILEis different: if value is a valid foreditoroption, it's used for it, otherwise it's used as a value forcmdoption.

You can set env settings per command:

OPEN_FILE=atom oe path/to/file.js:4:15
OPEN_FILE= "code -r -g" node script.js

CLI

Package could be installed globally.

npm install open-in-editor -g

In this caseoecommand will be available in terminal.

Usage:

oe [filename] [options]

Options:

--cmd <command> Command to open file
--debug Debug errors
-e, --editor <editor> Editor: atom, code, sublime, webstorm, phpstorm, idea14ce, vim, visualstudio, emacs
-f, --file <filename> File to open
-h, --help Output usage information
-p, --pattern <pattern> Filename pattern and args, i.e. something going after cmd
-v, --version Output the version

Related projects

  • express-open-in-editorExpressextension to open files from browser.
  • babel-plugin-source-wrapperBabelplugin that instruments source code to associate objects with location they defined in code base.
  • Component Inspector– developer tool to inspect components that can open component creation source location in editor. Has integrations forReact,Backboneand can be adopted for other frameworks.

License

MIT