Here's an alternative approach:
Doing it manually with built in commands
To convert a line that contains just your title you can do the following:
Run the command
:center to move the title to the centre of the line.*
To add the
# characters type the following normal mode commands:
If this is not self explanatory, just try typing them in!
* Vim calculates the position based on your
'textwidth' setting, using a value of
'textwidth' is set to 0. You can pass in a different value if necessary:
To add an "end of section" line below the cursor, you can do the following:
Type in the normal mode command to add a line containing a single
# character o#Esc
Run the command
:right to right align it.
Fill in the entire line with
# characters with the normal mode commands: Vr#
Creating command-line commands
You can easily convert these into commands:
:command StartComment center | execute "normal! hhv0r#ywA \<Esc>p"
:command EndComment execute "normal! o#" | right | normal! Vr#
Note that we use
:execute in the first command in order to more easily include an Esc keypress, and we use it again in the second line in order to end insert mode so we can continue with another ex command.
Creating normal mode mappings
And it's even easier to create mappings instead:
:nnoremap <leader>c :center<CR>hhv0r#ywA <Esc>p
:nnoremap <leader>e o#<Esc>:right<CR>Vr#
Eagle-eyed readers might have spotted that depending on whether your title has an odd or even number of characters, the resulting comment will end in the 79th or 80th column. If this is an issue, here's a slightly more complicated mapping that always creates an exactly 80 character heading by first creating an entire line of 80
# characters and then moving the title into the middle of that line with blockwise visual mode yank/put:
:nnoremap <leader>c :center<CR>o#<Esc>:right<CR>Vr#kA <Esc>v^hyj1vpkdd
It's left as an exercise to the reader to convert this into a command-line command.
Watch out for
Note that none of the above will work well if you have
'expandtab' set, because each tab will be replaced with a single
# character. But you're writing Python, so of course you're indenting with the PEP 8 mandated four spaces, so this won't be an issue!