Copy the decimal equivalent of a hex number
From Vim Tips Wiki
Tip 448 • Previous Tip • Next Tip
Created: March 25, 2003 Complexity: intermediate Author: Mohit Kalra Minimum version: 6.0 Karma: 13/6 Imported from: Tip#448
Here is a mapping that will copy a hexadecimal number in a register after converting it to the decimal equivalent. The tip is pretty useful if you are a programmer.
:map \y g*<esc>:let @*=@/ + 0<enter>
Usage:
- Place the cursor on any hexadecimal number (eg 0xff, 0xfefe, 0x3434) and press \y.
- Place the cursor at the location where you want to paste the number in decimal and press "*p
- The number is also copied to the clipboard (windows) so you can paste it in other applications.
Example: If the hexadecimal number is 0xff, then 255 will be copied to the clipboard.
[edit] Configuring the tip
If you do not like the above key combinations or the register being used, you can configure the tip to use other mappings as explained below:
Change \y in the above mapping to any key combination of your choice.
Change @* to @<any_other_lower_case_letter> to copy the contents to another register. If you do this, pasting will require the command "<that_same_lower_case_letter>p
[edit] Side effects
The tip uses the search register for the conversion. Therefore any last search will be lost.
The tip also uses a register to yank the result. The earlier contents of that register (in our case the * register) will be lost.
[edit] Comments
There is a way of doing this without changing the search history
map \y :let @*=<C-R><C-W> +0<cr>
And a way to not change the " * " register contents would be to use some global variables instead I guess...Some think like
map \y :let g:HexYank=<C-R><C-W> +0<cr>
for yanking, and..
map \p :if exists("g:HexYank") <Bar> exe "normal i" . g:HexYank <Bar> endif <cr>
for pasting
"<some-register>yiw
should also work instead of searching.
