1 *project.txt* Plugin for managing multiple projects with multiple sources
2 For Vim version 6.x and Vim version 7.x.
3 Last Change: Fri 13 Oct 2006 10:20:13 AM EDT
7 aricvim email-at-sign charter.net
9 *project* *project-plugin*
12 Commands...................|project-invoking|
13 Inheritance.............|project-inheritance|
14 Mappings...................|project-mappings|
15 Adding Mappings.....|project-adding-mappings|
16 Settings...................|project-settings|
17 Example File................|project-example|
18 Tips...........................|project-tips|
21 You can use this plugin's basic functionality to set up a list of
22 frequently-accessed files for easy navigation. The list of files will be
23 displayed in a window on the left side of the Vim window, and you can press
24 <Return> or double-click on filenames in the list to open the files. I find
25 this easier to use than having to navigate a directory hierarchy with the
28 You can also instruct the Plugin to change to a directory and to run Vim
29 scripts when you select a file. These scripts can, for example, modify the
30 environment to include compilers in $PATH. This makes it very easy to use
31 quickfix with multiple projects that use different environments.
33 Other features include:
34 o Loading/Unloading all the files in a Project (\l, \L, \w, and \W)
35 o Grepping all the files in a Project (\g and \G)
36 o Running a user-specified script on a file (can be used to launch an
37 external program on the file) (\1 through \9)
38 o Running a user-specified script on all the files in a Project
40 o High degree of user-configurability
41 o Also works with |netrw| using the XXXX://... notation where XXXX is
42 ftp, rcp, scp, or http.
44 All of this is specified within a simple text file and a few global variables
47 You must set 'nocompatible' in your |vimrc| file to use this plugin. You can
48 stop the plugin from being loaded by setting the "loaded_project" variable: >
49 :let loaded_project = 1
52 ==============================================================================
53 COMMANDS *project-invoking*
55 You can use the plugin by placing it in your plugin directory (e.g.,
56 ~/.vim/plugin). See |add-global-plugin|. When you start vim the next time, you
57 then enter the command >
62 If you do not specify the filename, $HOME/.vimprojects is used.
64 To have Vim come up with the Project Window enabled automatically (say, from a
65 GUI launcher), run Vim like this: [g]vim +Project
67 Note that you can invoke :Project on only one file at a time. If you wish to
68 change the Project File, do a :bwipe in the Project Buffer, then re-invoke the
69 Plugin as described above.
71 Several Projects can be kept and displayed in the same file, each in a fold
72 delimited by { and } (see |fold.txt|). There can be any number of nested
73 folds to provide you with a Project hierarchy. Any line without a { or a } in
74 the file is considered to be a filename. Blank lines are ignored, and any
75 text after a # is ignored.
77 Because the plugin uses standard Vim folds, you can use any of the
78 |fold-commands|. You can double-click on the first line of a fold to open and
79 close it. You can select a file to open by putting the cursor on its name and
80 pressing <Return> or by double-clicking on it. The plugin will create a new
81 window to the right or use the |CTRL-W_p| equivalent if it exists.
84 Each Project Entry has this form:
87 <Description>={projpath} [{options}] {
92 {options} is one or more of the following (on the same line):
99 Note that a project_entry can reside within a project_entry. This allows you
100 to set up a hierarchy within your Project.
102 The <Description> will be displayed in the foldtext and cannot contain "=".
103 There can be no space character directly on either side of the =.
105 The {projpath} is the path in which the files listed in the Project's fold
106 will be found, and it may contain environment variables. If the path is a
107 relative path, then the plugin constructs the whole path from the Project's
108 parent, grandparent, etc., all the way up the hierarchy. An outermost
109 project_entry must have an absolute path. See the |project-inheritance|
110 example below. {projpath} may contain spaces, but they must be escaped like
111 normal Vim escapes. Here are two examples of the same directory:
113 Example=/my/directory/with\ spaces {
115 Example="/my/directory/with spaces" {
118 I recommend this for Windows®: >
120 Example="c:\My Documents" {
123 But Vim is smart enough to do this, too: >
125 Example=c:\My\ Documents {
128 CD= provides the directory that Vim will change to when you select a file in
129 that fold (using |:cd|). This allows you, for example, to enter |:make| to use
130 the local Makefile. A CD=. means that Vim will make {projpath} or its
131 inherited equivalent the current working directory. When CD is omitted, the
132 directory is not changed. There can be no space on either side of the =. The
133 value of CD can also be a relative path from a parent's CD. See the
134 |project-inheritance| example below. This directive is ignored for |netrw|
135 projects. Spaces are allowed in the path as for {projpath}.
137 in= and out= provide the means to run arbitrary Vim scripts whenever you enter
138 or leave a file's buffer (see the |BufEnter| and |BufLeave| autocommand
139 events). The idea is to have a Vim script that sets up or tears down the
140 environment for the Project like this:
143 let $PROJECT_HOME='~/my_project'
144 " Put the compiler in $PATH
145 if $PATH !~ '/path/to/my/compiler'
146 let $PATH=$PATH.':/path/to/my/compiler'
150 " Remove compiler from $PATH
151 if $PATH =~ '/path/to/my/compiler'
152 let $PATH=substitute($PATH, ':/path/to/my/compiler', '', 'g')
155 Then you can use :make with the proper environment depending on what file you
156 are currently editing. If the path to the script is relative, then it is
157 relative from {projpath}. These directives are inherited by Subprojects
158 unless the Subproject specifies its own. For use with |netrw| projects, the
159 paths specified for in= and out= must be absolute and local.
161 filter= specifies a |glob()| file pattern. It is used to regenerate the list
162 of files in a Project fold when using the \r (<LocalLeader>r) map in the
163 Project Window. The filter value must be in quotes because it can contain
164 multiple file patterns. If filter is omitted, then the * pattern is used.
165 There can be no space on either side of the =. A Subproject will inherit the
166 filter of its parent unless it specifies its own filter.
168 flags= provides the means to enable/disable features for a particular fold.
169 The general mnemonic scheme is for lower case to turn something off and upper
170 case to turn something on. {flag} can contain any of the following
175 l Turn off recursion for this fold for \L. Subfolds are also
176 blocked from the recursion.
178 r Turn off refresh. When present, do not refresh this fold when
179 \r or \R is used. This does not affect subfold recursion.
181 S Turn on sorting for refresh and create.
183 s Turn off sorting for refresh and create.
185 T Turn on top gravity. Forces folds to the top of the current
186 fold when refreshing. It has the same affect as the 'T' flag
187 in g:proj_flags, but controls the feature on a per-fold basis.
189 t Turn off top gravity. Forces folds to the bottom of the
190 current fold when refreshing.
192 w Turn off recursion for this fold for \W. Subfolds are also
193 blocked from the recursion.
196 Flags are not inherited by Subprojects.
198 Any text outside a fold is ignored.
201 ==============================================================================
202 INHERITANCE *project-inheritance*
204 It's best to show inheritance by comparing these two Project Files:
206 Parent=~/my_project CD=. filter="Make* *.mk" flags=r {
209 Child2=include CD=. filter="*.h" {
213 Child1's path is "~/my_project/c_code" because ~/my_project is inherited. It
214 also inherits the CD from Parent. Since Parent has CD=., the Parent's cwd is
215 "~/my_project". Child1 therefore inherits a CD of "~/my_project". Finally,
216 Child1 inherits the filter from Parent. The flags are not inherited.
218 Child2 only inherits the "~/my_project" from Parent.
220 Thus, the example above is exactly equivalent to this:
222 Parent=~/my_project CD=. filter="Make* *.mk" flags=r {
223 Child1=~/my_project/c_code CD=~/my_project filter="Make* *.mk" {
225 Child2=~/my_project/include CD=~/my_project/include filter="*.h" {
229 (For a real Project, Child1 would not want to inherit its parent's filter, but
230 this example shows the concept.) You can always enter \i to display what the
231 cursor's project inherits.
234 ==============================================================================
235 MAPPINGS *project-mappings*
239 \r Refreshes the Project fold that the cursor is in by placing in the
240 fold all the files that match the filter. The Project is refreshed
241 using an indent of one space for every foldlevel in the hierarchy.
243 You may place a "# pragma keep" (without the quotes) at the end of a
244 line, and the file entry on that line will not be removed when you
245 refresh. This is useful, for example, when you have . as an entry so
246 you can easily browse the directory.
248 Note that this mapping is actually <LocalLeader>r, and the default of
249 |<LocalLeader>| is \.
251 This does not work for Projects using |netrw|.
253 \R Executes \r recursively in the current fold and all folds below.
254 This does not work for Projects using |netrw|.
256 \c Creates a Project fold entry. It asks for the description, the path
257 to the files, the CD parameter, and the filename |glob()| pattern.
258 From this information, it will create the Project Entry below the
261 This does not work for Projects using |netrw|.
263 \C Creates a Project fold entry like \c, but recursively includes all the
267 Select a file to open in the |CTRL-W_p| window or in a new window. If
268 the cursor is on a fold, open or close it.
272 Same as <Return> but horizontally split the target window.
273 <LocalLeader>s is provided for those terminals that don't recognize
277 Load all files in a project by doing horizontal splits.
281 Same as <Return> but ensure that the opened file is the only other
282 window. <LocalLeader>o is provided for those terminals that don't
283 recognize <C-Return>.
287 Same as <Return> but only display the file--the cursor stays in the
291 (Double-click) If on a closed fold, open it. If on an open fold
292 boundary, close it. If on a filename, open the file in the |CTRL-W_p|
293 window or in a new window.
302 Increase the width of the Project Window by g:proj_window_increment or
303 toggle between a width of
304 g:proj_window_width + g:proj_window_increment
308 Whether you toggle or monotonically increase the width is determined
309 by the 't' flag of the g:proj_flags variable (see |project-flags|).
311 Note that a Right Mouse click will not automatically place the cursor
312 in the Project Window if it is in a different window. The window will
313 go back to the g:proj_window_width width when you leave the window.
315 <space> Same as <RightMouse>
319 Move the text or fold under the cursor up one row. This may not work
320 in a terminal because the terminal is unaware of this key combination.
321 <LocalLeader><Up> is provided for those terminals that don't recognize
327 Move the text or fold under the cursor down one row. This may not work
328 in a terminal because the terminal is unaware of this key combination.
329 <LocalLeader><Down> is provided for those terminals that don't
332 \i Show in the status line the completely resolved and inherited
333 parameters for the fold the cursor is in. This is intended for
334 debugging your relative path and inherited parameters for manually
337 \I Show in the status line the completely resolved filename. Uses the
338 Project_GetFname(line('.')) function.
341 Run the command specified in g:proj_run{x} where {x} is the number
342 of the key. See the documentation of g:proj_run1 below.
345 Run the command specified in g:proj_run_fold{x} where {x} is the
346 number of the key. The command is run on the files at the current
347 Project level. See the |project-settings| below.
350 Run the command specified in g:proj_run_fold{x} where {x} is the
351 number of the key. The command is run on the files at the current
352 Project level and all Subprojects. See the |project-settings| below.
354 \0 Display the commands that are defined for \1 through \9.
356 \f0 Display the commands that are defined for \f1 through \f9 and \F1
357 through \F0. Same as \F0.
359 \l Load all the files in the current Project level into Vim. While files
360 are being loaded, you may press any key to stop.
362 \L Load all the files in the current Project and all Subprojects into
363 Vim. Use this mapping with caution--I wouldn't suggest using \L to
364 load a Project with thousands of files. (BTW, my Project file has more
365 than 5,300 files in it!) While files are being loaded, you may press
368 \w Wipe all the files in the current Project level from Vim. (If files
369 are modified, they will be saved first.) While files are being wiped,
370 you may press any key to stop.
372 \W Wipe all the files in the current Project and all Subprojects from
373 Vim. (If files are modified, they will be saved first.) While files
374 are being wiped, you may press any key to stop.
376 \g Grep all the files in the current Project level.
378 \G Grep all the files in the current Project level and all Subprojects.
380 \e Set up the Environment for the Project File as though you had selected
381 it with <Return>. This allows you to do a \e and a :make without
382 having to open any files in the project.
384 \E Explore (using |file-explorer|) the directory of the project the
385 cursor is in. Does not work with netrw.
387 <F12> When the 'g' flag is present in g:proj_flags (see |project-flags|)
388 this key toggles the Project Window open and closed. You may remap
389 this toggle function by putting the following in your vimrc and
390 replacing <Leader>P with whatever key combination you wish:
392 nmap <silent> <Leader>P <Plug>ToggleProject
394 Note that the Project Plugin remaps :help because the Help Window and the
395 Project Window get into a fight over placement. The mapping avoids the
398 ==============================================================================
399 ADDING MAPPINGS *project-adding-mappings*
401 You can add your own mappings or change the mappings of the plugin by placing
402 them in the file $HOME/.vimproject_mappings. This file, if it exists, will be
403 sourced when the plugin in loaded. Here is an example that will count the
404 number of entries in a project when you press \K (Kount, C is taken :-): >
408 function! SpawnExec(infoline, fname, lineno, data)
409 let b:loadcount = b:loadcount + 1
410 if getchar(0) != 0 | let b:stop_everything=1 | endif
412 call Project_ForEach(1, line('.'), "*SpawnExec", 0, '')
413 delfunction SpawnExec
414 echon b:loadcount." Files\r"
416 if exists("b:stop_everything")
417 unlet b:stop_everything
422 nnoremap <buffer> <silent> <LocalLeader>K :call <SID>Wc()<CR>
424 Here's another example of how I integrated the use of perforce with the plugin
425 in my $HOME/.vimproject_mappings:
427 function! s:DoP4(cmd)
428 let name=Project_GetFname(line('.'))
429 let dir=substitute(name, '\(.*\)/.*', '\1', 'g')
431 exec "!".a:cmd.' '.Project_GetFname(line('.'))
435 nmap <buffer> <silent> \pa :call <SID>DoP4("p4add")<CR>
436 nmap <buffer> <silent> \pe :call <SID>DoP4("p4edit")<CR>
438 (Note that I CD to the directory the file is in so I can pick of the $P4CONFIG
439 file. See the perforce documentation.)
441 This creates the mappings \pe to check out the file for edit and \pa to add
442 the file to the depot.
444 Here is another example where I remap the <Return> mapping to use an external
445 program to launch a special kind of file (in this case, it launches ee to view
446 a jpg file). It is a bit contrived, but it works.
448 let s:sid = substitute(maparg('<Return>', 'n'), '.*\(<SNR>.\{-}\)_.*', '\1', '')
449 function! s:LaunchOrWhat()
450 let fname=Project_GetFname(line('.'))
452 exec 'silent! !ee "'.fname.'"&'
454 call {s:sid}_DoFoldOrOpenEntry('', 'e')
457 nnoremap <buffer> <silent> <Return> \|:call <SID>LaunchOrWhat()<CR>
459 If the file ends in .jpg, the external program is launched, otherwise the
460 original mapping of <Return> is run.
462 ==============================================================================
463 SETTINGS *project-settings*
465 You can set these variables in your vimrc file before the plugin is loaded to
466 change its default behavior
469 The width of the Project Window that the plugin attempts to maintain.
472 The Project Plugin is not always successful in keeping the window
473 where I want it with the size specified here, but it does a decent
476 g:proj_window_increment
477 The increment by which to increase the width of the Project Window
478 when pressing <space> or clicking the <LeftMouse>. Default: 100
479 (See |project-mappings|.)
484 Various flags to control the behavior of the Project Plugin. This
485 variable can contain any of the following character flags.
489 b When present, use the |browse()| when selecting directories
490 for \c and \C. This is off by default for Windows, because
491 the windows browser does not allow you to select directories.
493 c When present, the Project Window will automatically close when
496 F Float the Project Window. That is, turn off automatic
497 resizing and placement. This allows placement between other
498 windows that wish to share similar placement at the side of
499 the screen. It is also particularly helpful for external
502 g When present, the mapping for <F12> will be created to toggle
503 the Project Window open and closed.
505 i When present, display the filename and the current working
506 directory in the command line when a file is selected for
509 l When present, the Project Plugin will use the |:lcd| command
510 rather than |:cd| to change directories when you select a file
511 to open. This flag is really obsolete and not of much use
514 L Similar to l, but install a BufEnter/Leave |:autocommand| to
515 ensure that the current working directory is changed to the
516 one specified in the fold CD specification whenever that
517 buffer is active. (|:lcd| only changes the CWD for a window,
520 m Turn on mapping of the |CTRL-W_o| and |CTRL-W_CTRL_O| normal
521 mode commands to make the current buffer the only visible
522 buffer, but keep the Project Window visible, too.
524 n When present, numbers will be turned on for the project
527 s When present, the Project Plugin will use syntax highlighting
528 in the Project Window.
530 S Turn on sorting for refresh and create.
532 t When present, toggle the size of the window rather than just
533 increase the size when pressing <space> or right-clicking.
534 See the entry for <RightMouse> in |project-mappings|.
536 T When present, put Subproject folds at the top of the fold when
539 v When present, use :vimgrep rather than :grep when using \G.
541 g:proj_run1 ... g:proj_run9
542 Contains a Vim command to execute on the file. See the
543 mappings of \1 to \9 above.
545 %f is replaced with the full path and filename
546 %F is replaced with the full path and filename with spaces
548 %n is replaced with the filename alone
549 %N is replaced with the filename alone with spaces quoted
550 %h is replaced with the home directory
551 %H is replaced with the home directory with spaces quoted
552 %r is replaced with the directory relative to the CD path
553 %R is replaced with the directory relative to the CD path
555 %d is replaced with the CD directory.
556 %D is replaced with the CD directory.with spaces quoted
557 %% is replaced with a single % that is not used in
560 (Deprecated: %s is also replaced with the full path and
561 filename for backward compatibility.)
563 For example, gvim will be launched on the file under the
564 cursor when you enter \3 if the following is in your vimrc
566 let g:proj_run3='silent !gvim %f'
567 < Here are a few other examples: >
568 let g:proj_run1='!p4 edit %f'
569 let g:proj_run2='!p4 add %f'
570 let g:proj_run4="echo 'Viewing %f'|sil !xterm -e less %f &"
572 On Windows systems you will want to put the %f, %h, and %d in
573 single quotes to avoid \ escaping.
575 g:proj_run_fold1 ... g:proj_run_fold9
576 Contains a Vim command to execute on the files in a fold. See
577 the mappings of \f1 to \f9 and \F1 to \F9 above.
579 %f is the filename, %h is replaced with the project home
580 directory, and %d is replaced with the CD directory. Multiple
581 filenames can be handled in two ways:
583 The first (default) way is to have %f replaced with all the
584 absolute filenames, and the command is run once. The second
585 is to have the command run for each of the non-absolute
586 filenames (%f is replaced with one filename at a time). To
587 select the second behavior, put an '*' character at the
588 beginning of the g:proj_run_fold{x} variable. (The '*' is
589 stripped before the command is run.)
591 For example, note the difference between the following: >
592 let g:proj_run_fold3="*echo '%h/%f'"
593 let g:proj_run_fold4="echo '%f'"
595 Note that on Windows systems, you will want the %f, %h, and %c
596 within single quotes, or the \ in the paths will cause
597 problems. The alternative is to put them in |escape()|.
600 ==============================================================================
601 PROJECT EXAMPLE FILE *project-example*
603 Here is an example ~/.vimprojects file: >
605 1 My Project=~/c/project CD=. in=in.vim out=out.vim flags=r {
609 5 GUI Files=. filter="gui*.c gui*.h" {
613 9 gui.h # Header file
615 11 Database Files=. filter="data*.c data*.h" {
620 16 OS-Specific Files {
621 17 Win32=. filter="os_win32*.c os_win32*.h" {
625 21 Unix=. filter="os_unix*.c os_unix*.h" {
632 (Don't type in the line numbers, of course.)
635 ==============================================================================
636 TIPS ON USING PROJECT PLUGIN *project-tips*
638 1. You can create a Project Entry by entering this: >
640 Label=~/wherever CD=. filter="*.c *.h" {
643 Then you can put the cursor in the fold and press \r. The script will fill
644 in the files (C files in this case) from this directory for you. This is
645 equivalent to \c without any dialogs.
647 2. You can edit the Project File at any time to add, remove, or reorder files
650 3. If the Project Window ever gets closed, you can just enter >
652 < to bring it back again. (You don't need to give it the filename; the
655 If you have the 'm' flag set in g:proj_flags, then you get the Project
656 Window to show up again by pressing |CTRL-W_o|. This, of course, will
657 close any other windows that may be open that the cursor is not in.
659 4. Adding files to a Project is very easy. To add, for example, the 'more.c'
660 file to the Project, just insert the filename in the Project Entry then
663 5. When |quickfix| loads files, it is not equivalent to pressing <Return> on
664 a filename, so the directory will not be changed and the scripts will not
665 be run. (If I could make this otherwise, I would.) The solution is to use
666 the \L key to load all of the files in the Project before running
669 6. If the Project window gets a bit cluttered with folds partially
670 open/closed, you can press |zM| to close everything and tidy it up.
672 7. For advanced users, I am exporting the function Project_GetAllFnames()
673 which returns all the filenames within a fold and optionally all its
674 Subprojects. Also, I export Project_ForEach() for running a function for
675 each filename in the project. See the code for examples on how to use
676 these. Finally, I export Project_GetFname(line_number) so that you can
677 write your own mappings and get the filename for it.
679 8. Some people have asked how to do a global mapping to take the cursor to
680 the Project window. One of my goals for the plugin is for it to be as
681 self-contained as possible, so I'm not going to add it by default. But you
682 can put this in your vimrc:
684 nmap <silent> <Leader>P :Project<CR>
687 9. You can put the . entry in a project, and it will launch the
688 |file-explorer| plugin on the directory. To avoid removal when you
689 refresh, make the entry look like this:
693 ==============================================================================
696 The following people have sent me patches to help with the Project
710 vim:ts=8 sw=8 noexpandtab tw=78 ft=help: