VIM: Replace [aeiou]' with the respective accented letter

884 views Asked by At

I know that VIM support digraph, and it would be perfect if it's possible to use with :s command, but I can't find a way to use it!

I think something like:

:%s/\([aeiouAEIOU]\)'/\=digraph(submatch(1)."!")/g

Would be perfect, but I didn't find a digraph function. Thanks in advance.

EDIT
Ok, after a bit of diggin in the built-in VIM's functions, I've found tr and a first solution to the problem:

:%s/\([aeiouAEIOU]\)'/\=tr(submatch(1), 'aeiouAEIOU', 'àèìòùÀÈÌÒÙ')/g

However, I still want to know if there's a way to use digraph in expressions :)

2

There are 2 answers

1
FDinoff On BEST ANSWER
function! Digraph(letter, type)
    silent exec "normal! :let l:s = '\<c-k>".a:letter.a:type."'\<cr>"
    return l:s
endfunction

This function will allow you to generate any digraph you want.

It simulates typing <c-k><char><char> by running it with the normal command and assigning it to the local variable s. And then it returns s.

After this function is defined and you can use it like this.

:%s/\([aeiouAEIOU]\)'/\=Digraph(submatch(1), "!")/g

Note: This was based off of the source code for EasyDigraph

1
lreeder On

Here's another approach using a hand-coded vim function (add to your vimrc):

" get a matching digraph for a given ASCII character
function! GetDigraph(var1)
   "incomplete dictionary of digraphs, add your own....
   let DigDict = {'a': 'à', 'e': 'è', 'i': 'ì'}
   "get the matching digraph.  If no match, just return the given character
   let DictEntry = get(DigDict, a:var1, a:var1)
   return DictEntry
endfunction

Call it like this :%s/\([aeiouAEIOU]\)'/\=GetDigraph(submatch(1))/g