我写的脚本:
command! -nargs=? -complete=dir CD :echo escape((expand('%:h') =~ '[\\/]$' ? expand('%:h') : expand('%:h') . '/').'<args>', ' ')
目的:
:CD
-- 切换到当前文件所在目录
:CD ../some_dir
-- 切换到上层目录的兄弟目录。
上述脚本,在windows下工作的很好。但是,在linux下,遇到路径包含空格,就抓瞎了。
实验,escape('<args>', ' ') 也没有改进,总是提示输入了多个路径。
求助中
vim切换工作目录
- lilydjwg
- 论坛版主
- 帖子: 4249
- 注册时间: 2009-04-11 23:46
- 系统: Arch Linux
- 联系:
Re: vim切换工作目录
这样好了:
代码: 全选
command! -nargs=? -complete=dir CD if len(<q-args>) | exe "cd" fnameescape(<q-args>) | else | cd %:p:h | endif
-
- 帖子: 403
- 注册时间: 2007-10-27 1:04
Re: vim切换工作目录
多谢
还是基本函数不熟悉;将 escape() 换成 fnameescape() 之后, 我的脚本就能通用了!
command! -nargs=? -complete=dir CD :silent execute 'cd ' . fnameescape((expand('%:h') =~ '[\\/]$' ? expand('%:h') : expand('%:h') . '/').'<args>')
注,我想要的是,:CD 会以当前编辑文件的所在目录为基础,来切换路径; 并且 :CD<CR> 等效于 :CD .<CR>
就好象,:cd 是以当前工作目录为基础,来切换路径;
还是基本函数不熟悉;将 escape() 换成 fnameescape() 之后, 我的脚本就能通用了!
command! -nargs=? -complete=dir CD :silent execute 'cd ' . fnameescape((expand('%:h') =~ '[\\/]$' ? expand('%:h') : expand('%:h') . '/').'<args>')
注,我想要的是,:CD 会以当前编辑文件的所在目录为基础,来切换路径; 并且 :CD<CR> 等效于 :CD .<CR>
就好象,:cd 是以当前工作目录为基础,来切换路径;
- lilydjwg
- 论坛版主
- 帖子: 4249
- 注册时间: 2009-04-11 23:46
- 系统: Arch Linux
- 联系:
Re: vim切换工作目录
哦,没看清。你那行代码太多符号了 =w=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 是以当前工作目录为基础,来切换路径;