如何在VIM中配置元音字母后跟’的自动替换规则(含E/e例外)
Custom Accented Character Replacement for Vim
Hey there! Based on your specific replacement rules (even though it’s not strict Italian orthography, it’s a solid starting point), here’s how you can set up the <leader>-triggered normal-mode mapping in your .vimrc to handle all those accented characters automatically:
The Full Mapping
Drop this line right into your .vimrc file:
nnoremap <leader>r :%s/\v(^|\s)([eE])'/\1\=toupper(submatch(2)) ==# 'E' ? 'È' : 'è'/g | %s/\v([^[:space:]])[eE]'/\1\=toupper(submatch(2)) ==# 'E' ? 'É' : 'é'/g | %s/\v([aAiIoOuU])'/\=submatch(1) ==# 'A' ? 'À' : submatch(1) ==# 'a' ? 'à' : submatch(1) ==# 'I' ? 'Ì' : submatch(1) ==# 'i' ? 'ì' : submatch(1) ==# 'O' ? 'Ò' : submatch(1) ==# 'o' ? 'ò' : submatch(1) ==# 'U' ? 'Ù' : 'ù'/g<CR>
How It Works (Breakdown)
Let’s walk through each part so you know exactly what’s happening:
- First substitution: Handles the
e/Eexception for leading cases. It matchese'orE'that comes right after a line start (^) or space (\s), then replaces it withèorÈrespectively, stripping the single quote. - Second substitution: Takes care of non-leading
e/Ecases. It matchese'orE'that follows any non-space character, replacing it withéorÉ. - Third substitution: Handles all
a/i/o/u(uppercase and lowercase) cases. It matches any of these letters followed by a single quote, then swaps them out for their corresponding accented versions (àìòùÀÌÒÙ) and removes the quote.
Quick Notes
- You can replace
<leader>rwith any trigger key combo you prefer (e.g.,<leader>afor "accents")—just swap out therfor your key of choice. - This runs on the entire file (
%range) when you press the leader combo in normal mode. If you want to run it only on a selected range, you can adjust the mapping to use'<,'>instead of%, but the current setup works great for full-file fixes.
内容的提问来源于stack exchange,提问作者Michele
相关产品推荐
相关产品推荐

