public
Last active

FindFile.vim with tabnew

  • Download Gist
FindFile.vim
VimL
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
" vim600: set foldmethod=marker:
" $Id:$
" PURPOSE: {{{
" - FindFile: Switch to an auto-completing buffer to open a file quickly.
"
" REQUIREMENTS:
" - Vim 7.0
"
" USAGE:
" Put this file in your ~/.vim/plugin directory.
" If you are working on a project, go to the root directory of the project,
" then execute:
"
" :FindFileCache .<CR>
" or
" :FC .<CR>
"
" This will recursively parse the directory and create the internal cache.
"
" You can also put in multiple arguments in :FC:
"
" :FC /dir1 /dir2 /dir3
"
" You can add to the cache by calling :FC again. File with the same path
" will not be added to the cache twice.
"
" To find a file:
"
" :FindFile<CR>
" or
" :FF<CR>
"
" This opens a scratch buffer that you can type in the file name. Press
" <Esc> will quit the buffer, while <Enter> will select and edit the file.
"
" To clear the internal cache, do:
"
" :FindFileCacheClear<CR>
" or
" :FCC<CR>
"
"
" You can put the following lines in your ~/.vimrc in order to invoke
" FindFile quickly by hitting <C-f>:
"
" :nmap <C-f> :FindFile<CR>
"
" By default, all the *.o, *.pyc, and */tmp/* files will be ignored, in
" addition to the wildignore patterns. You can customize this by setting in
" your .vimrc:
"
" let g:FindFileIgnore = ['*.o', '*.pyc', '*/tmp/*']
"
" CREDITS:
" Please mail any comment/suggestion/patch to
" William Lee <wl1012@yahoo.com>
"
 
" Section: Mappings {{{1
command! -nargs=* -complete=dir FindFileCache call <SID>CacheDir(<f-args>)
command! -nargs=* -complete=dir FC call <SID>CacheDir(<f-args>)
 
command! FindFileCacheClear call <SID>CacheClear()
command! FCC call <SID>CacheClear()
 
command! FindFile call <SID>FindFile()
command! FF call <SID>FindFile()
 
command! FindFileSplit call <SID>FindFileSplit()
command! FS call <SID>FindFileSplit()
 
command! FindFileTab call <SID>FindFileTab()
command! FT call <SID>FindFileTab()
 
 
" Global Settings {{{1
if !exists("g:FindFileIgnore")
let g:FindFileIgnore = ['*.o', '*.pyc', '*/tmp/*']
endif
 
" Section: Functions {{{1
" File cache to store the filename
let s:fileCache = {}
" The sorted keys for the dictionary
let s:fileKeys = []
 
fun! CompleteFile(findstart, base)
if a:findstart
return 0
else
" TODO: We can definitely do a binary search on the keys instead of
" doing a linear match.
for k in s:fileKeys
let matchExpr = <SID>EscapeChars(a:base)
if match(k, matchExpr) == 0
call complete_add({'word': k, 'menu': s:fileCache[k], 'icase' : 1})
endif
if complete_check()
break
endif
endfor
return []
endif
endfun
 
fun! <SID>MayComplete(c)
if pumvisible()
return a:c
else
return "" . a:c . "\<C-X>\<C-O>"
endif
endfun
 
fun! <SID>FindFileSplit()
split
call <SID>FindFile()
endfun
 
fun! <SID>FindFileTab()
tabnew
call <SID>FindFile()
endfun
 
fun! <SID>FindFile()
new FindFile
setlocal buftype=nofile
setlocal bufhidden=hide
setlocal noswapfile
" Map the keys for completion:
" We are remapping keys from ascii 33 (!) to 126 (~)
let k = 33
while (k < 127)
let c = escape(nr2char(k), "\\'|")
let remapCmd = "inoremap <expr> <buffer> " . c . " <SID>MayComplete('" . c . "')"
exe remapCmd
let k = k + 1
endwhile
 
inoremap <buffer> <CR> <ESC>:silent call <SID>EditFile(getline("."))<CR>
inoremap <buffer> <ESC> <C-[>:silent call <SID>QuitBuff()<CR>
nnoremap <buffer> <ESC> :silent call <SID>QuitBuff()<CR>
setlocal completeopt=menuone,longest,preview
setlocal omnifunc=CompleteFile
setlocal noignorecase
startinsert
endfun
 
fun! <SID>CacheClear()
let s:fileCache = {}
let s:fileKeys = []
echo "FindFile cache cleared."
endfun
 
fun! <SID>EscapeChars(toEscape)
return escape(a:toEscape, ". \!\@\#\$\%\^\&\*\(\)\-\=\\\|\~\`\'")
endfun
 
fun! <SID>CacheDir(...)
echo "Finding files to cache..."
for d in a:000
"Creates the dictionary that will parse all files recursively
for i in g:FindFileIgnore
let s = "setlocal wildignore+=" . i
exe s
endfor
let files = glob(d . "/**")
for i in g:FindFileIgnore
let s = "setlocal wildignore-=" . i
exe s
endfor
let ctr = 0
for f in split(files, "\n")
let fname = fnamemodify(f, ":t")
let fpath = fnamemodify(f, ":p")
" We only glob the files, not directory
if !isdirectory(fpath)
" If the cache already has this entry, we'll just skip it
let hasEntry = 0
while has_key(s:fileCache, fname)
if s:fileCache[fname] == fpath
let hasEntry = 1
break
endif
let fnameArr = split(fname, ":")
if len(fnameArr) > 1
let fname = fnameArr[0] . ":" . (fnameArr[1] + 1)
else
let fname = fname . ":1"
endif
endwhile
if !hasEntry
let s:fileCache[fname] = fpath
let ctr = ctr + 1
endif
endif
endfor
let s:fileKeys = sort(copy(keys(s:fileCache)))
echo "Found " . ctr . " new files in '" . d . "'. Cache has " . len(s:fileKeys) . " entries."
endfor
endfun
 
fun! <SID>QuitBuff()
silent exe "bd!"
endfun
 
fun! <SID>EditFile(f)
" Closes the buffer
let fileToOpen = a:f
if has_key(s:fileCache, a:f)
let fileToOpen = s:fileCache[a:f]
else
" We attempt to find the file in the list if it is not a
" complete key
let matchExpr = <SID>EscapeChars(a:f)
for k in s:fileKeys
if match(k, matchExpr) == 0
let fileToOpen = s:fileCache[k]
break
endif
endfor
endif
if filereadable(fileToOpen)
silent exe "bd!"
silent exe "edit " . fileToOpen
echo "File: " . fileToOpen
else
echo "File " . fileToOpen . " not found. Run ':FileFindCache .' to refresh if necessary."
endif
endfun

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.