2

Although I have been using Vim for quite some time now, I am still a novice when it comes to customizing it. I am not sure if what I have in mind can be done. Here what I would like to do: When writing a bash script I have a template to comment a function

1  #!/bin/bash
2
3  ###########################################################
4  # template
5  # globals:
6  #   none
7  # returns:
8  #   none
9  ###########################################################
10
11
12 my_function () {
13 ...

What I am currently doing is to position the cursor at line 11 and then issue the command:

:3,9y

This yanks lines 3 to 9 into the buffer. Then I hit p and it pastes the lines after the cursor. I would like to simplify this process. Can I achieve the above behavior by defining something like a macro? Ideally I would define it the .vimrc file and not rely on the script to have the template exaclty at lines 3 through 9.

  • Have you searched for a vim add-on that will do this? I'm pretty sure there's at least one on vim.org. – Herb Wolfe Nov 14 '16 at 13:39
2

You could place the text of your banner in a named register:

let @c  = "###########################################################\n"
let @c .= "# template\n"
let @c .= "# globals:\n"
let @c .= "#   none\n"
let @c .= "# returns:\n"
let @c .= "#   none\n"
let @c .= "###########################################################"

and put it with:

"cp
  • That is exactly what I was hoping/looking for. Thanks. – nautical Nov 14 '16 at 16:50
1

snippets are like the built-in :abbreviate on steroids, usually with parameter insertions, mirroring, and multiple stops inside them. One of the first, very famous (and still widely used) Vim plugins is snipMate (inspired by the TextMate editor); unfortunately, it's not maintained any more; though there is a fork. A modern alternative (that requires Python though) is UltiSnips. There are more, see this list on the Vim Tips Wiki.

There are three things to evaluate: First, the features of the snippet engine itself, second, the quality and breadth of snippets provided by the author or others; third, how easy it is to add new snippets.

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.