Opening files in the same folder as the current fi

2020-05-13 23:46发布

In vim, when I have a buffer open, I often need to load another file in the same directory that file is in, but since I don't usually cd into it, the pwd is a parent folder, so I have to retype the path every time. Is there a shortcut for this? or a way to change the pwd to the directory the file is in?

example:

cd /src
vi lib/foo/file.js

lib/foo has two files: file.js and file2.js

in vi:

:e file2.js  # doesn't work

标签: unix vim vi
12条回答
Summer. ? 凉城
2楼-- · 2020-05-14 00:07

Add this line to your vimrc configuration

autocmd BufEnter * cd %:p:h

% current file name
:p expand to full path
:h head (last path component removed)

See :help expand for further information.

查看更多
Viruses.
3楼-- · 2020-05-14 00:11

Try the following:

:e %:p:h/file2.js
查看更多
来,给爷笑一个
4楼-- · 2020-05-14 00:15

I have the following three lines on my .vimrc:

map ,e :e <C-R>=expand("%:p:h") . "/" <CR>
map ,t :tabe <C-R>=expand("%:p:h") . "/" <CR>
map ,s :split <C-R>=expand("%:p:h") . "/" <CR>

Now ,e <some-file> opens on this buffer. ,t and ,s do the same but on a new tab/split window.

查看更多
走好不送
5楼-- · 2020-05-14 00:16

Another option is to do:

:e <Tab> 

This cycles through folders in your working directory.

查看更多
相关推荐>>
6楼-- · 2020-05-14 00:17

Newer versions of vim have a autochdir command built in, if not you can fall back to a BufEnter like setup.

" set vim to chdir for each file
if exists('+autochdir')
    set autochdir
else
    autocmd BufEnter * silent! lcd %:p:h:gs/ /\\ /
endif
查看更多
我欲成王,谁敢阻挡
7楼-- · 2020-05-14 00:18

:Ex short for :Explore does exactly what you asked for.

查看更多
登录 后发表回答