Editing Searching for files 0 We recommend that you log in before editing. This will allow other users to leave you a message about your edit, and will let you track edits via your Watchlist. Creating an account is quick and free. {{TipImported |id=799 |previous=798 |next=800 |created=October 3, 2004 |complexity=intermediate |author=elz |version=7.3 |rating=2/2 |category1=File Handling |category2= |category3= }} Vim offers several commands for searching for files by name: {{help|prefix=no|:find}}, {{help|prefix=no|:sfind}}, {{help|prefix=no|:tabfind}} on the command-line, several normal-mode commands like {{help|prefix=no|gf}}, and others. If you just enter <code>:find filename</code>, Vim will directly open the first file found in your {{help|prefix=no|'path'}} matching "filename". You can provide a count like <code>:2find filename</code> to edit the second file on the path matching the filename, but it can be difficult to figure out what order the file you're looking for might occur in your path. Vim 7.3 makes this easier, by introducing tab-completion of filenames found by the <code>:find</code> command. If you set {{help|prefix=no|'wildmenu'}}, then you can see a list of all files found by the command and select which one to edit without worrying about counts or anything else. You can also use external utilities like <code>find</code> on Unix-like systems to look for files. For example, if you want to populate your {{help|prefix=no|quickfix}} list with the output of a <code>find</code> command, you could add this to your [[vimrc]] file: <source lang="vim"> " find files and populate the quickfix list fun! FindFiles(filename) let error_file = tempname() silent exe '!find . -name "'.a:filename.'" | xargs file | sed "s/:/:1:/" > '.error_file set errorformat=%f:%l:%m exe "cfile ". error_file copen call delete(error_file) endfun command! -nargs=1 FindFile call FindFiles(<q-args>) </source> Then, when in normal mode, use the <code>:FindFile</code> command to search for a filename pattern. All the file names that match this pattern (under the current directory) will be displayed in the quickfix window, along with a description of each of one of them. Notice, the search is done in a recursive manner, and you can use wildcards. If your version of <code>find</code> supports it, you can do a case-insensitive search by replacing <code>-name</code> with <code>-iname</code>; otherwise you can specify multiple cases with a pattern like <code>[Ff]oo[Bb]ar.txt</code>. If you want to use a regular expression, you can call <code>find</code> with the <code>-regex</code> or <code>-iregex</code> flags instead (if supported). The function uses some standard Unix-like utilities: find, file, sed. ==Related plugins== * {{script|id=1984|text=FuzzyFinder}} (requires {{script|id=3252|text=L9 plugin}}) * [http://kien.github.io/ctrlp.vim/ Ctrl-P] (temporarily expands the commandline) * {{script|id=3025|text=Command-T}} (requires Ruby support) * {{script|id=4198|text=AsyncFinder}} (requires Python support, splits open a temporary window, never locks up Vim) ==See also== *[[Find files in subdirectories]] ==Comments== I use this little script. If we find an exact match (wc -l = 1), i open the file in vim. If I find more than one matches, open a list of the files. Then I can use 'gf' (gotofile) in vim to open the specific file. <pre> #! /bin/sh ## fvim: finds files and opens them in vim listfile=/tmp/fvim.tmp ## Find files and store them in a list find . -iname "$1" > $listfile findcount=`cat $listfile | wc -l` if [ $findcount -ge 2 ] ; then vim $listfile else vim `cat $listfile` fi </pre> ---- This is what I do: <source lang="vim"> function! Find(...) if a:0 == 1 let filename = a:1 execute "args `find . -iname '*".filename."*' -print`" elseif a:0 == 2 let path = a:1 let filename = a:2 execute 'args `find '.path." -iname '*".filename."*' -print`" endif endfunction command! -nargs=+ Find call Find(<f-args>) </source> ---- This way the files are opened in your args list and you can use <code>:argdo</code> <pre> " Use grep on filenames instead of relying on find's patterns. " TODO: How to hook this up with 'gf'? command! -nargs=1 FindFiles call FindFiles(<q-args>) function! FindFiles(filename) let error_file=tempname() silent exe '!find . ~ \|grep -Pis "'.a:filename.'" -- - \| xargs file \| sed "s/:/:1:/" > '.error_file setl errorformat=%f:%l:%m exe "cfile ". error_file copen call delete(error_file) endfunction </pre> Loading editor Below are some commonly used wiki markup codes. Simply click on what you want to use and it will appear in the edit box above. Insert: – — … ° ≈ ≠ ≤ ≥ ± − × ÷ ← → · § Sign your username: ~~~~ Wiki markup: {{}} | [] [[]] [[Category:]] #REDIRECT [[]] <s></s> <sup></sup> <sub></sub> <code></code> <blockquote></blockquote> <ref></ref> {{Reflist}} <references/> <includeonly></includeonly> <noinclude></noinclude> {{DEFAULTSORT:}} <nowiki></nowiki> <!-- --> <span class="plainlinks"></span> Symbols: ~ | ¡ ¿ † ‡ ↔ ↑ ↓ • ¶ # ¹ ² ³ ½ ⅓ ⅔ ¼ ¾ ⅛ ⅜ ⅝ ⅞ ∞ ‘ “ ’ ” «» ¤ ₳ ฿ ₵ ¢ ₡ ₢ $ ₫ ₯ € ₠ ₣ ƒ ₴ ₭ ₤ ℳ ₥ ₦ № ₧ ₰ £ ៛ ₨ ₪ ৳ ₮ ₩ ¥ ♠ ♣ ♥ ♦ View this template This field is a spam trap. DO NOT fill it in! Edit summary Preview Mobile Desktop Show changes