0

I have a file called Index.txt with the following lines:

/Project/A/B/C/D/main.c
/Project/A/B/C/D/main_backend.c
/Project/A/B/C/D/main_frontend.c 

I'd like to create a command called Fsearch to execute a search in Index.txt using regular expressions, match the first occurrence and execute :vsplit with it. For example, if I execute:

:Fsearch main_backend.c

Vim should execute:

:vsplit /Project/A/B/C/D/main_backend.c

and If I execute:

:Fsearch main*.c

Vim should execute:

:vsplit /Project/A/B/C/D/main.c

This is what I've tried so far but I'm pretty sure it could be improved:

function! CopyMatches(reg)  
let l:file = grep -m 1 a:reg ~/Index.txt
echom l:file
if len(l:file) > 0
    exec ':vsp ' . l:file
else
echom 'File not found: ' . l:file
end
endfunction  
command! -nargs=* Fsearch call CopyMatches('<args>')  

Any suggestion?

1 Answer 1

1

You could try this:

function! CopyMatches(reg)
   execute "silent! grep!" a:reg " ~/Index.txt"
   redraw!
   let l:file = getqflist()
   if len(l:file) > 0
       let l:path_head = fnamemodify( "~/workspace", ":p" )
       for l:item in l:file
           let l:current_file = l:path_head . "/" . l:item["text"]  
           if match( l:current_file, getcwd() ) != -1 
               execute 'vsplit' fnamemodify( l:current_file, ":~:.")
               return
           endif
        endfor
        echom "File not found:" a:reg
   else
      echom "File not found:" a:reg
   endif
endfunction

command! -nargs=* Fsearch call CopyMatches('<args>')

Explanation:

  • The :grep built-in command is a wrap used by Vim to execute an external grep (see :help grep for more information).
  • The :grep! form of the :grep command doesn't jump to the first match automatically (i.e., :grep! won't open Index.txt).
  • The :silent! command is used to suppress the default full screen grep output.
  • Vim uses quickfix list with :grep so you can get all occurrences from getqflist() function (see :help getqflist() for details)
9
  • Thanks it works gr8. With your earlier solution if I want to change the line execute "silent! grep!" a:reg " ~/Index.txt" to execute "silent! grep!" a:reg " ~/cscope.files" | "grep cut -d/ -f 5- <<<"${PWD}"" . So that I can get only relative result always. plz Any suggestion how to do it?
    – ypp
    Apr 25, 2016 at 18:53
  • system("pwd | cut -d/ -f 5-".expand("%")) giving desired search string I wanna use so I tried execute "silent! grep!" a:reg " ~/cscope.files" | exe "silent! grep" system("pwd | cut -d/ -f 5-".expand("%")) but it keep on running after doing Ctrl+c its giving error E486: pattern not found :dev
    – ypp
    Apr 25, 2016 at 19:21
  • say my $pwd is /home/user/workspace/Project_1/A/B/C/ for now :Fsearch main*.c say I am getting following result (i am not using -m1 in grep) /home/user/workspace/Project_1/A/B/main.c /home/user/workspace/Project_1/A/B/C/main_backend.c /home/user/workspace/Project_1/A/B/C/D/main_frontend.c /home/user/workspace/Project_1/A/main_common.c But I want :Fsearch main*.c should fetch only result i.e w.r.t. $pwd /home/user/workspace/Project_1/A/B/C/main_backend.c /home/user/workspace/Project_1/A/B/C/D/main_frontend.c
    – ypp
    Apr 26, 2016 at 8:42
  • It always telling file not found. I printed the l:current_file just below echom "File not found:" a:reg It showing l:current_file /users/user1./Project_1/A/main_common.c instead it should have this value Project_1/A/main_common.c And before redraw! it printing /users/user1/Index.txt:10:./Project_1/A/main_common.c
    – ypp
    Apr 26, 2016 at 14:51
  • Now I removed ./ i.e period slash at start of each line of Index.txt but added print is printing the value of l:current_file /users/user1Project_1/A/main_common.c i.e vsplit trying to open vsplit /users/user1Project_1/A/main_common.c thats why it return always file not found, instead it should open vsplit Project_1/A/main_common.c
    – ypp
    Apr 26, 2016 at 18:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.