How to create a command that transforms this text

# Title

Into this text

# Title #
  • Is there a name for this kind of text? I have a hard time searching for this in search engines.
  • I don't use this in well structured code packages, but I use this kind of titles in lengthy scripts, to break down the code in sections.
  • 1
    "Is there a name for this kind of text?" - I'd call it a "frame" or - more generally - a "decoration". Also, "border" seems to be used. Google for any of these plus "ascii". These things are so common, I don't think there's a canonical name.
    – Friedrich
    Commented Feb 28, 2023 at 18:53

1 Answer 1


These comment boxes are called ascii frames, decorations or border. Adding the following to a .vimrc creates a :Title command that replaces a comment by a title in an ascii frame:

autocmd FileType python command! Title normal! A #<esc>yyppv$r#kkv$r#

The :Title command will only work for python scripts. It can be abbreviated to :Ti. It could be defined for different file types as well. A version that uses the entire line selection V, could also be defined as such: A #<esc>yyppVr#kkVr#, but it would then not work well on indented text, as explained in a comment, it would "replace[...] the indentation with more octothorpes".

:help normal

If the [!] is given, mappings will not be used. Without it, when this command is called from a non-remappable mapping (:noremap), the argument can be mapped anyway.

  • 1
    You might want to use normal!. I think instead of v$r# you can use Vr#.
    – D. Ben Knoble
    Commented Feb 28, 2023 at 18:00
  • 1
    Yes, there is page: :h :normal
    – Friedrich
    Commented Feb 28, 2023 at 18:42
  • 2
    @D.BenKnoble I find v$r# superior to Vr# since it works as intended if there's indentation before the comment, it will produce a box including the same indentation, instead of replacing the indentation with more octothorpes
    – filbranden
    Commented Feb 28, 2023 at 19:27
  • 3
    @PaulRougieux I suggest you only leave one command suggestion in your answer, not the original one then the updated one
    – filbranden
    Commented Feb 28, 2023 at 19:28
  • 1
    This feels like a game of vimgolf: normal! A #<esc>yyPv$r#yyjp
    – Phil R
    Commented Mar 3, 2023 at 22:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.