Wikia

Vim Tips Wiki

Changes: Collect filenames of current subtree

Edit

Back to page

(mention built-in method but keep script, it works and could be interesting)
(Change <tt> to <code>, perhaps also minor tweak.)
 
Line 3: Line 3:
 
|previous=658
 
|previous=658
 
|next=660
 
|next=660
|created=February 18, 2004
+
|created=2004
 
|complexity=basic
 
|complexity=basic
 
|author=Siegfried Bublitz
 
|author=Siegfried Bublitz
Line 19: Line 19:
 
Sometimes though, you may simply want to list out all the files in a directory tree in a buffer. The below function does just this. It globs the file names of the current directory and iterates through all the names, globbing it again if it is a directory. Note, it can be very slow for large directories.
 
Sometimes though, you may simply want to list out all the files in a directory tree in a buffer. The below function does just this. It globs the file names of the current directory and iterates through all the names, globbing it again if it is a directory. Note, it can be very slow for large directories.
   
The following mapping abbreviates the invocation to pressing <tt>\L'</tt>:
+
The following mapping abbreviates the invocation to pressing <code>\L'</code>:
   
 
<pre>
 
<pre>

Latest revision as of 05:41, July 13, 2012

Tip 659 Printable Monobook Previous Next

created 2004 · complexity basic · author Siegfried Bublitz · version 5.7


In Vim you can edit directories, but sometimes it is more convenient to have the names of all files in the complete subtree listed in one buffer. The netrw plugin which creates the directory listing can be tweaked to show a tree view, by using the g:netrw_liststyle variable in your .vimrc or from the command-line before invoking the directory explorer:

:let g:netrw_liststyle=3

Sometimes though, you may simply want to list out all the files in a directory tree in a buffer. The below function does just this. It globs the file names of the current directory and iterates through all the names, globbing it again if it is a directory. Note, it can be very slow for large directories.

The following mapping abbreviates the invocation to pressing \L':

map <Leader>L :call ListTree('.')<CR>
function! ListTree(dir)
  new
  set buftype=nofile
  set bufhidden=hide
  set noswapfile
  normal i.
  while 1
    let file = getline(".")
    if (file == '')
      normal dd
    elseif (isdirectory(file))
      normal dd
      let @" = glob(file . "/*")
      normal O
      normal P
      let @" = glob(file . "/.[^.]*")
      if (@" != '')
        normal O
        normal P
      endif
    else
      if (line('.') == line('$'))
        return
      else
        normal j
      endif
    endif
  endwhile
endfunction

CommentsEdit

Around Wikia's network

Random Wiki