How to enter non-ascii characters using hex or octal codes in vi

61,553

Solution 1

:help i_CTRL-V_digit

In insert mode, type Ctrl+V followed by

  • a decimal number (0-255)
  • o then an octal number (o0-o377, i.e., 255 is the maximum value)
  • x then a hex number (x00-xFF, i.e., 255 is the maximum value)
  • u then a 4-hexchar Unicode sequence
  • U then an 8-hexchar Unicode sequence

Decimal and octal numbers are limited to three digits.  Decimal numbers less than 100 may include leading zeroes, which are ignored.  Octal numbers less than 100oct (i.e., 64) may include leading zeroes, but they are not required.  Octal numbers greater than or equal to 100oct may not include leading zeroes (but you may type a leading o if you want to).

You can terminate a number by typing a character that is not a valid digit for that radix.  For example,

  • Ctrl+V    065 → A.
  • Ctrl+V    65B → Ab.
  • Ctrl+Vo041 → !.
  • Ctrl+Vo419 → !9.

Regular (one-octet) hex numbers are limited to two digits.  As with the above, you can repeat the radix character (e.g., Ctrl+Vuu0041 → A)  for characters specified by hex codes.  o and x are case-insensitive.

Solution 2

I assume that you use vim, because :helpoctal is a vim's command. On some systems vi is just a symlink to vim which runs it in vi-compatible mode.

In vim:

  • You can enter unicode characters from basic multilingual plane you can use:
    Press ctrl+v and then enter four digit hex unicode code.
  • Another option is digraphs. You can read more about them in vim's help (help: dig).
    Press ctrl+k and then two-character sequence.
    You can list sequences supported in you vim usig command :digraph .

In nvi, vi and elsewhere:

  • Ctrl+Shift and hit U and then enter unicode hex code.
Share:
61,553

Related videos on Youtube

luser droog
Author by

luser droog

Updated on September 18, 2022

Comments

  • luser droog
    luser droog over 1 year

    I'm trying to write a golfing library for postscript. But it needs to be condensed itself. So I need a convenient way to type-in arbitrary bytes within mostly ascii text.

    I know this can easily be done with absolutely any programming language, but can I do it in vi? (:help octal was no help).

    Edit: Here's the resulting golfing library for postscript. Fortunately, I realized early on that golfing the library itself was a stupid idea and I did not do that.

  • Admin
    Admin about 7 years
    I think that Ctrl+V is for entering decimal digits, if not followed by x
  • wisbucky
    wisbucky almost 6 years
    To add some more tips: the decimal number must be between 0-255. The hex number between x00-xFF.
  • mas
    mas almost 6 years
    @Carlos is correct. This answer is wrong. Alan's answer is correct.
  • ThorSummoner
    ThorSummoner over 5 years
    ascii esaple is ctrl+v x1b (for ascii color sequences)
  • Chris R. Donnelly
    Chris R. Donnelly almost 5 years
    A reminder that if you are in Windows and remapped Ctrl+V to Paste, you can use Ctrl+Q in Insert mode in its place. See stackoverflow.com/questions/426896/…