History Report a problem
Article Edit this page Discussion

Copy the decimal equivalent of a hex number

From Vim Tips Wiki

Jump to: navigation, search
 

Tip 448Previous TipNext 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:

  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.

[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.


Rate this article:

Share this article:

Hubs Highlights International Sites Wikia messages
Entertainment
Gaming
Cartoons & Comics
Science Fiction
Hobbies
Sports
See all...
Grand Theft Auto
Terminator Wiki
Legend of Zelda Wiki
Flash Gordon
Everquest II Wiki
Yo-Yo Wiki
German
Spanish
Chinese
Japanese
More...
Wikia is hiring for several open positions


Vote for featured Wikia!

Send this article to a friend
"Copy the decimal equivalent of a hex number"
 
 
Hi!

I thought you'd like this page from Wikia!

http://vim.wikia.com

Come check it out!
Send confirmation