Note 1 As I said in the comment this kind of box is not a good thing: it distract the eye from the important part of your file (the comment itself or the actual code). There are better way to create headers. Now this is only my opinion and this site is not here to argue about this so here is a solution.
Note 2 I give here a solution with the plugins I know but you could probably use any snippet manager other than Ultisnips.
Note 3 Using a snippet manager only for this snippet is probably overkill and you should probably considering creating your own vimscript function. For example, here is an answer I made (with a great help from @Peter Rincker) for a similar question. It uses only vimscript and creates headers a little lighter.
What you are looking for is an intelligent snippet, it is also a built-in feature of Ultisnips + vim-snippets:
First, install ultisnips which is a snippet engine (it will allow you to work with snippets but doesn't provide the snippets themselves).
Then you have two ways to get the snippet you're looking for:
Method 1 You can install vim-snippets which provides a whole list of predefined snippets.
Method 2 You can declare the snippet by yourself (which is a little bit more work but is lighter since you get only the snippet you need):
First create a file ~/.vim/my-snippets/Ultisnips/all.snippets
.
Then in this file add this:
global !p
def make_box(twidth, bwidth=None):
b, m, e, i = (s.strip() for s in get_comment_format())
bwidth_inner = bwidth - 3 - max(len(b), len(i + e)) if bwidth else twidth + 2
sline = b + m + bwidth_inner * m[0] + 2 * m[0]
nspaces = (bwidth_inner - twidth) // 2
mlines = i + m + " " + " " * nspaces
mlinee = " " + " "*(bwidth_inner - twidth - nspaces) + m
eline = i + m + bwidth_inner * m[0] + 2 * m[0] + e
return sline, mlines, mlinee, eline
def get_comment_format():
""" Returns a 4-element tuple (first_line, middle_lines, end_line, indent)
representing the comment format for the current file.
It first looks at the 'commentstring', if that ends with %s, it uses that.
Otherwise it parses '&comments' and prefers single character comment
markers if there are any.
"""
commentstring = vim.eval("&commentstring")
if commentstring.endswith("%s"):
c = commentstring[:-2]
return (c, c, c, "")
comments = _parse_comments(vim.eval("&comments"))
for c in comments:
if c[0] == "SINGLE_CHAR":
return c[1:]
return comments[0][1:]
endglobal
snippet box "A nice box with the current comment symbol" b
`!p
box = make_box(len(t[1]))
snip.rv = box[0]
snip += box[1]
`${1:content}`!p
box = make_box(len(t[1]))
snip.rv = box[2]
snip += box[3]`
$0
endsnippet
The part in the global/endglobal
are python helper function made to create a box and get the comment character for the buffer. The part in the snippet/endsnippet
is the actual snippet using the python interpolations.
Then independently from the method you followed, you simply have to type box
and then use the key you defined in your vimrc to trigger a snippet and the box will be created. You'll then enter insert mode allowing you to type the text you want to put in a box, the length of the box will be updated while you enter the text.
Note that I didn't create the code in this answer, it comes directly from the vim-snippets directory here and here
Some interesting resources about snippets:
- I asked a question to differentiate the different snippets managers. As long as you choose snipmate compatible snippets engines, the one I gave you should work.
- Vimcast made some good episodes about Ultisnips.
- And of course the ultisnip doc is a good place to start.
&comments
and&textwidth
to make it language-independent (when possible, that is).//// text utilities
. Your example looks like you want function banners.