8

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

Iazel
  • 2,296
  • 19
  • 23
  • You probably want to add `c` at the end to confirm, or do you want to replace every vowel in your document ? – Xavier T. Sep 10 '13 at 15:11
  • Not everyone, only vowels followed by `'`. In Italy some people have the bad habit to writes the accented vowel as `a'`, especially the uppercase ones (Window is to blame: no easy way for them). – Iazel Sep 10 '13 at 15:22

2 Answers2

5
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

FDinoff
  • 30,689
  • 5
  • 75
  • 96
  • A small typo: in the `:s` command, the call to Digraph should be `Digraph(submatch(1), "!")` – Iazel Sep 10 '13 at 18:53
2

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

lreeder
  • 12,047
  • 2
  • 56
  • 65
  • If I understand lazel correctly, I think (s)he's wanting programmatic access to `digraph-table`, which is pretty expansive. Unfortunately, it doesn't seem possible to access this except via simulating keystrokes to type Ctrl-K . – Conspicuous Compiler Sep 10 '13 at 16:16