vim substitution expand to uppercase

Viewed 110

I insert the name of my file into its skeleton when i open it with this command into my ~/.vimrc file :

function! HPPFile()
    silent! 0r $HOME/.vim/templates/skeleton.hpp
    %s/FileName/\=expand("%:t:r")/g
    %s/FILENAME/\U\=expand("%:t:r")\E/g
endfunction
autocmd BufNewFile *.hpp call HPPFile()

the first expansion line works well %s/FileName/\=expand("%:t:r")/g : all instances of FileName get replaced by the file name

but the second expansion line doesn't works %s/FILENAME/\U\=expand("%:t:r")\E/g -> it seems i cannot use both the uppercase substitution and the expand action

I've tried some variations but without chance, how could i do that ?

2 Answers

From :help sub-replace-\=:

When the substitute string starts with "\=" the remainder is interpreted as an
expression.

You can use :help substitute() to uppercase your filename:

function! HPPFile()
    silent! 0r $HOME/.vim/templates/skeleton.hpp
    %s/FileName/\=expand("%:t:r")/g
    %s/FILENAME/\=substitute(expand("%:t:r"),'.*','\U&','')/g
endfunction

It seems once you're in evaluation mode you must use it for everything. Try

%s/FILENAME/\=toupper(expand("%:t:r"))/g
Related