6

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.

Vivian De Smedt
  • 16,336
  • 3
  • 18
  • 37
davidlowryduda
  • 2,424
  • 19
  • 27
  • @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 05 '15 at 23:07
  • 1
    Yes, you're right. Sorry. I didn't realize the distinction between python and vim. – aharris88 Feb 05 '15 at 23:10
  • 7
    Hmm, this one is borderline, but I'd consider this more of a question for [so]. I won't closevote, but I'm worried questions like these are going to turn the site into just an SO clone. – Doorknob Feb 05 '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 06 '15 at 11:36
  • Note: The question is now closed with marked as duplicate, since more people voted on that. But I think unclear what you're asking would be better (I voted for that option)... You could make the question better, and ask for re-open! – Martin Tournoij Feb 06 '15 at 13:02
  • 2
    Just in case this doesn't get reopened: http://paste.ubuntu.com/10093002/ - no comments, the code is fairly readable. – muru Feb 06 '15 at 15:04
  • @muru Thank you, that's exactly what I was looking for. If this question were still open and you answered, I would upvote and accept. – davidlowryduda Feb 06 '15 at 18:27

1 Answers1

7

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:

muru
  • 24,838
  • 8
  • 82
  • 143