Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatic insertion of a colon after 'def', 'if' etc

Tags:

python

vim

I recently switched to Vim at the request of a friend after Sublime Text 2 decided it didn't believe a module was installed even though it was...I digress.

I've managed to set up some stuff to make editing Python (currently me only language) easier. However, there's one feature I'm missing from Sublime. It would automatically add a colon to the end of lines which require them (the beginning of function definitions, if statements etc.). This prevented countless annoying errors and I miss it :P

I was wondering whether there was some sort of command I could put in .vimrc to do this.

An example: If were to type def, I would like vim to automatically insert a colon to make it def : and place the cursor before the colon ready for me to type my function name.

Cheers and apologies if I'm being stupid in any way.

like image 562
Whonut Avatar asked Jul 16 '12 15:07

Whonut


3 Answers

Rather than use imaps like @CG Mortion's answer suggests, I would strongly advise you to use iabbrs for these sorts of small fixes instead.

With an imap you would never be able to type "define" in insert mode unless you paused between pressing 'd', 'e', or 'f', or did one of a number of other hacky things to prevent the mapping from happening. With iabbr the abbreviation is only expanded once a non-keyword character is pressed. iabbr def def:<left> works exactly as you'd expect and it doesn't have the drawbacks of imaps.


As mentioned in other answers, I would also suggest you move on to a snippet engine once you decide that you want something a little more complex. They are very powerful and can save you loads of time.

like image 115
Randy Morris Avatar answered Oct 26 '22 21:10

Randy Morris


Take a look at the old snipmate, the new snipmate and ultisnips. These plugins provide roughly similar "snippet-expansion" mechanisms modeled after TextMate's system.

like image 32
romainl Avatar answered Oct 26 '22 20:10

romainl


This snippet doesn't do exactly what you are asking, but it may still be useful to you.

Basically, it puts a semicolon at the end of a line (if the line starts with predefined words, and the colon is not there yet) when you press Enter.

inoremap <cr> <c-r>=ColonCheck()<cr><cr>

fun! ColonCheck()
  if getline(".") =~ '^\s*\(if\|else\|elif\|while\|for\|try\|except\|finally\|class\|def\)\>.*[^:]\s*$'
    return ":"
  else
    return ""
  endif
endfun
like image 1
Paolo Moretti Avatar answered Oct 26 '22 21:10

Paolo Moretti