Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to type a Unicode character using its UTF-8 sequence in Vim?

Tags:

vim

unicode

is the currency symbol for North Korea. Its Unicode code-point is U+20a9.
In insert mode, I can press Ctrl-V u20a9 to type it.
If I only know its UTF-8 form e2 82 a9, how can I type it easily?

like image 672
kev Avatar asked Aug 04 '12 23:08

kev


People also ask

Does UTF-8 include Unicode?

UTF-8 is an encoding system for Unicode. It can translate any Unicode character to a matching unique binary string, and can also translate the binary string back to a Unicode character. This is the meaning of “UTF”, or “Unicode Transformation Format.”

How do I insert a Unicode character?

Inserting Unicode characters To insert a Unicode character, type the character code, press ALT, and then press X. For example, to type a dollar symbol ($), type 0024, press ALT, and then press X. For more Unicode character codes, see Unicode character code charts by script.

Does Vim support Unicode?

Tip of the Trade: The editor Vim supports unicode natively. There are three ways to take advantage of unicode's capabilities via Vim. The one that's best for you will depend on your setup and needs. character codes are available online.


2 Answers

I just found this solution:

In insert mode, press Ctrl-R ="\xe2\x82\xa9" Enter.

I'd like to know about any other (shorter?) methods, though.

like image 112
kev Avatar answered Sep 25 '22 17:09

kev


Same solution with a twist of automation to help remember it:

command! -nargs=* UTF8 call EncodeUTF8(<f-args>)
fun! EncodeUTF8(...)
   let utf8str = ""
   for i in a:000
      let utf8str .= "\\x" . i
   endfor
   exe "norm i" . eval("\"".utf8str."\"")
endfun

Now you can :UTF8 e2 82 a9

You can also type this particular character with <C-k>W=. See :help digraph-table-mbyte.

Note that you can also get information about a character with ga and g8 in normal mode. So it might be easier to just do <C-r>="\xe2\x82\xa9" once and then do ga to get the code-point.

like image 45
Conner Avatar answered Sep 23 '22 17:09

Conner