Sublime Forum

Open with emacs/vim : feature request

#1

i want to edit certain files using emacs / vim

0 Likes

#2

Well it’s certainly possible to install those editors on your machine and use them. I believe there are variations available for most operating systems.

0 Likes

#3

It is relativly easy to create a plugin to do that. There is even already one for OSX only Open With. If you specify how you would want to open the editor (command panel/context menu/side bar) and which OS you are on, I (or someone else) can try to help you to set it up.

1 Like

#4

i am using linux manjaro(arch)-gnome

i want to open with emacs.

I want to use the context menu please ! i want it on top first option

0 Likes

#5

You can just create a plugin for that: Open Tools > Developer > New Plugin… and paste this and save:

import subprocess

import sublime
import sublime_plugin


class OpenWithEditorCommand(sublime_plugin.WindowCommand):
    def run(self, callargs=None, shell=False):
        view = self.window.active_view()
        file_name = view.file_name()
        if not callargs:
            sublime.error_message("No call arguments defined")
            return
        if not file_name:
            sublime.error_message("Save your file first!")
            return
        #
        startupinfo = None
        if sublime.platform() == 'windows':
            startupinfo = subprocess.STARTUPINFO()
            startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
        #
        row, col = view.rowcol(view.sel()[0].b)
        #
        kwargs = {
            "file_name": file_name,
            "line": str(row + 1),
            "column": str(col),
        }
        #
        if isinstance(callargs, list):
            callargs = [sublime.expand_variables(c, kwargs) for c in callargs]
        else:
            callargs = sublime.expand_variables(callargs, kwargs)
        #
        print("callargs:", callargs)
        subprocess.Popen(callargs, shell=shell, startupinfo=startupinfo)

Now you have a command to open an arbitrary editor with the current file. Next we need to create the context menu, for this reason we select Preferences > Browse Packages… and open the User folder. Inside that folder create a file Context.sublime-menu with the content (you may adapt the commands to your preferences):

[
    {
        "caption": "Open With Emacs",
        "command": "open_with_editor",
        "args": {
            "callargs": ["emacs", "+$line:$column", "$file_name"]
        },
    },
    {
        "caption": "Open With Vim",
        "command": "open_with_editor",
        "args": {
            "callargs": ["vim", "$file_name", "-g", "+$line"],
            "shell": true
        },
    },
]

Since this does not show up on top you may install PackageResourceViewer and integrate the context menu into the Default/Context.sublime-menu file, because that is the file, which is loaded first.

4 Likes

#6

@r-stein Great post and also a good basic structure for a plugin. If you don’t mind I will use it as well and probably enhance it a bit and might put it on PackageControl.io as well.

I still have a question: Why do you have the line included? Any special reason for that?

Your python code looks pretty clean. I also made one plugin as a debut: https://packagecontrol.io/packages/Yet%20Another%20Launcher

As a coding and ST beginner I guess the python code is unneccessary clumsy. Anyway, it works for me.

0 Likes

#7

@dhbn I am fine with you using and publishing it under any license.

I have included the current line and column such that the editor can jump to the piece of code you are currently editing. (And I added # to the start of every indented line, because of the code formatter of the forum [it did handle such cases bad the last time I tried without it])

2 Likes

#8

Thank You @r-stein

0 Likes