" 自动切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.