0

I have defined a python function in my .vimrc to use it on vim buffers in visual mode. The function has been vnoremap-ed to <F7>. Unfortunately, all the python output appears in the :messages of vim. Therefore, I would like to ask, if it possible to redirect the output to the current file @% instead. That is, I am looking for something of the sort :r!python ./some_function.py, used for calling regular python scripts and piping the output to the current file. The function I am using adds exactly two lines to the message block.

According to :help python

    Vim displays all Python code output in the Vim message area.  Normal
    output appears as information messages, and error output appears as
    error messages.

    In implementation terms, this means that all output to sys.stdout
    (including the output from print statements) appears as information
    messages, and all output to sys.stderr (including error tracebacks)
    appears as error messages.

Based on this, I can't imagine how this can be done and if it is possible at all. I will appreciate your help. Thank you

EDIT

The flowing code can serve as a minimal example. The following lines have to be included in ~/.vimrc

python << EOF
import vim
def sum_vals():
        sum=sum(float_zero(v) for v in vim.eval('@"').splitlines() )
        print("sum:>> %15.5E \n"%sum )
EOF
vnoremap <F7> y:python sum_vals()<Enter>
" An even simpler example
noremap <F8> :python print("hello world") <Enter>

Upon pressing <F7> the above function will yank the values in a visual selected column block and print their sum to the message block.

Pressing <F8> will on the contrary show the message hello world.

I would like to get the output of the above functions it in the current file instead of the :message block.

4
  • 1
    import vim; cb = vim.current.buffer; cb.append('line'); see the docs. Commented Jan 29, 2021 at 21:28
  • What does the :vnoremap <F7> mapping do? It's probably possible to adapt it to paste the output somewhere in the buffer, but to tell you how to adapt it, I guess we need to know what it's doing right now... Commented Jan 30, 2021 at 1:36
  • 1
    @filbranden i included an example function. You have to select a column of numbers in visual mode, press f7 and the function will compute their sum. Commented Jan 30, 2021 at 14:04
  • @filbranden or <F8> will show the message hello world Commented Jan 30, 2021 at 19:19

1 Answer 1

1

To simplify using visual mode functionality, it makes sense to have an external executable. Let's create one, e.g. the first time python file is opened:

let g:python_summator_code =<< END
import sys
print(sum(int(line) for line in sys.stdin))

END
augroup useful_python_scripts
  autocmd!
  au FileType python :let g:python_summator=trim(system("mktemp"))
  au FileType python :call writefile(g:python_summator_code, g:python_summator, "b")
  au FileType python :vnoremap <expr><F7> "!python " . g:python_summator . " \<Enter>"
augroup END

Now you are ready to do the following:

  1. source configs or reload vim
  2. open python file
  3. visually select a few lines with numbers
  4. press F7
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.