new VIM user. I'm trying to make creating python properties easier for my class definitions. What I would like for say I type

:pyp x

then VIM will autofill where my cursor is

def x(self):
   return self.x
   def x(self,val):
      self._x = val

or more abstractly I type

:pyp <property_name>

and VIM fills

def <property_name>(self):
   return self.<property_name>
   def <property_name>(self,val):
      self._<property_name> = val

I've looked at a few posts and the wikis on functions, macros but I'm very unsure of how to go about it or what to even look up as I am brand new VIM user, less than a week old.

I tried using this as an example, in my .vimrc but I couldn't even get that to work.


the system I am working on, its very very difficult if not impossible to get pluggins so I have to do stuff through my vimrc or other method. Basically trying to get a plugin installed could take half a year.

So the code I am currently trying is

function! PythonProperty(prop_name)
 let cur_line = line('.')
 let num_spaces = indent('.')
 let spaces = repeat(' ',num_spaces)
 let lines = [ spaces."@property",
             \ spaces."def ".prop_name."(self):",
             \ spaces."   return self.".property,
             \ spaces."@property.setter",
             \ spaces."def".prop_name."(self,val)",
             \ spaces."   self._".prop_name." = val" ]
 call append(cur_line,lines)

and I am getting the errors

E121: Undefined variable: prop_name

I am typing :call PythonProperty("x")

  • Use a: to refer to function arguments. a:prop_name is what you want. – filbranden Jul 17 '19 at 16:29

Define a list of all the lines you want to insert, and then insert them with either append() or setline(). You'll also need to extract the current indentation.


PS: half a year is almost the time we spent on validating our snippet plugins...

| improve this answer | |
  • is there a command for getting current line position and column of my cursor? – Melendowski Jul 17 '19 at 13:10
  • There are many (line('.') + col('.'), and other functions like getpos(), cursorsomething()...). Check :h functions – Luc Hermitte Jul 17 '19 at 15:06
  • I uploaded some code that I am attempting @Luc Hermitte – Melendowski Jul 17 '19 at 15:12
  • Regarding your edit, to address a parameter, you need to add its scope, which will always be a: => Use a:propname to refer to the parameter value. See :h :function – Luc Hermitte Jul 17 '19 at 15:57

the system I am working on, its very very difficult if not impossible to get pluggins so I have to do stuff through my vimrc or other method. Basically trying to get a plugin installed could take half a year.

Please note that you don't need to install plug-ins system wide and they can live completely inside your home directory (under ~/.vim more exactly.

A plug-in manager such as vim-plug can be installed by downloading a single file to ~/.vim/autoload and then it will get plug-ins by cloning them from git inside a different subdirectory under ~/.vim.

I can see how this can look somewhat worrisome from a security point of view. The reason you might say it's difficult to get plug-ins in your environment is that this code should go through a security review, and having a plug-in manager download it "on the fly" from the Internet is a problem.

But then, is that so much different than pasting potentially large chunks of Vimscript from this website and others into your .vimrc? 😁

If you're interested in a small number of plug-ins and want to manage them on your own, then consider fetching it yourself (and vetting the source code) and using Vim 8's package support to have it loaded automatically. (For Vim 7, the pathogen script does something similar, so you can use that as a fallback.)

Finally, some very popular Vim plug-ins have been packaged by Linux distributions such as Red Hat and Debian. In the process, they have been vetted by those Linux distributions. So, while this might involve requesting IT to have them installed on machines, etc., perhaps that helps with the trust and vetting issue?

If you do consider plug-ins, take a look at the UltiSnips plug-in. Or you'll also find other snippet managers which might suit your particular use case.

| improve this answer | |
  • OP is unfortunately in a pretty tight env (no external storage devices, some kind of air-gap where they dont have internet, etc.). But you are correct in general! – D. Ben Knoble Jul 17 '19 at 20:06

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.