1

Question

I want to have basically the same as :read ![external command], but instead the external command I want to read the output from an internal command.

Example

Shells echo / external command: :read !echo 0x539 -> output: 0x539

Vims echo / internal command: :read echo 0x539 -> output: 1337


More explanation

The second example will obviously fail, because thats not vim syntax. But is there an equivalent how I can archive this in vim/neovim? I know that you can generally output the result from vimscript commands in the "= register. (In case you don't know, you can press CTRL-R and then = while being in insert mode to get into the expression register). But I don't know how to use echo with it.

So if you have any ideas, I would be glad to hear them :)

2 Answers 2

2

One version:

:put =execute('echo 0x539')

though I would probably do i<C-v>u0539<esc>.

You can also redir => {var} into a variable, which can be more readable than the execute() function (different from the command!) if there are multiple lines of script.

5
  • Thank you that is exactly what I wanted. And I know I could use i<CTRL-V>... but I have a lot of hex numbers and I think to use the vim-echo command is the best way to convert them all. I just took 0x539 just as an example, but still thanks :) And btw you can use <CTRL-R>=execute('echo abc') (in insert mode) too :))
    – Bog
    Nov 28, 2022 at 16:12
  • 1
    You can maybe use something like :%s/0x[a-fA-F0-9]\+/\=submatch(0)->str2nr(16)/ @Pixelbog; that finds all hex numbers starting with 0x and replaced it with the decimal variant. Nov 28, 2022 at 16:19
  • oh wow thank you very much. I learned 3 new things from you :) 1. the submatch(0) thing. 2. the whole line, really interesting what you can all do in vim. Vim is really beautiful isnt it? :) And 3. for some reason if I try to use the exact same line in neovim, it does not work. So I have now more to learn :D Thank you very much :))
    – Bog
    Nov 28, 2022 at 16:53
  • 1
    @Pixelbog yes I'm aware of i_CTRL-R. For brevity I would be inclined to use the pattern 0x\x\+.
    – D. Ben Knoble
    Nov 28, 2022 at 18:49
  • Oh wow thats so cool :D I learned something new again. I didn't know that there is an hexadecimal escape, thanks :))
    – Bog
    Nov 29, 2022 at 9:49
2

But I don't know how to use echo with it.

You don't.

What comes after :help :echo is an expression but :echo <expr> is not the only way to consume expressions. As it stands, the "= register that you use with :help i_ctrl-r is called "expression register" for a reason: it expects an expression, like :echo.

0x0539 is the expression you want to consume so, in insert mode, <C-r>=0x0539<CR> inserts 1337 at the cursor, as expected.

3
  • First of all, dude I see you almost all over the internet, if it has to do something with vim xD I love it haha. And second, maybe it is a stupid question, but do you know if I can write multiple hex numbers seperated by a comma or in an array?
    – Bog
    Nov 29, 2022 at 9:59
  • Nevermind. I should have first tested it before writing a comment haha. The Answer is yes it is possible, in array of strings seperated by commas. Like this: =['0x539', '0x45', '0x2a'] :)
    – Bog
    Nov 29, 2022 at 10:01
  • @Pixelbog a list of numbers should work, but the expression register typically turns lists into multiple lines.
    – D. Ben Knoble
    Nov 29, 2022 at 13:23

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.