Open file with line numbers in editor from Node.js.
The main functionality is extracted from react-dev-utils with slight modifications so that it can be used as a standalone package. The original source code is licensed under MIT.
Also added column number support.
There are also a few other existing packages with the same purpose:
However, both expects env variables like EDITOR
to be set in order to open files. This package infers the editor to open by checking current running processes before falling back to env variables.
On the other hand,react-dev-utils
includes many other utilities and dependencies and is thus not suitable for standalone usage.
const launch = require('launch-editor')
launch(
// filename:line:column
// both line and column are optional
'foo.js:12:34',
// try specific editor bin first (optional)
'code',
// callback if failed to launch (optional)
(fileName, errorMsg) => {
// log error if any
}
)
An express/connect/webpack-dev-server compatible middleware is also available:
const launchMiddleware = require('launch-editor-middleware')
app.use('/__open-in-editor', launchMiddleware())
The middleware factory function accepts the following arguments (all optional, the callback can be in any position as long as it's the last argument):
- A specific editor bin to try first. Defaults to inferring from running processes, then fallback to env variables like
EDITOR
andVISUAL
. - The root directory of source files, in case the file path is relative. Defaults to
process.cwd()
. - a callback when it fails to launch the editor.
To launch files, send requests to the server like the following:
/__open-in-editor?file=src/main.js:13:24
Value | Editor | Linux | Windows | macOS |
---|---|---|---|---|
appcode |
AppCode | ✓ | ||
atom |
Atom | ✓ | ✓ | ✓ |
atom-beta |
Atom Beta | ✓ | ||
brackets |
Brackets | ✓ | ✓ | ✓ |
clion |
Clion | ✓ | ✓ | |
code |
Visual Studio Code | ✓ | ✓ | ✓ |
code-insiders |
Visual Studio Code Insiders | ✓ | ✓ | ✓ |
codium |
VSCodium | ✓ | ✓ | ✓ |
emacs |
Emacs | ✓ | ||
idea |
IDEA | ✓ | ✓ | ✓ |
notepad++ |
Notepad++ | ✓ | ||
pycharm |
PyCharm | ✓ | ✓ | ✓ |
phpstorm |
PhpStorm | ✓ | ✓ | ✓ |
rider |
Rider | ✓ | ✓ | ✓ |
rubymine |
RubyMine | ✓ | ✓ | ✓ |
sublime |
Sublime Text | ✓ | ✓ | ✓ |
vim |
Vim | ✓ | ||
visualstudio |
Visual Studio | ✓ | ||
webstorm |
WebStorm | ✓ | ✓ | ✓ |
zed |
Zed | ✓ |
You can use the LAUNCH_EDITOR
environment variable
LAUNCH_EDITOR=codium
LAUNCH_EDITOR=my-editor-launcher.sh
# gets called with 3 args: filename, line, column
filename=$1
line=$2
column=$3
# call your editor with whatever args it expects
my-editor -l $line -c $column -f $filename