分页: 1 / 1

vim切换工作目录

发表于 : 2013-11-26 18:32
sarrow
我写的脚本:
command! -nargs=? -complete=dir CD :echo escape((expand('%:h') =~ '[\\/]$' ? expand('%:h') : expand('%:h') . '/').'<args>', ' ')

目的:

:CD
-- 切换到当前文件所在目录
:CD ../some_dir
-- 切换到上层目录的兄弟目录。

上述脚本,在windows下工作的很好。但是,在linux下,遇到路径包含空格,就抓瞎了。

实验,escape('<args>', ' ') 也没有改进,总是提示输入了多个路径。

求助中

Re: vim切换工作目录

发表于 : 2013-11-27 0:41
lilydjwg
这样好了:

代码: 全选

command! -nargs=? -complete=dir CD if len(<q-args>) | exe "cd" fnameescape(<q-args>) | else | cd %:p:h | endif

Re: vim切换工作目录

发表于 : 2013-11-27 13:09
sarrow
多谢

还是基本函数不熟悉;将 escape() 换成 fnameescape() 之后, 我的脚本就能通用了!

command! -nargs=? -complete=dir CD :silent execute 'cd ' . fnameescape((expand('%:h') =~ '[\\/]$' ? expand('%:h') : expand('%:h') . '/').'<args>')

注,我想要的是,:CD 会以当前编辑文件的所在目录为基础,来切换路径; 并且 :CD<CR> 等效于 :CD .<CR>

就好象,:cd 是以当前工作目录为基础,来切换路径;

Re: vim切换工作目录

发表于 : 2013-11-27 13:18
lilydjwg
sarrow 写了:多谢

还是基本函数不熟悉;将 escape() 换成 fnameescape() 之后, 我的脚本就能通用了!

command! -nargs=? -complete=dir CD :silent execute 'cd ' . fnameescape((expand('%:h') =~ '[\\/]$' ? expand('%:h') : expand('%:h') . '/').'<args>')

注,我想要的是,:CD 会以当前编辑文件的所在目录为基础,来切换路径; 并且 :CD<CR> 等效于 :CD .<CR>

就好象,:cd 是以当前工作目录为基础,来切换路径;
哦,没看清。你那行代码太多符号了 =w=