Opening files in the same folder as the current file, in vim

喜欢而已 提交于 2019-12-02 17:15:42

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

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

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.

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.

Try the following:

:e %:p:h/file2.js

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.

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

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.

Steven Mastandrea

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

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.

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.

Another option is to do:

:e <Tab> 

This cycles through folders in your working directory.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!