Vim Tips Wiki
(Remove html character entities)
(Adjust previous/next navigation)
Line 4: Line 4:
 
|id=1185
 
|id=1185
 
|previous=1183
 
|previous=1183
|next=1188
+
|next=1189
 
|created=March 24, 2006
 
|created=March 24, 2006
 
|complexity=basic
 
|complexity=basic

Revision as of 10:17, 12 April 2009

Duplicate tip

This tip is very similar to the following:

These tips need to be merged – see the merge guidelines.

Tip 1185 Printable Monobook Previous Next

created March 24, 2006 · complexity basic · author orsenthil · version 5.7


The present working directory can be displayed in Vim with:

:pwd

To change to the directory of the currently opened file:

:cd %:h

References

:help filename-modifiers

Comments

Here's a mapping to make this easier:

nmap <silent> <Leader>cd :cd %:p:h<CR>

Note the use of :p first expand the % to a full path -- it takes care of the case where you're already in the correct directory. If that happens, %:h expands to an empty string and the :cd command throws an error. %:p:h is safe as it will never send an empty directory into the :cd command.

Note that 'autochdir' can be turned on to automatically switch to the directory of the current buffer upon entering the buffer. Alternatively, try:

nmap <silent> <Leader>sp :sp <c-r>=expand('%:p:h')<CR>/

(The last / can be changed to a backslash if on Windows.) Then, hitting <Leader>sp will produce a command-line all set to allow editing of a file in the same directory as the current buffer.


Also see VimTip64 (Always set your working directory to the file you're editing)


This is very useful. Personally I have these commands defined:

command! CD cd %:p:h
command! LCD lcd %:p:h