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条回答
走好不送
2楼-- · 2020-05-14 00:18

A simple idea is to use the autochdir setting. Try this in your .vimrc:

set autochdir

This will change the working directory of vim to the directory of the file you opened.

Note: I'm not sure what version added this feature. I updated from vim 6 to 7.2 to get this to work.

查看更多
女痞
3楼-- · 2020-05-14 00:18

You probably know that once you're in vim, you can use

:cd lib/foo

to change into lib/foo.

On the other hand, to change into the directory of the file you're current editing, try

:cd %:p:h

You can always use

:pwd

to check your current working directory.

And of course, if you forgot what file you're editing, just hit ctrl-g.

查看更多
Ridiculous、
4楼-- · 2020-05-14 00:21

The solution with autochdir has its own pitfalls. For example you have to stay at the dir you are because Tags are defined with relative path or you need Makefile or pom.xml in the current dir.

You can use

:e <C-R>%

and then modify path by deleting name of the current file and enter new one. Or. Use the map

nnoremap <leader>e :edit <C-R>=fnamemodify(@%, ':p:h')<CR>/

And then by pressing \e or ,e (depends on leader settings) you will receive command that open directory in which your current file is and you can complete this command manually and can use ctrlD to show all files from the current file directory.

Or. You can try 0scan plugin.

Keys 0f

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

So vim has a :find command which will use the variable 'path'. If you have consistent directory structures for your code such as all source in under a directory "src" then you can just add that to the 'path' variable. Then when you do :find foo.js it will search both your current location then the folders listed in your path variable. It basically emulates the way bash or some other shell uses the PATH variable to find the appropriate binary to run.

查看更多
倾城 Initia
6楼-- · 2020-05-14 00:30

I put this in my .vimrc file, which will change the current directory to the file in the buffer. This sets the current directory for the buffer, and updates when you switch buffers.

autocmd BufEnter * lcd %:p:h
查看更多
家丑人穷心不美
7楼-- · 2020-05-14 00:30

On the "shortcut" front, have you looked into the :Sex command? This opens up the file browser in a split so that you can open other files easily. :Sex default to the directory of the current buffer. Pretty nifty feature, if I say so myself.
I bind it to ;o for easy access:

map ;o :Sex <CR>

You can also use :Ex if you want to keep it in the current buffer instead of creating a split.

查看更多
登录 后发表回答