Last Updated: February 25, 2016
·
1.47K
· herson

Global Search in vim

Search through multiples files is a pain in neck, in applications like Sublime Text you can use the "find in files" option (command-shift-F), but you can do that in vim too, using the :lvim command.

:lvim /seach/gj ./**/*.html

Here is a wrapper function to make it more user friendly:

function! GlobalSeach()
    let text = escape(input("what do you want to seach?: "),  '\\/')
    if text == ""
        echo "" | return
    endif
    let extension = escape(input("Wich extension? (* for all): "), '\\/')
    if extension == ""
        echo "" | return
    endif

    let search_command = ':lvim /\V' . text . '/gj ./**/*.' . extension
    try
        execute search_command 
    catch
        echo "Nothing found"
        return
    endtry

    lwindow
endfunction

""" mapping the function to leader-shift-f
noremap <leader>F :call GlobalSeach() <CR>