Cito
Cito

Reputation: 1709

VIM Search - FuzzyFinder to search specific file

I'm newbie at VIM using it as IDE. I'm trying to figure how to open in an easy way files within my "project".

I have this configurations in my .vimrc file:

colorscheme Tomorrow-Night
" nocompatible has to be the first of all ( use the real vimpower )
set nocompatible

" backup rules
set backup " enable backup files (.txt~)
set undofile " enable persistent undo

silent execute '!mkdir -p $HOME/.vim/tmp/backup'
set backupdir=$HOME/.vim/tmp/backup " where to store backup
silent execute '!mkdir -p $HOME/.vim/tmp/swap'
set directory=$HOME/.vim/tmp/swap " where to store swap
silent execute '!mkdir -p $HOME/.vim/tmp/views'
set viewdir=$HOME/.vim/tmp/views " where to store view
silent execute '!mkdir -p $HOME/.vim/tmp/undo'
set undodir=$HOME/.vim/tmp/undo " where to store undo 

" syntax
syntax on " enable syntax highlighting
" filetype
filetype on " enable filetype detection
filetype plugin on " enable filetype plugins
filetype indent on " enable filetype indentation

" tabstop settings
set tabstop=4 " a tab found in a file will be represented with 4 columns
set softtabstop=4 " when in insert mode <tab> is pressed move 4 columns
set shiftwidth=4 " indentation is 4 columns

" show linenumbers
set number

" Documentation configuration
let g:pdv_cfg_Author = 'Abraham Cruz <[email protected]>'

" Autocompletition
" Complete options (disable preview scratch window)
set completeopt = menu,menuone,longest
" Limit popup menu height
set pumheight = 15

" SuperTab option for context aware completion
let g:SuperTabDefaultCompletionType = "context"
let g:SuperTabNoCompleteAfter = ['^', ',', '\s']        
let g:SuperTabMappingForward = '<c-space>'
let g:SuperTabMappingBackward = '<s-c-space>'

" Disable auto popup, use <Tab> to autocomplete
" let g:clang_complete_auto = 0
" Show clang errors in the quickfix window
let g:clang_complete_copen = 1

" TagBar Lint
let g:tagbar_phpctags_memory_limit = '512M'

" Upload on save if the project is configured to do so
autocmd BufWritePost * :call SyncUploadFile()

" Open each buffer in its own tab
:au BufAdd,BufNewFile * nested tab sball

So every new buffer is opened in a new tab.

I know if I do:

:e libra*/Core/Sess*/Ob*

for instance, it will open the file library/Core/Session/Object.php But the problem is if I make one mistake, instead of open the file, it will open a new Buffer with the name libra*/Core/Sess*/Ob* (supposing the file does not exists). Also, I don't like to be writing every time all the / so will be better to just do:

:e libr*Cor*Sess*Obj*.php

But it will open a new buffer with that name (libr*Cor*Sess*Obj*.php), instead of opening the file...

I came to know also that I can do:

:fin libra(press tab here) 

And it will autocomplete... but the problem here is it will show (for this example) :find libexslt/ and I know for sure that folder is not within the folder I'm. Actually, here is an screenshot of the folders I have there:

Folders on working folder

So as you can see, I have no libexslt there... so I don't know where it is getting that info from...

Is there any way to open a file easily? In this example, I know the file is Session/Object.php so could I do something like:

:fin *Sessi*Obj*php

? Also, I know the file has inside the declaration:

<?php
class Core_Session_Object

Or, another example,

<?php
namespace Core\Session;
class Object 

Can I do something like:

:vimgrep /class*Core_Session_Object/ *.php
:vimgrep /name*Core*Sess*Obj/ *.php

?

Upvotes: 0

Views: 173

Answers (2)

David
David

Reputation: 20085

It's always good to learn pure Vim methods for navigating files, which makes romainl's answer really good.

There are also a couple of plugins that provide the fuzzy-finder feature.

  1. Ctrl-p plugin
  2. Command-t plugin

That said, I've had Ctrl-p installed for a long time, but I rarely use it because I just don't need it.

Upvotes: 0

romainl
romainl

Reputation: 196456

You can use tab-completion for :edit and :find but the default mechanism is not very user-friendly. The "wildmenu" is a very nice feature that makes the whole process a lot easier. Enable it with:

:set wildmenu

and take a look at the following sections of the documentation:

:help wildmenu
:help wildmode
:help wildignore
:help wildignorecase

The ** wildcard allows you to recurse through subdirectories so you can do:

:e **/obj*ph<Tab>

The behavior of :find depends on the value of the path option whose default value is not very useful if you don't write C.

Set it to this much more useful generic value to recurse through subdirectories:

:set path=.,**

With the settings above, you should be able to open your file with:

:e **/obj*ph<Tab>

or:

:find obj*ph<Tab>

Note that opening a file to access a method or some other symbol is not very efficient, whether you use clever commands/mappings or not. Tag-jumping is a much more efficient method. You'll need a symbol "database", usually crated before the search, for that, though.

See these sections of the documentation:

:help tags
:help ctags
:help cscope

And :h include-search for a lightweight variant.

Using :vimgrep or :grep can also serve your needs but I'd suggest you take a look at the faster ack and ag.


Here is a cleaned up version of your vimrc with the relevant options added:

filetype plugin indent on
syntax on

colorscheme Tomorrow-Night

set wildmenu

set path=./**

set backup
set undofile

silent execute '!mkdir -p $HOME/.vim/tmp/backup'
set backupdir=$HOME/.vim/tmp/backup " where to store backup
silent execute '!mkdir -p $HOME/.vim/tmp/swap'
set directory=$HOME/.vim/tmp/swap " where to store swap
silent execute '!mkdir -p $HOME/.vim/tmp/views'
set viewdir=$HOME/.vim/tmp/views " where to store view
silent execute '!mkdir -p $HOME/.vim/tmp/undo'
set undodir=$HOME/.vim/tmp/undo " where to store undo

set tabstop=4
set softtabstop=4
set shiftwidth=4

set number

set completeopt = menu,menuone,longest
set pumheight = 15

augroup VIMRC
    autocmd!
    autocmd BufWritePost * :call SyncUploadFile()
    autocmd BufAdd,BufNewFile * nested tab sball
augroup END

let g:pdv_cfg_Author = 'Abraham Cruz <[email protected]>'

let g:SuperTabDefaultCompletionType = "context"
let g:SuperTabNoCompleteAfter = ['^', ',', '\s']
let g:SuperTabMappingForward = '<c-space>'
let g:SuperTabMappingBackward = '<s-c-space>'

let g:clang_complete_copen = 1

let g:tagbar_phpctags_memory_limit = '512M'

Upvotes: 2

Related Questions