Take the 2-minute tour ×

I want to set it so that when I write a text file and save it - it is saved to a default folder called TEXT which will be in my main Vim folder eg. C:\Program Files\Vim\vim73\TEXT

at the moment they are saved by default in the vim73 folder mixed in with everything else. so if I type :W^M the file gets saved there and I want it to go to the folder named TEXT

share|improve this question
add comment

4 Answers

up vote 6 down vote accepted

When you save a file vim will default it to your current working directory. You can use the command :pwd to verify this. To change it you can use :cd SomeDirectoryPath.

You could also add the cd command to your .vimrc (or the equivalent for windows) to automatically change your current directory every time you start vim.

share|improve this answer
thanks this is what I have done, adding it to my vimrc, works fine –  elwoode Dec 6 '10 at 9:00
add comment

Another possible approach would be to intercept the writing process with an autocmd for a writing event, probably BufWriteCmd. Have the autocmd function check to see if the file has a .txt extension (or whatever you use) and bypass the normal write process to save however/wherever you want. For docs see:

:h BufWriteCmd

Here's some code you could put in vimrc, not thoroughly tested to make sure behavior is exactly what you'd want, but it does basically work:

function! WriteTextFile()
    execute 'write! c:\text\'.expand("%:p:t")
    set nomodified
au BufWriteCmd *.txt call WriteTextFile()
share|improve this answer
I have similar autocmds using 'bufwritepost' –  Wesley Rice Dec 4 '10 at 22:48
@without: That would work. Difference is code I gave saves only in the desired directory. If this were using BufWritePost then it would save the text files to the desired directory in addtion to the getcwd directory. I think that's also the case if it were simply in BufWrite, but not positive. –  Herbert Sitz Dec 4 '10 at 23:14
add comment

As GWW mentioned, you can add a cd command to the _vimrc file. I found the proper format to use difficult to find. The path must not be enclosed in quotes, spaces must be escaped with a backslash, backslashes should not be escaped, and the path must end in a backslash. Here is an example:

cd C:\Documents\ and\ Settings\someUser\

I hope this helps.

share|improve this answer
add comment

You can use abbrevations, if executed :w it's automatically changes to the text below, I think that might help you.

cnorea w w! "C:\Program Files\Vim\vim73\TEXT"
share|improve this answer
What a bad mapping! w is such a useful move command… –  Benoit Dec 4 '10 at 20:02
@Benoit, I mean the :w save command, not the movemenet command. Your'e right, I've just fixed it. –  Fatih Arslan Dec 4 '10 at 20:07
still, doing that, you can no more use the following commands: :wn :wall :wq :win :windo :winc :wp. So, I suggest using abbrevs instead of mappings, trigger the abbreviation in command edition only (c), and do not trigger other abbreviations (nore) with cnoreabbrev w w! "C:... –  Benoit Dec 4 '10 at 20:49
Is this correct now? Didn't know that a map applies to other commands to. I've fixed it again. Thanks –  Fatih Arslan Dec 4 '10 at 21:01
I suggest using <expr> abbreviation: cnoreabbrev <expr> w ((getcmdline()==#'w')?('w! C:\\Program\ Files\\Vim\\vim73\\TEXT'):('w')). This will forbid expanding w if it is not at the start of the command line. Note that .../TEXT is a directory, so you do need something after TEXT. Also note that w! "C:..." is just the same as w! because " starts comment. So do not forget to test your answers, -1 for not doing this! –  ZyX Dec 4 '10 at 21:52
show 3 more comments

Your Answer


