" files.vim - Browse the current directory recursively to open a file " Maintainer: Allen Kim " License: MIT license " Version: 0.1 " Do not load this plugin if is has already been loaded. if exists("g:loadedFiles") finish endif let g:loadedFiles = 0.1 " version number " Function : Files(PUBLIC) " Purpose : Lists files of the current directory including sub directories " Args : none " Returns : none function! Files() call s:OpenWindow() endfunction " ------------------------------------------------------------------------------ " Function : OpenWindow(PRIVATE) " Purpose : Display all files in a temporary window " Args : None " Returns : None function! s:OpenWindow() " Save the current buffer number. This is used later to open a file when a entry is selected from the temporary window. let s:lastBuffer = bufnr('%') let bname = '__files__' " If the window is already open, jump to it let winnum = bufwinnr(bname) if winnum != -1 if winnr() != winnum " If not already in the window, jump to it exe winnum . 'wincmd w' endif setlocal modifiable silent! %delete _ " Delete the contents of the buffer to the black-hole register else " Open a new window at the bottom && open a new buffer let bufnum = bufnr(bname) if bufnum == -1 let wcmd = bname else let wcmd = '+buffer' . bufnum endif exe 'silent! botright 15split '.wcmd endif " Mark the buffer as scratch setlocal buftype=nofile setlocal bufhidden=delete setlocal noswapfile setlocal nowrap setlocal nobuflisted setlocal winfixheight " Setup the cpoptions properly for the maps to work let old_cpoptions = &cpoptions set cpoptions&vim " Create mappings to select and edit a file from the file list nnoremap :call OpenFile('edit') vnoremap :call OpenFile('edit') nnoremap o :call OpenFile('split') vnoremap o :call OpenFile('split') nnoremap v :call OpenFile('vsplit') vnoremap v :call OpenFile('vsplit') nnoremap q :close " Restore the previous cpoptions settings let &cpoptions = old_cpoptions " " Build file list " normal! gg normal! a""" help: to open file, 'o'(split), 'v'(vsplit) silent! read !ls -fdF $PWD/* $PWD/**/* $PWD/**/**/* | grep -v "/tmp" " " end of build file list " normal! gg " Move the cursor to the beginning of the file let currentLine = getline(".") " string of current line while currentLine !~ "^\/" " if not an absolute file name if currentLine !~ '\v"""' " if not comment line normal! dd " delete this line else normal! j " go to next line endif let currentLine = getline(".") endwhile setlocal nomodifiable " not for edit endfunction " ------------------------------------------------------------------------------ " Function : OpenFile(PRIVATE) " Purpose : Open a file selected from the temporary window " Args : openType, how to open a file, split, vsplit, or edit " Returns : None function! s:OpenFile(openType) range let fname = getline(".") " string of current line let escFname = escape(fname, ' *?[{`$%#"|!<>();&' . "'\t\n") " special character escaped file name " close this window since it is invoked by key map from the temporary window " this has to be done AFTER you get the contents of current line silent! close if a:openType == 'split' " Edit the file in a new horizontally split window above the previous window wincmd p exe 'belowright new ' . escFname elseif a:openType == 'vsplit' " Edit the file in a new vertically split window above the previous window wincmd p exe 'belowright vnew ' . escFname elseif a:openType == 'edit' let winnum = bufwinnr('^' . escFname . '$') if winnum != -1 " If the selected file is already open in one of the windows, jump to it exe winnum . 'wincmd w' else " use the previous window wincmd p let split_window = 0 " open in a split window if; " . current buffer is modified or is the preview window " . current buffer is a special buffer (maybe used by a plugin) if &modified || &previewwindow let split_window = 1 elseif &buftype != '' && bufnr('%') != bufnr('__files__') let split_window = 1 endif if split_window exe 'split ' . escFname else exe 'edit ' . escFname endif endif endif endfunction