Please don't do that: This type of title comments is just too heavy and
doesn't improve the readability of your code. If you really need to create a
section use light multi lines comments like:
"
" Solving a big problem
"
That's much easier to read. So now that I gave my totally subjective opinion,
which you don't have to agree with:
Here's a function to let you do what you want
function! MyComment(...)
" get the arguments properly
if (a:0 == 0)
let argString = ""
let argChar = "-"
elseif (a:0 == 1)
let argString = a:1
let argChar = "-"
elseif (a:0 == 2)
let argString = a:1
let argChar = strpart(a:2, 0, 1)
else
echom "Too many arguments"
return
endif
" Get the characters to use at the beginning and the end of the title
let result = substitute(&commentstring, "commentstring=", "", "")
let resultList = split(result, "%s")
let Comment = resultList[0]
if (len(resultList)>1)
let EndComment = resultList[1]
else
let EndComment = Comment
endif
" create the comment title
let nb = (80 - strlen(argString) - len(Comment) - len(EndComment)) / 2
let i = 0
let commentString = argString
while i < nb
let i = i + 1
let commentString = argChar . commentString . argChar
endwhile
" append one more char if necessary
if strlen(argString) % 2 != 0
let commentString = commentString . argChar
endif
let commentString = Comment . commentString . EndComment
" Put the title in the buffer
exe ":normal o"
exe ":normal i" . commentString
endfunction
You can call the function in 3 different ways:
:call MyComment()
will insert an "empty title" of 80 characters like this:
//-------------------------------------------------------------------//
:call MyComment("Title section")
will insert a title with the default -
character like this:
//--------------------------Title section----------------------------//
:call MyComment("Another title", ".")
will let you choose the character to
repeate in your title (If the second string contains more than one
character, only the first one will be used):
//...........................Another title............................//
Also with this function if you are editing a C
file, using :call
MyComment("Hello world")
will insert:
/*--------------------------------Hello world---------------------------------*/
In a java
file you'll get:
//--------------------------------Hello world---------------------------------//
And a php
file:
/*--------------------------------Hello world---------------------------------*/
Improvements
For now the function doesn't takes into consideration the indentation level,
if I get motivation I'll edit my answer otherwise I'll let it as an exercise
for the reader ;-) (remember that you can get the indent level of a
line with :foldlevel(x)
this
page
might help).
It would also be possible the create a command to shorten the call of the
function, this tricky part might be to handle the optional number of
arguments.
EDIT In the comments @Peter Rincker suggested a much more elegant solution
than mine. With his
authorization I'll present here a new version inspired by his function and the
explanation of the different tricks he used:
function! MyComment(...)
" get the arguments properly
let commentText = get(a:000, 0, '')
let commentChar = get(a:000, 1, '-')[0]
" Get the number of char to add on left
let len = (80 - len(commentText) - len(printf(&commentstring, '')))
let left = len / 2
let right = len - left
" force the title with repeat() function and insert it in the buffer
put=printf(&commentstring, repeat(commentChar, left) . commentText . repeat(commentChar, right))
endfunction
First the part where he gets the arguments:
let commentText = get(a:000, 0, '')
According to :h function-argument
a:000
is a dictionary containing all the arguments, then a:000[0]
is
equivalent to a:1
The function get()
third parameter is a default value.
This syntax allows to greatly reduce the ugly if...else
structure that I
originally used.
Another cool trick is to use printf(&commentstring, '')
to replace the %s
part
of the string. Once again much cleaner than my substitute
workaround
And finally the reason I used his function: the use of repeat()
:
printf(&commentstring, repeat(commentChar, left) . commentText . repeat(commentChar, right))
The function repeat the expression passed as first parameter, the number of time
passed as second parameter and return the concatenated result. This way in a
single line two loops are written in a very readible format.
Many thanks to Peter for the improved version!