How can I open command line prompt from Sublime in windows7

孤人 提交于 2019-12-31 09:04:12

问题


I'v created a function in VIM named OpenCMD(), it used for open command line or terminal in VIM (And cd in the current file path)

func! OpenCMD()
    if has('win32')
        let com = '!cmd /c start cd '. expand('%:p:h')
    else
        let com = '!/usr/bin/gnome-terminal --working-directory=' . expand('%:p:h')
    endif
    silent execute com
endfunc
nmap cmd :call OpenCMD()

Now, I want to open command line and cd in the current file path in Sublime (sublime 3 beta). The function as the same as the OpenCMD().

And I searched an question in stackover flow: Sublime Text 2 - Open CMD prompt at current or project directory (Windows)

I did as the the first guy answered (Create cmd, cmd.py and Context.sublime-menu). But it cannot work, the cmd operation always disabled.

Is there any way can get it? Thanks in advance!


回答1:


The answer about Sublime Text 2 - Open CMD prompt at current or project directory (Windows) is nearly correct.

Only one step (for me) has to be changed is the file name should be uppercase. Use CMD instead of cmd.


My steps (Win7):

  • Open folder %APPDATA%\Sublime Text 3\Packages or just click Preferences -> Browser Packages.. in sublime-text-3 Beta
  • Create a folder named CMD (Uppercase). The path of CMD should be %APPDATA%\Sublime Text 3\Packages\CMD.
  • Open the folder CMD, and create a file, named cmd.py (lowercase), paste the context as below:
import os, sublime_plugin
class CmdCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        file_name=self.view.file_name()
        path=file_name.split("\\")
        current_driver=path[0]
        path.pop()
        current_directory="\\".join(path)
        command= "cd "+current_directory+" & "+current_driver+" & start cmd"
        os.system(command)
  • Create a file (again), named Context.sublime-menu. Add context as below:
[
     { "command": "cmd" }
]
  • The Cmd function will work in context menu (right-click). For example:

Of cause, if you want to open command line by command (by 'cmd' for example), you can add the following context into Default (Windows).sublime-keymap file. :

{ "keys": ["c", "m", "d"], "command": "cmd"}

You can open it from Preferences -> Key Bindings - User




回答2:


You can install Terminal package in Sublime text 3 using the following steps.

  1. Click on Package Control in Preferences.
  2. Select Install Package.
  3. Search "Terminal" in the packages list then install it.

Now when you right click on a file or folder you will see Open Terminal Here option




回答3:


For Windows i replace the command with:

command= "cmd /K cd "+current_directory



回答4:


Thank you so much @Marslo! But, I think we can improve the plugin a bit... (i"m on st3 beta, window 8)

import os
import sublime_plugin

class CmdCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        os.system("cd " + self.view.file_name() + " & start cmd")


来源:https://stackoverflow.com/questions/18606682/how-can-i-open-command-line-prompt-from-sublime-in-windows7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!