Vim替换:展开为大写

3

当我用这个命令打开文件时,我会将文件名插入到其框架中,该命令在我的 ~/.vimrc 文件中:

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()

第一行扩展命令运行良好 %s/FileName/\=expand("%:t:r")/g :所有的 FileName 都会被文件名所替换。

但是第二行扩展命令不起作用 %s/FILENAME/\U\=expand("%:t:r")\E/g -> 看起来我不能同时使用大写字母替换和扩展操作。

我尝试了一些变化但没有成功,我该如何做?

2个回答

7

来自:help sub-replace-\=

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

您可以使用:help substitute()将文件名转为大写:

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

谢谢 :) 如果我需要做比大写字母更复杂的事情,我可以使用这个解决方案! - hugogogo

4

看起来一旦你进入评估模式,就必须将其用于所有内容。尝试

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

完美!谢谢 :) - hugogogo
2
顺便说一句::help string-functions:help toupper() - romainl

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接