Morning Guys,

I want to to add some functions to my vimrc and want to achieve e.g.: type :bang into vim => it adds a shebang into the file. Or when I type class foo:<ENTER> it auto add except KeyboardInterrupt: 2-3 lines after.

I have my own colors and rewrote some parts of the python syntax. - so I am a little bit familier with vim. But thats a big little and not enough.

could you point me in some direction?

thanks K.

  • The given answer is good. If you want something more powerful, though, you should look at using a snippet plugin like Ultisnips. You'll find snippets for all major programming languages and you can easily add your own.
    – B Layer
    Sep 1 '21 at 23:05

I think you want to learn about insert mode abbreviations.

:iabbrev :bang #!/usr/bin/python3.9

When you type :bang in INSERT mode, the text will be replaced to #!/usr/bin/python3.9 when you trigger the abbreviation. Abbreviations are triggered when you hit the space bar or return key (among other ways).

For an abbreviation with multi-lines, use the <cr> for carriage return.

:iabbrev foo: foo:<cr><cr>    except keyboardInterrupt
  • looks nice but it does not work: got always on error loading my vimrc Tried with :iabbrev or set iabbrev and just iabbrev Vim version 8.1.2269
    – somboku
    Sep 1 '21 at 15:34
  • 1
    @user37716 what error? Sep 1 '21 at 16:03
  • well, "command not found", but got it right now and wanted to reply. :) I had to add it like: :ab try try:<cr>except keyboardInterrupt: in my vimrc but now I have an indent after that <cr> Do vim now mixing up with python syntax?
    – somboku
    Sep 1 '21 at 16:39
  • I don't understand why :iabbrev wouldn't work. That's a valid command. You probably spelled it wrong. The indent appeared because your filetype is set to python and you have auto-indentation for python. Return after a colon will indent. Sep 1 '21 at 17:13
  • omg. i cant be that fool. you were right. just tested it again. so, goal achieved and brought another easter egg. do you think there is a workaround for this indent - as i dont want it to turn off. i've read about special characters, maybe to put a backspace after <cr>?
    – somboku
    Sep 1 '21 at 17:33

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.