Vim Tips Wiki

Find and edit any file in a directory using tag-like pattern matching

1,612pages on
this wiki

Redirected from VimTip1088

Tip 1088 Printable Monobook Previous Next

created 2005 · complexity basic · author Reva Revadigar · version 6.0

Often, you work in a huge code base containing a lot of directories and source files (C++, C#, etc) and you want to quickly edit a file. If you know at least some part of the file name, you can use this tip to look up the file and edit it.

You need some Unix tools such as find, cat, sed and sort. GNU Windows versions of these will do as well.

Put the following in a batch file (these examples are for Windows; you can do equivalents for Unix):

@find [directory] > c:\files.txt
@cat c:\files.txt | sed -f c:\tags.sed | sort > c:\files.tags

Replace [directory] with whatever directory you want to find the files in. For example, c:\root.

Here, c:\tags.sed contains the following:

s/\(.*\/\)\(.*\)/\2 \1\2 1;" F/g

Once, this is done, you simply do the following in your vimrc:

set tags=c:\files.tags

That's it! You can now look up and edit any file.

Example: If you have c:\root\Foo.cpp and c:\root\FooBar.cpp and you gave c:\root to find tool above, then doing :tj Foo<tab> will give you completion for Foo.cpp and FooBar.cpp.


ctags does the same:

ctags -R --file-tags=yes c:/

If you want to know how Vim integrates with ctags, check :help 29.1, :help tagsrch.txt.

See also script#1581.

Around Wikia's network

Random Wiki