3

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

4
  • This looks more like a superuser.com question. Commented Dec 2, 2009 at 16:33
  • Never heard of superuser.com ... Reading the faq on both stackoverflow and superuser still leaves me unsure where is the right place to ask it. However, I posted it there and appreciate the suggestion. Commented Dec 2, 2009 at 16:46
  • 2
    I think it's appropriate to ask this question here (on stackoverflow) because 1) vim is an IDE for many and 2) the solution to this problem might entail some coding. Commented Dec 2, 2009 at 18:00
  • Thank kind of "problem" as been evoked there: meta.stackexchange.com/questions/5790/… The reactions make me think that SO should host no vim questions at all... Commented Dec 2, 2009 at 18:43

1 Answer 1

7

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.