Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Can it be done?

I am editing a file, and I would like to copy it to a new folder. If I try :w ~/Documents/new_folder/new_file.txt, then I get Can't open file for writing.

So I try :!mkdir ~/Documents/new_folder, but I get the error (in a popup terminal window):

C:\Windows\system32\cmd.exe /c mkdir ~/Documents/new_folder
The syntax of the command is incorrect.
shell returned 1
Hit any key to close this window...

What am I missing? Thanks!

share|improve this question

3 Answers 3

up vote 3 down vote accepted

Windows gvim uses Windows' cmd.exe to execute :! commands and cmd.exe does not understand ~ as an alias for $HOME. Also, cmd.exe accepts only \ as a path separator, not /. Try instead

:cd ~
:!mkdir Documents\new_folder

Since :cd is a Vim internal command, Vim will correctly expand ~ itself.

[Edit: Added ! in front of mkdir (typo), changed path separator from / to \ (mistake) and added sentence about it.]

share|improve this answer
    
if I do :mkdir ..., then it doesn't know the command; if I do :!mkdir ..., then I get the same error as above. –  Richard Herron Jun 14 '11 at 16:07
    
@richardh: I fixed the answer to one that should actually work. The missing ! was a typo. Some Windows commands accept either / or `` as a path separator and I though cmd.exe was one of them. Apparently not. –  garyjohn Jun 14 '11 at 16:40
    
Thanks! In fact, you don't need to switch the directory (although that is fewer key strokes)... So my original errors were using the ~ and using the wrong tabs. –  Richard Herron Jun 14 '11 at 17:26
    
FWIW, Windows API accepts both slashes internally, it's just cmd that tries to parse /new_folder as an option switch. (It worked this way even before MS-DOS had directories.) –  grawity Jun 14 '11 at 18:51
1  
/ isn't treated as a switch character if one uses quotation marks, as in my answer. (-: –  JdeBP Jun 14 '11 at 20:32

Windows uses backslashes for directory paths. Also, Windows uses %HOMEPATH% for the user's home directory instead of the unix-like ~.

Try

:!mkdir \%HOMEPATH\%\Documents\new_folder
share|improve this answer
    
that does it! Although typing \%HOMEPATH\% and losing tab completion are no fun. Thanks! –  Richard Herron Jun 14 '11 at 16:12
    
It's technically %HOMEDRIVE%%HOMEPATH% - or %USERPROFILE% which is the same path most of the time. –  grawity Jun 14 '11 at 18:52

You're missing:

  • the C in the /C option that should have been passed to CMD. I'm presuming a transcription error on your part.
  • that ~ isn't part of the filesystem proper. It's Bourne-family/C-shell-family syntactic sugar. It doesn't exist outwith those shells (and things that emulate their filesystem name semantics).

If you want the CMD command interpreter to expand the USERPROFILE environment variable (not HOME as on Unix/Linux, note) in a command line, the correct syntax is

mkdir "%USERPROFILE%/Documents/new_folder"

Notice the quotation marks that will cope with directory names containing space characters.

Of course, in turn you'll have to escape the % characters to prevent vim from recognizing and processing them as its own peculiar command line metacharacters.

share|improve this answer
    
Thanks much! Good catch on me dropping the /c above; I'll fix. For some reason I thought that because gVim was doing tab-completion for me that Windows would take the backslashes and ~. –  Richard Herron Jun 14 '11 at 16:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.