@snisnik esta bueno.. Pero es bastante incomodo acostumbrarse

Finding file under cursor on VIM

Working in a big project, I had the problem that the scripts included lots of files and used a lot of templates that were called programatically, because of that, I was looking for files a lot.

Searching the vim wiki I found a script that is used to find files inside a directory and choose the file you need. I’ve modified it a bit and finally it looks like this:

"Function for finding files
" Find file in current directory and edit it.
function! Find(name)
  let l:list=system("find . -name '".a:name."*[htm|html|php]' | grep -v .svn | perl -ne 'print \"$.\\t$_\"'")
  let l:num=strlen(substitute(l:list, "[^\n]", "", "g"))
  if l:num < 1
    echo "'".a:name."' not found"
    return
  endif
  if l:num != 1
    echo l:list
    let l:input=input("Which ? (CR=nothing)\n")
    if strlen(l:input)==0
      return
    endif
    if strlen(substitute(l:input, "[0-9]", "", "g"))>0
      echo "Not a number"
      return
    endif
    if l:input<1 || l:input>l:num
      echo "Out of range"
      return
    endif
    let l:line=matchstr("\n".l:list, "\n".l:input."\t[^\n]*")
  else
    let l:line=l:list
  endif
  let l:line=substitute(l:line, "^[^\t]*\t./", "", "")
  execute ":e ".l:line
endfunction
command! -nargs=1 Find :call Find("<args>")

The function is used doing something like

:Find <name>

The real thing here is to map a keybiding to search the file where the cursor is standing.. Like this:

:nmap <leader>f "zyiw:exe "Find ".@z.""<CR>

This way, with the keystroke “\f” you start the function searching for the word under cursor. Be aware that the keybinding changes if you modified your leader variable.

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL


Leave a comment