9

I frequently use Programming Puzzles and Code-golf. Lots of users over there have written custom languages purely for golfing. I'm even working on my own right now. Lots of these languages have their own custom encodings so that they can squish more commands into a single byte. For example, seriously, jelly and 05AB1E.

Can I configure vim to use my own custom encoding? I'd like to configure a mapping of bytes to unicode code-points. For example:

0x01 == "¢"

Then, when I type <C-v><C-a>, it would display a ¢ character, and when I write, it would save the byte 0x01.

Is this even remotely possible? And also, would this work well with the system clipboard? (e.g., I can copy and paste from my browser to vim and vice-versa)

  • 2
    You would probably need to write your own syntax script and use :help conceal. – romainl Jun 7 '16 at 7:19
  • By looking at the Vim help for encoding and charconvert I saw that it internally tries to use the iconv function first to convert character sets. iconv can handle conversion between different character sets. An answer on to how to add a character set to iconv can be found here (seems you have to mail the maintainer). Maybe this helps you. :) – Erwin Rooijakkers Jun 11 '16 at 17:46
7
+50

As romainl suggested, you can achieve so with the conceal feature of vim.

Here is a script example that can be used:

let rules = [
      \ ['01' , '☺'],
      \ ['02' , '☻'],
      \ ['03' , '♥'],
      \ ['04' , '♦'],
      \ ['05' , '♣'],
      \ ['06' , '♣'],
      \ ['07' , '•'],
      \ ['08' , '◘'],
      \ ['^A' , '¢'],
      \ ]

for [value, display] in rules
  execute "syntax match vartest /".value."/ conceal cchar=".display
endfor

" Ensure the character is concealed
set conceallevel=2
set concealcursor=nvc

As you can see, I took some of the seriously mapping and your ^A example. Careful if you want to copy/paste this code, the ^A needs to be typed <C-v> <C-A>.

What this code does is, it define for each value/display pair a syntax matching.

e.g.

syntax match vartest /08/ conceal cchar=◘

Will match every 08 sequence and the file and conceal them with the character.

You will see , but the actual text in the file is 08.


You can then take a list of rules and (with a fancy macro, i.e. for seriously: 0i\ ['^[f s', '^[lxf)s'],^[ld$) transforme each line into a usable vim array.

Also, if you want to type literal unicode with vim, you can use this workflow:

<C-V> u XXXX

Where XXXX is the unicode value, so typing <C-V>u0001 will insert ^A as desired.


If you want to verify the content of the file, I recommend using a hex dump of the file, that you can have with xxd.

e.g.

$ vim

<C-V><C-A><C-V><C-B>
:x a.txt

$ xxd a.txt
0000000: 0102 0a                                  ...

You can see here your ^A as 01 and your ^B as 02. (0a represents the new line at the end of the file)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.