4

I am attempting to learn more about scripting vim and writing plugins with python, but I'm having trouble getting started. Although I've read :h python (first step, read the manual), I had trouble parsing it.

So I thought it would be a good idea to try to understand a very simple python plugin.In this question, a user wants to make a box like

################################################################################
#################################### LOAD ######################################
################################################################################

around words that they type. How can we do this in vim through python? Specifically, I would like to write a function MyPythonFunction() and be able to have the line

some words

with my cursor on that line, perform :call MyPythonFunction(), and have this function (written as much as possible in python) create the block of hashes with the line of text centered in the middle row.

  • @aharris88 Did you read my question and that question? This is entirely about python. That has nothing to do with python. I chose this challenge for no other reason than I liked that question – davidlowryduda Feb 5 '15 at 23:07
  • 1
    Yes, you're right. Sorry. I didn't realize the distinction between python and vim. – aharris88 Feb 5 '15 at 23:10
  • 7
    Hmm, this one is borderline, but I'd consider this more of a question for Stack Overflow. I won't closevote, but I'm worried questions like these are going to turn the site into just an SO clone. – Doorknob Feb 5 '15 at 23:11
  • Is your question about the python part or about how to interface the python code with vim? I agree with @Doorknob that "how do I write a python snipped that does this" is off topic and should be asked on SO. If you however know how to do this in pure python, but just don't get it to work with the vim api, that would be on topic but feels very broad. IMO you should instead focus on a specific part of the python<->vim interfacing that you cant figure out (e.g. "How do I replace the current line in vim from a python script"). – l4mpi Feb 6 '15 at 11:36
  • 2
    Just in case this doesn't get reopened: paste.ubuntu.com/10093002 - no comments, the code is fairly readable. – muru Feb 6 '15 at 15:04
5

To use Python in Vim, your Vim must be compiled with support for it. Most Linux distributions (Debian-based, CentOS, Arch, for example) have support for Python2, but not for Python3. To check for Python support you can use:

:echo has('python')
:echo has('python3')

If it echoes 1, it means Vim has this feature (python is Python 2). If it echoes 0, it means Vim doesn't have this feature.


The simplest way to use Python is to do:

:python some statement

However, this will become unwieldy very quickly, so instead we can do:

:python <<EOF
print('it is an')
print('ex-parrot')
EOF

Now, to get to the question. This can be accomplished using a fairly simple function:

function! PythonBox()
    python <<EOF
import vim
this_buffer = vim.current.buffer
(row,col)   = vim.current.window.cursor
this_line   = vim.current.line
this_buffer.append ('#'*80, row - 1)
this_buffer[row] = '{:#^80}'.format(' ' + this_line + ' ')
this_buffer.append ('#'*80, row + 1)
EOF
endfunction

Notes:

  • @Carpetsmoker what's an ex-parrot? A Vim in-joke? – muru Feb 7 '15 at 18:01
  • Oh, that's interesting. I did not mean to edit this post. I wonder if upvoting+accepting had something to do with that. Two servers got out of order somewhere or something. – davidlowryduda Feb 7 '15 at 18:05
  • @mixedmath I rolled back yet again, then. :D – muru Feb 7 '15 at 18:06
  • 3
    No, it's a Monty Python reference :-) I would have expected that all Python programmers are familiar with it, given that there are quite a few Monty Python references in Python documentation and such. – Martin Tournoij Feb 7 '15 at 18:06
  • @Carpetsmoker I'm familiar with Monty Python (the movies, anyway) but not with that particular one. :D – muru Feb 7 '15 at 18:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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