当我使用:e命令创建一个新文件时,我不想指定整个路径,只需要新文件名即可。这可以实现吗?
如其他人所建议的,你可以使用 autochdir,它将切换到你打开的文件所在的目录,另一个选项是
:cd mydirectory
这将改变目录。它可以是绝对路径或相对路径,因此:cd ..
将向上移动一个级别。或者您可以使用:cd %:h
,它也会切换到当前文件所在的目录,但不设置autochdir。
:cd
该命令将更改目录到用户的主目录(在Windows上,它会打印当前目录)。
:cd -
这将会把目录更改为你之前访问的目录。
如果您使用 netrw 文件浏览器浏览文件系统,您可以按 c 键来设置当前目录。
注:对于较新的版本,请尝试 c
不起作用时使用 cd
。
set autochdir
命令在shell中生效'c'键
,这样你就可以在更改目录后使用!command args
命令。 - pimgeekc
更改为 cd
。 - user202729尝试在您的.vimrc
文件中添加 set autochdir
。如果您只想更改一次,请使用:cd
(或:cd!
强制更改)。
cnoremap cd. lcd %:p:h
和 nnoremap ,cd :cd %:p:h<CR>:pwd<CR>
。 - ErichBSchulz'c'
键来完成此操作。 - pimgeek使用netrw:
,除了按下c键设置当前目录外,您还可以输入:
let g:netrw_keepdir= 0
.vimrc
文件中添加以下内容,就可以使netrw
的浏览目录与当前目录相同。我不知道vim出了什么问题。我希望启动vim时使用当前目录作为默认目录。
我已经按照上面自动更改工作目录的提示,在我的.vimrc文件中设置为noautcd。
虽然我还没有开始,但是从现在开始,我将像这样启动vim:
vim —cmd 'cd `pwd`'
—
(破折号)字符,你可能想要使用 --
。 - user202729我将下面的内容添加到我的.vimrc
文件中,这将自动将Vim的工作目录设置为当前文件所在目录:
autocmd BufEnter * silent! :lcd%:p:h
set autochdir
也可以实现这个功能 :-) - Ciro Santilli OurBigBook.com" 自动切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.
:e example.txt
这样的文件,Vim将在项目根目录中编辑example.txt
。.vimrc
中,然后重新加载Vim并运行:PlugInstall
来安装Rooter插件。Plug 'airblade/vim-rooter'
这是我在使用Nvim NvChad时遇到问题的解决方案。
我尝试使用set autochdir
,但仍然无法解决问题。
所以我接下来做的事情是设置自定义映射。
M.mappings = require "custom.mappings"
添加到custom/chadrc.lua
中custom
文件夹中创建新文件mappings.lua
mappings.lua
中local M = {}
M.abc = {
n = {
["<leader>cf"] = {"<cmd>cd %:h <CR>", "Set Files Location As Dir"}
}
}
return M
<leader>cf
切换到当前文件所在目录希望这能帮到你!
%
代表当前文件名和路径,:
用于添加修改器,h
表示文件路径的头部,也就是最后一个路径分隔符之前的路径,不包括文件名本身。 - falstro