1

example:

fun! s:MyScript(startline,endline)

python3 << endpython
import vim

startline = vim.eval("a:startline")
endline = vim.eval("a:endline")

for n in range(startline, endline + 1):
    line = vim.current.buffer[n - 1]

#===========================
# start input lines

if re.search(...,line):
   do this or that

# end input lines
#===========================

    vim.current.buffer[n - 1] = line

endpython
endfun

I want to insert python code dynamically between start and end input lines in above script. I know that I can use Vim's input field (Let question = ....) to do certain things with "line" p.e. line = eval(myinputlline) but how can I insert python code like if re.search(....) and more lines at once?

17
  • Save the code to a file and use py3file. Commented Dec 18, 2019 at 13:07
  • @phd` Hi, You mean all code or only the "input code"? Commented Dec 18, 2019 at 19:19
  • 1
    Save Python code in a separate (from vim) file. Commented Dec 18, 2019 at 19:20
  • @phd but what about the vim script? I still don't understand what you mean. Commented Dec 18, 2019 at 21:21
  • Instead of python3 << endpython you run py3file file.py. To change filenames at the execution time use execute: :execute 'py3file ' . filename. Commented Dec 18, 2019 at 21:29

1 Answer 1

1

This works for me: file test-vim.py:

import vim

startline = vim.eval("line(\"'<\")")
endline = vim.eval("line(\"'>\")")

print(startline)
print(endline)

I started vim somefile. Selected a few lines from 6 to 3. Ran :py3file test-vim.py. vim reported

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

1 Comment

Not the answer of my question but I can live with it :) Thanks.

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.