How to set working/current directory in Vim?

前端 未结 6 945
忘掉有多难
忘掉有多难 2020-12-04 09:42

So when I want to create a new file by using the :e command I don\'t want to specify the whole path, just the new filename. Can it be done?

相关标签:
6条回答
  • 2020-12-04 10:29

    Try adding set autochdir to your .vimrc. If you want to change it just this once, use :cd (or :cd! to force it).

    0 讨论(0)
  • 2020-12-04 10:33

    I don't know what is wrong with vim. I want the directory where I start up vim as the current.

    I have followed the tip about autochd above and set that to noautcd in my .vimrc.

    I haven't done it yet, but I am about to start up vim like this from now on:

    vim —cmd 'cd `pwd`'
    

    That will make it stick to the current directory!

    0 讨论(0)
  • 2020-12-04 10:36

    Also if you are browsing the filesystem with the netrw file explorer you can set the current directory by pressing the c key.

    0 讨论(0)
  • 2020-12-04 10:37

    Adding this to my .vimrc automatically changes Vim's working dir to the current file:

    autocmd BufEnter * silent! :lcd%:p:h
    
    0 讨论(0)
  • 2020-12-04 10:40

    As already suggested, you can use autochdir, which will change to the directory of the file you opened, the other option is

    :cd mydirectory
    

    which will change the directory. This can be an absolute or relative path, so :cd .. will move up one level. Or you can use :cd %:h which will also change to the directory the current file is in, but without setting autochdir.

    :cd
    

    will change directory to your home directory (or on windows, print the current directory).

    :cd -
    

    will change the directory to the previous directory you visited.

    0 讨论(0)
  • 2020-12-04 10:48

    With netrw: in addition to pressing the c key to set the current directory, you may also put:

    let g:netrw_keepdir= 0
    

    in your .vimrc; this means that netrw will keep the browsing directory the same as the current directory.

    0 讨论(0)
提交回复
热议问题