Albtzrly
Albtzrly

Reputation: 944

How do I open the windows command prompt to the current directory of the active file in VIM?

So I have a file open in vim and I would like to open the command prompt to the file's current directory. Here's what I've tried...

:!start cmd /k

This will just open the command prompt to the C:\ directory even though I've set :cd to a different directory in vim.

:!start cmd /k "cd c:\%"

This fails because the % gets escaped.

:!start cmd /k "set vimcwd=%; cd C:\\%vimcwd\%"

I'm not sure why this wouldn't work, but regardless, it's more than I'd like to type. There's got to be an easier way to do this.

Upvotes: 3

Views: 1264

Answers (2)

Marslo
Marslo

Reputation: 3261

I've wrote a function to open the current directory in cmd. Just input cmd, and the commandline will show, and cd to current file directory.

function! OpenCMD()
  if has('win32') || has('win95') || has('win64')
    let tcc = 'C:\Program Files\JPSoft\TCCLE13x64\tcc.exe'
    let console2 = '!cmd /c start C:\Marslo\Tools\Software\System\CommandLine\Console2\Console.exe'

    if 'java' == &filetype
      let com = console2 . ' -d "' . expand('%:p:h') . '"'
    else
      let com = "shell"
    endif
  else
    let com = '!/usr/bin/gnome-terminal --working-directory=' . expand('%:p:h')
  endif
  let saveit = ':w!'
  echo 'Goto "' . expand('%:p:h') . '" in command line'
  silent execute saveit
  silent execute com
endfunction

command! cmd :call OpenCMD()<CR>

Upvotes: 0

Albtzrly
Albtzrly

Reputation: 944

I figured out a good way to do it.

:!start cmd /k cd %:p:h

This will open the command prompt to the directory of the current file.

I ended up adding these command to my vimrc file so I can easily open command prompt or windows explorer at the current file's directory.

"Open command prompt by running :Cp
command Cp :!start cmd /k cd %:p:h<CR>

"Open windows explorer by running :We
command We :!start Explorer /select,%:p<CR>

Upvotes: 5

Related Questions