0

I want to open a bld.log file as default else the input filename in vim vsplit window or in newtab no matter from which nested subfolder I am opening it.
So I added following function in ~/.vimrc but instead of opening searched path path1 its opening the newfile with name path1.
Please let me know what I am missing here or how can I achieve it ?

function! Err1(...)  
if a:0 > 0
    let s:path1 = findfile(a:1,';')
else
    let s:path1 = findfile('bld.log',';')
end
exec ':vsp ' . s:path1
endfunction  
command -nargs=1 Err call Err1(...)  

P.S. I want to do above in .vimrc only not in .bashrc

1 Answer 1

0

You can do this using exec (this answers the original question before edit, how to use :tabnew from the function):

function! Err1()
    let s:path1 = findfile('README.md','.,**')
    echo findfile('README.md',';')
    exec ':tabnew ' . s:path1
endfunction
command Err call Err1()

Here is an example of function which accepts an optional parameter:

function! Err1(...)
    if len(a:1) > 0
      let l:path = a:1
    else
      let l:path = 'README.md'
    end
    let l:file = findfile(l:path,'.,**')
    if len(l:file) > 0
      exec ':vsp ' . l:file
    else
      echom 'File not found: ' . l:path . ' ' . l:file
    end
endfunction
command -nargs=* Err call Err1('<args>')

Another option is to add set path=.,** into the .vimrc and then just use the standard "file" command, like :find bld.log.

12
  • Thanks a lot Boris. Also one more doubt can you tell me same function can we rewrite to receive README.md as argument i.e runtime parameter.
    – ypp
    Mar 15, 2016 at 22:11
  • @sidyll : I tried this but not working function! Err1(file1) let s:path1 = findfile('a:file1',';') "findfile('xr_bld.ncs4k.log',';') exec ':vsp ' . s:path1 endfunction command Err call Err1()
    – ypp
    Mar 15, 2016 at 22:27
  • @ypp if you're using a variable as parameters, remove the quotes findfile(a:file, ...
    – sidyll
    Mar 15, 2016 at 22:31
  • @ypp you edited the question and my answer now looks like a useless one, because you already have the exec in your code. When you have the question answered - just mark it as "accepted" and if you have more questions - ask in comments or post another separate question. It is really annoying to see when the question turns into something completely different after you answered it. I also added information about :find command into my answer, check maybe you can just use the standard feature instead of making your own command. Mar 16, 2016 at 8:24
  • @Boris I am Sorry if you feel offended, but i wasn't able to add code in comment So I edited original question. My mistake I apologize.
    – ypp
    Mar 16, 2016 at 10:10

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.