How to set working/current directory in Vim?

Text EditorVim

Text Editor Problem Overview


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?

Text Editor Solutions


Solution 1 - Text Editor

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.

Solution 2 - Text Editor

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

Solution 3 - Text Editor

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

Solution 4 - Text Editor

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.

Solution 5 - Text Editor

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!

Solution 6 - Text Editor

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

autocmd BufEnter * silent! :lcd%:p:h

Solution 7 - Text Editor

Update: Now I just use those code instead of install a plugin

" 自动切nvim到当前文件所在路径, 避免leaderF每个命令前都要敲一下 :pwd.
    " 代替autochdir:  Switch to the directory of the current file unless it breaks something.
    au AG BufEnter * call AutoDiR()
    func! AutoDiR()
        " Don't mess with vim on startup.
        let should_cd = (!exists("v:vim_did_enter") || v:vim_did_enter)
                                                  "" v:vim_did_enter :
                                "     0 during startup
                                "     1 just before VimEnter.
        "  forbid for some filetypes
            " let should_cd = should_cd && david#init#find_ft_match(['help', 'dirvish', 'qf']) < 0
        " Only change to real files:
        let should_cd = should_cd && filereadable(expand("%"))
                                    " filereadable()
                                            " The result is a Number, which is |TRUE| when a file with the
                                            " name {file} exists, and can be read.
        if should_cd
            silent! cd %:p:h
        endif
    endf

    " cdh:   cd here
    nno cd :cd %:p:h<cr><cmd>pwd<cr><cmd>let g:Lf_WorkingDirectory=getcwd()<cr><cmd>echo "Lf目录:"..g:Lf_WorkingDirectory<cr>
    " nno cdh
    cnorea <expr> cdh ( getcmdtype() == ":" && getcmdline() == 'cdh') ?
                      \ 'cd %:p:h<cr>  :pwd<cr>'
                      \ :  'cdh'


    " 方法2: 不好使? 要敲一次pwd触发?
        " au AG VimEnter * set autochdir | pwd | echom '设置了autochdir'
                                " Note: When this option is on,  some plugins may not work.
    " 貌似不行:
        " Plug 'https://github.com/airblade/vim-rooter'


    " 对于vscode-nvim
        " vscode用wsl的nvim作为bin时,pwd永远是Microsoft VS Code.  等邮件通知更新吧. 其实不影响使用?
        " 我的笔记: https://github.com/vscode-neovim/vscode-neovim/issues/520#issuecomment-1013853745
        "
        "  这个也不行:
        " au AG BufEnter * silent! lcd %:p:h
                " lcd: local window cd ?
                "      Like |:cd|, but only set the current directory for the  current window.

You could install the Rooter Vim plugin, which automatically changes the working directory to the automatically-detected project root when you open a file or directory. After Rooter has set the working directory for you, if you try to edit a file like :e example.txt, Vim will edit example.txt in the project root.

If you’re using the vim-plug plugin manager, you can install Rooter by adding the following to your .vimrc, then reloading Vim and running :PlugInstall.

Plug 'airblade/vim-rooter'

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFredView Question on Stackoverflow
Solution 1 - Text EditorfalstroView Answer on Stackoverflow
Solution 2 - Text EditorDave KirbyView Answer on Stackoverflow
Solution 3 - Text EditorJohn FeminellaView Answer on Stackoverflow
Solution 4 - Text EditorChip CampbellView Answer on Stackoverflow
Solution 5 - Text EditorMcUsrView Answer on Stackoverflow
Solution 6 - Text EditorMatthias BraunView Answer on Stackoverflow
Solution 7 - Text EditorGood PenView Answer on Stackoverflow