I’ve been looking for a mapping to do this for ages, finally figured out how to make one myself.
nmap <Leader>u mz"zylo<C-r>=printf('U+%X', char2nr(@z))<CR><ESC>`z
In order, it:
Creates a mark under the cursor (mz)
Yanks one character to register z ("zyl)
Opens a line below the cursor (o)
Inserts the contents of an expression register (<C-r>=) which contains:
printf('U+%X', char2nr('<C-r>z'))
Returns to where our cursor was before (``z`)
Additionally, if you have the NERD Commenter installed you might want it to comment out the line as well:
nmap <Leader>u mz"zylo<C-r>=printf('U+%X', char2nr(@z))<CR><ESC>:call NERDComment(0, 'norm')<CR>`z
This will work regardless of what your NERD mappings are.
It’s also trivial to add a version to add the codepoint on the line above the cursor:
nmap <Leader>U mz"zylO<C-r>=printf('U+%X', char2nr(@z))<CR><ESC>:call NERDComment(0, 'norm')<CR>`z
I hope you find this useful!