Vim Tips Wiki
Advertisement
Tip 448 Printable Monobook Previous Next

created March 25, 2003 · complexity intermediate · author Mohit Kalra · version 6.0


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:

  1. Place the cursor on any hexadecimal number (eg 0xff, 0xfefe, 0x3434) and press \y.
  2. Place the cursor at the location where you want to paste the number in decimal and press "*p
  3. 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.

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

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.

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.


Advertisement