vpillai vpillai - 2 years ago 146
Bash Question

VIM - shell working directory integration

I use cscope integrated to VIM to navidate a large code base. I can use the :E command to navigate the directory structure. But, how do I open a shell on a specific directory where I navigated to?

I came across

:cd %:p:h

Is there an easier way to do this?

Answer Source

With Vim, there are always multiple ways to suit your needs.

You could for example create a shortcut (called mapping) for the command you've found (and/or combine this with opening a shell):

:nnoremap <Leader>. :cd %:p:h<CR>

But this particular use case is so prevalent that there's actually an option for it:

:set autochdir

As an alternative, some people always open Vim from the root directory of their current project; this ensures short (and consistent!) paths, too. There are even plugins that automatically :cd to the project dir for you.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download