16

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.

cmd_in_sublime3

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

Community
  • 1
  • 1
Marslo
  • 2,994
  • 4
  • 26
  • 35
  • Where did you save `cmd.py`? You might also want to check the console for errors. – skuroda Sep 05 '13 at 16:50
  • Hi @skuroda, I think I found the problem. The file folder should be `CMD` instead of `cmd` – Marslo Sep 06 '13 at 10:33
  • Possible duplicate of [Sublime Text 2 - Open CMD prompt at current or project directory (Windows)](http://stackoverflow.com/questions/12103028/sublime-text-2-open-cmd-prompt-at-current-or-project-directory-windows) – Jacob Dec 09 '16 at 20:34

5 Answers5

33

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: Open_cmd_in_Sublime

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

Community
  • 1
  • 1
Marslo
  • 2,994
  • 4
  • 26
  • 35
  • It works with `Cmd` too, and if you see there's another file by default called `User`, so it seems that it only requires that the first letter is in uppercase. – Patricio Sard Jan 06 '15 at 18:54
  • 3
    As i favor typing instead of clicking: add `[ { "caption": "Open CMD window in this folder", "command": "cmd" }, ]` to a file called `cmd.sublime-commands` in the CMD directory to be able to use the command palette. – maggie Jan 16 '17 at 07:51
  • Thanks ! It worked but can't understand the background process that's making it work! – guru_001 May 10 '17 at 10:17
  • I wouldn't use the command keys 'cmd' like that, it prevents you from entering those three letters in the editor. – kristianp Aug 05 '19 at 05:48
  • This flat out doesn't work. I think it's missing a major caveat - Python must be installed? – AllenKll Mar 16 '21 at 01:26
11

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

Ali
  • 746
  • 1
  • 7
  • 17
  • On my windows 10 machine, it uses PowerShell as default (not Command Prompt). So how can I enable Command Prompt as a default shell ? – gia huy Apr 23 '20 at 03:14
  • I have had the solution for my problem, anyone care about it could be scroll down. – gia huy Apr 23 '20 at 03:33
2

For Windows i replace the command with:

command= "cmd /K cd "+current_directory
Oleg alex
  • 99
  • 6
  • 1
    This should be: `command= "start cmd /K cd "+current_directory` otherwise it freezes sublime – oraz Apr 16 '18 at 07:03
2

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")
math2001
  • 4,167
  • 24
  • 35
0

[Bonus]

As @Ali pointed out in his solution and my comment was below.

If you want activate Command Prompt instead PowerShell, you can edit the file Preference > Package Settings > Terminal > Settings - Default :

{
  
// Replace with your own path to cmder.exe
"terminal": "C:\\Windows\\system32\\cmd.exe", // On my Windows 10 machine
"parameters": ["/START", "%CWD%"]

}

as be suggested as here

NavaneethaKrishnan
  • 1,213
  • 1
  • 9
  • 22
gia huy
  • 329
  • 1
  • 6
  • 12