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?

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

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


+3


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

      

+2


source


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))
EOF

      

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

+3


source


You can do math in vim using special case =

, for example if you enter insert mode

2+3=<C-R>=2+3<CR>

      

You get

2+3=5

      

PS <C-R>

means CTRL + r <CR>

means hitting return.

+3


source


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.

+1


source







All Articles