sponsor Vim development Vim logo Vim Book Ad

autocwd.vim : Auto current working directory update system

 script karma  Rating 0/0, Downloaded by 81    Comments, bugs, improvements  Vim wiki

created by
yssl
 
script type
utility
 
description
# autocwd.vim

autocwd.vim automatically changes the current working directory (CWD) of vim
when you change the current buffer (or window).
You can define patterns that may be included in a file path or buffer name,
and corresponding working directories in your .vimrc.

This plugin requires a version of vim with python support.
You can check your vim with `:echo has('python')`.

Screenshot:
https://cloud.githubusercontent.com/assets/5915359/3622432/de3ce5c8-0e33-11e4-8a78-ff5d8bc33d92.png

## Usage

You can define patterns and working directories in your .vimrc as follows:

```
let g:autocwd_patternwd_pairs = [
\[pattern1, working_directory1],
\[pattern2, working_directory2],
...
\]
```

- pattern is a substring of a file path or buffer name with Unix shell-style wildcards.  
For example, '\*.vim' matches files with .vim extension and '*/project1/*' matches files
that contains '/project1/' in their absolute file paths.  
(Please refer https://docs.python.org/2/library/fnmatch.html for more information.
patterns are processed by python's fnmatch function internally.)

- working_directory will be the CWD when the corresponding pattern matches the c
urrent file path or buffer name.  
It can be one of following types:

type                                         | example: working_directory | example: CWD to be changed
---                                             | ---                                              | ---
absolute path                         | '~/test'                                      | ~/test
vim's file name modifier (1) | '%:p:h'                                      | current file's directory
special keyword: '*REPO*'  | '*REPO*/bin'                           | ~/code/bin (2)

(1) Please refer http://vimdoc.sourceforge.net/htmldoc/cmdline.html#filename-modifiers for more information.  
(2) '*REPO*' is replaced with the repository root directory that contains current file.  
The example directory structure is:
```
+-- ~/code
| +-- .git
| +-- bin
| +-- examples
| | +-- ex1
| | | +-- current file
```

- If the current buffer matches one of the defined patterns, the CWD will
be changed to the corresponding working directory.
Otherwise, the default working directory that have been the CWD before
applying `g:autocwd_patternwd_pairs` will be restored.  
You can change the default working directory by `:cd` or other CWD-changing
commands (e.g., 'cd' of the NERDTree) when the current buffer does not
match any of predefined patterns.

- The order of patterns in `g:autocwd_patternwd_pairs` is meaningful.
If the current buffer matches both first and second patterns, the working
directory corresponding to the first pattern will be the CWD.


## Commands

**:AutoCWDPrint**  
Print the buffer name or file path, matched pattern, and working directory of windows in the current tab.

There is no activation commands for autocwd.vim.
If you install this plugin, it will starts to manage the CWD.

## Motivation

It is quite useful to set the CWD for each opened file in vim.
Vim provides `:lcd` command for this purpose.
However, it cannot deal with opening other files in the same window because `:lcd` is applied to a specific window, not buffer.  

autocwd.vim is designed to solve this problem.
Moreover, it provides more convenient way to set CWDs with Unix shell-style patterns.
 
install details
 

rate this script Life Changing Helpful Unfulfilling 
script versions (upload new version)

Click on the package to download.

package script version date Vim version user release notes
autocwd.vim.zip 1.2.0 2014-10-08 7.0 yssl     - add special keyword '*REPO*' that can be used in predefined working_directory.
    now you can set working directory to relative path from the repository directory.
autocwd.zip 1.1.0 2014-08-07 7.0 yssl change the name to autocmd.vim
CWDPattern.zip 1.0.0 2014-06-24 7.0 yssl Initial upload
ip used for rating: 54.211.212.174

If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
   
SourceForge.net Logo