Search code examples
vimfile-type

vi, condition in vimrc


I am trying to personalize my ~/.vimrc file.

Here is what I want: when the file name opened with vi is titi45.tex, and when I press <space> in normal mode, I want that the command make toto45 is executed. And if the file opened with vi is called titi65.tex, I want that make toto65 is executed, and so on.

I tried to use a command

au FileType tex nmap <space> :w<CR>:!make<CR><CR>

in my .vimrc but I don't know how to match the file name and use the number.

Can you help me?

Mathieu


Solution

  • You are looking for :make %<. BTW why don't you compile within vim? Avoid :!make. Prefer :make, and check the help related to the quickfix mode (:h quickfix).

    Your mapping would then be:

    nnoremap <buffer> <silent> <space> :update<cr>:make %<<cr>
    

    (the <buffer> part is very important, it makes sure your mapping won't leak to other filetypes; the other are not critical here, but good practices)


    EDIT: Sorry I missed the exact requirement.

    Then, you'll have to transform the target name. You'll to play with substitute() and :exe. But your substitution won't be a simple substitution. It looks like a rotating substitution. Solutions for this kind of substitution have been described over there: How do I substitute from a list of strings in VIM? And IIIRC, there exist a plugin that does similar things.

    In your case, I guess I would use a dictionary to define how names are substituted:

    let k_subs = { 'toto': 'titi', 'titi': 'toto' }
    nnoremap <buffer> <silent> <space> :update<cr>:exe 'make '.substitute(expand('%'), join(keys(k_subs), '\|'), '\=k_subs[submatch(0)]', '')cr>
    

    NB: I haven't tested it.

    If you want to get rid of the extension, it'd better be done in expand() argument.