开发者

How to replace char in file with appropriate ascii code in vim

开发者 https://www.devze.com 2022-12-13 05:32 出处:网络
In a vim buffer if I have a list of characters say: A B c C d D and for each one I want to replace it with its correspondi开发者_运维技巧ng ascii code ( in decimal ).Is there a way to do this w

In a vim buffer if I have a list of characters say:

A

B

c

C

d

D

and for each one I want to replace it with its correspondi开发者_运维技巧ng ascii code ( in decimal ). Is there a way to do this without using an external tool through :r!some_tool

For instance, I know there is the :ascii and ga commands but they print the value to the screen but I can't find a way to get its output into the buffer.


You can take advantage of the sub-replace-expression functionality along with char2nr() and submatch(). The following replaces any alphabetic character at the start of the line with its decimal equivalent.

%s/^\a/\=char2nr(submatch(0))/

If you want to do this for any character at the start of the line, simply replace the \a with ..

0

精彩评论

暂无评论...
验证码 换一张
取 消