[英]In vim toggle case of all characters in a text file with a single command
In Vim I need to convert all lowercase to uppercase and all uppercase to lowercase with a single command.在 Vim 中,我需要使用单个命令将所有小写字母转换为大写字母,并将所有大写字母转换为小写字母。 So if my text file looks like this..
所以如果我的文本文件看起来像这样..
Hello World
.. it needs to be toggled to look like this.. ..它需要切换到看起来像这样..
hELLO wORLD
I know :%s/[az]/\U&/g
will change all lowercase to uppercase and that :%s/[AZ]/\L&/g
will change all uppercase to lowercase.我知道
:%s/[az]/\U&/g
会将所有小写变为大写,并且:%s/[AZ]/\L&/g
会将所有大写变为小写。 But how would I write that to do both at the same time?但是我怎么写才能同时做呢?
In addition I know if my cursor is at the top of the file VG~
will toggle case everything but that's not the answer I need.此外,我知道如果我的 cursor 位于文件
VG~
的顶部,将切换所有内容,但这不是我需要的答案。 Thank you.谢谢你。
<Esc>1GVG~
Explanation:解释:
<Esc> — return to Normal mode; just in case we're in Insert mode or Command line
1G — jump to the 1st line
V — start Visual mode
G — jump to the last line extending selection
~ — toggle case in the selection
Or或者
<Esc>1Gg~G
g~<motion> — change case during motion; the motion is G (jump to last line)
Docs: http://vimdoc.sourceforge.net/htmldoc/change.html#~文档: http://vimdoc.sourceforge.net/htmldoc/change.html#~
Looks like you already know everything you need.看起来你已经知道你需要的一切。
ggVG~
marks all your code and toggles the case. ggVG~
标记所有代码并切换大小写。 If you want a single command you can either use:如果你想要一个命令,你可以使用:
:nnoremap <keybinding> ggVG~
or use this function, which does the same, but keeps your current position in the file:或使用此 function,它的作用相同,但将您当前的 position 保留在文件中:
function ToggleCase()
exec "normal! mqHmw"
exec "normal! ggVG~"
exec "normal! 'wzt`q"
endfunction
command ToggleCase silent call ToggleCase()
the first and last exec
mark your position in the file and restore them, after the case toggling.第一个和最后一个
exec
在文件中标记您的 position 并在案例切换后恢复它们。 See: :h marks
请参阅:
:h marks
type :ToggleCase
to use the function.键入
:ToggleCase
以使用 function。 Of cause you can bind this to a keybinding as well.当然,您也可以将其绑定到键绑定。
:nnoremap <keybinding> :ToggleCase<cr>
One more way to accomplish this, if you're ok adopting a plug-in, is to use the kana/vim-textobj-entire plug-in for a text object for the entire buffer.如果您可以采用插件,另一种方法是使用kana/vim-textobj-entire插件来获取整个缓冲区的文本 object。
As the plug-in README.md file says:正如插件 README.md 文件所说:
Though these are trivial operations (eg
ggVG
), text object versions are more handy, because you do not have to be conscious of the cursor position (egvae
).虽然这些是微不足道的操作(例如
ggVG
),但文本 object 版本更方便,因为您不必意识到 cursor position (例如vae
)。
With this plug-in installed and enabled, you can switch case of the whole buffer with:安装并启用此插件后,您可以使用以下命令切换整个缓冲区的大小写:
g~ae
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.