简体   繁体   English

在 vim 中,使用单个命令切换文本文件中所有字符的大小写

[英]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>

Since you mentioned using a single command and you mentioned some :%s/.../ substitutions, I'll offer this one:由于您提到使用单个命令并且您提到了一些:%s/.../替换,我将提供这个:

:%normal! g~~

This will run the g~~ command to switch case of a single line, for each line of the buffer.这将运行g~~命令为缓冲区的每一行切换单行的大小写。

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 (eg vae ).虽然这些是微不足道的操作(例如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.

相关问题 如何在 vim 中使用正则表达式来切换文件中所有字符的大小写 - How to use regex in vim to toggle case for all characters in a file 是否有Vim命令删除文件中的所有折叠标记? - Is there a Vim command to delete all fold markers in a file? 使用什么 Vim 命令删除文件每一行某个字符后的所有文本? - What Vim command to use to delete all text after a certain character on every line of a file? 为什么此Vim命令会擦除终端中的所有字符? - Why does this Vim command wipe all characters from my terminal? VIM:删除文件中所有行的特定单词之前的所有字符 - VIM : Deleting all characters before particular word for all the lines in a file 在vim中切换光标下的字符大小写 - Toggle the case of the character under cursor in vim 如何在文本文件中“应用”退格字符(理想情况下是在vim中) - How to “apply” backspace characters within a text file (ideally in vim) 用于“编辑新文件”或“切换到现有缓冲区”的单个vim命令? - a single vim command for “edit a new file” or “switch to existing buffer”? 在文本中的字符是unicode字符的情况下,如何替换vim中所有出现的文本? - How to replace all occurences of a text in vim where the characters in the text are unicode characters? 如何在 vim 中使用单个键绑定切换自动格式化? - How to toggle automatic formatting with a single keybind in vim?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM