How can I put multiple calculations in a vim text document?

I would like to insert the output of several calculations into my text document:

2**3 = (the result)   
2**4 =         
2**30 =


(** = exponential)
Can anyone help me?

It would be nice to have immediate scripts in vim without using .vimrc, just to do quick operations

for i in range (3,30)
print "2**".i."=".2**i


source to share

4 answers

You can write for...loop

like this:

:for i in range(3, 30) | call setline(i-2, printf('2**%d = %d', i, float2nr(pow(2, i)))) | endfor


Another way is to redirect messages to a file:

:redir! >output.txt
:for i in range(3, 30) | echo printf('2**%d = %d', i, float2nr(pow(2, i))) | endfor
:redir END
:e output.txt




If you prefer Python and your vim is compiled with support, you can use it:

:py import vim
:py for i in range(3, 30): vim.current.buffer.append("2**%d = %d" % (i, 2**i))


For more complex skills, you can write a block of code instead of oneliners. This is the way to do it, it's just not very convenient to do it on the interactive command line, but it is possible:

:py << EOF
import vim
for i in range(3, 30):
  for j in range(1, 3):
    vim.current.buffer.append("{}x{}".format(i, j))


More complex stuff is meant to be written in script files and source them as needed.



You can do math in vim using special case =

, for example if you enter insert mode



You get



PS <C-R>

means CTRL + r <CR>

means hitting return.



Also works

write some lines like python

total = 0.0
for i in range(10):
   total += i*i
print total


then select the lines visually (with shift-v

) and enter :!python

. What you should really see is :'<,'>!python

that the lines you typed should be replaced with the output of lines in python.



All Articles