5

Background

I have a code file and I like to place comments inside my code. Sometimes, these comments are boxed:

defmodule BananasModule do

    ##############
    # Public API #
    ##############

    def function_blabla do .....
end

Objective

My objective is to have a key sequence (a macro, for example) that surrounds the line I am at with a boxed comment. For example, given the following file:

defmodule BananasModule do


    <cursor> Public API

    def function_blabla do .....
end

After using some command, I would ideally get:

defmodule BananasModule do

    ##############
    # Public API #
    ##############

end

Do notice how the comment box is indented. This is very important.

What I have tried

I have tried out the NERDCommenter plugin, but unfortunately it doesn't have this feature.

I have also tried to create a macro following this discussion:

:let @a = "I## \<Esc>A ##\<Esc>yyPVr#jpVr#"

But the commend box I get after executing @a is not indented:

defmodule BananasModule do

####################
    ## Public API ##
####################

end

Problem

Although I feel like I came close, I don't know enough about macros and configurations to make my comment boxes indented.

5

Well, "put text into a box" is a well-known Vim excercise. I'm not sure where I saw it for the first time, but the most elegant solution, IMO, is to use :h v_r

So it becomes:

nnoremap <leader># I#<Space><Esc>A<Space>#<Esc>yy2P<C-V>$r#2j.

I#<Space><Esc> --- add "# " at the start of line

A<Space>#<Esc> --- add " #" at the end of line

yy2P --- make 2 copies of the current line (so we have three identical # text # lines)

<C-V>$r# --- change the top line into #####

2j. -- and the same for bottom line

| improve this answer | |
  • This does not help with the indentation problem I currently have. – Flame_Phoenix Jan 10 at 10:11
  • @Flame_Phoenix "The box" has the same indent as "Public API" had before – Matt Jan 10 at 10:15
  • You are correct! – Flame_Phoenix Jan 10 at 10:25
  • 1
    Very nice answer! The Vim golfer in me wants to do Y2Pvg_r#2j. for the last part. Using P makes the k unnecessary. Applying the r# to the last character (rather than end of line) makes it repeatable with .. nitpick: You're missing a # in explanation for the A command. – filbranden Jan 10 at 10:39
  • 1
    @filbranden Thank you, I improve my answer. – Matt Jan 10 at 12:00
0

This can be best accomplished with Ultisnips and vim-snippet.

vim-snippets provides a snippet box which can create a bounded box. It is just what you want. I have tested on a Python source file and it works great. Give it a try.

| improve this answer | |
  • I could not find any snippet box in ultisnips repo linked above. – Dilawar May 5 at 19:49
  • @Dilawar The box snippet is in all.snippets file here. – jdhao May 6 at 2:10
  • Thanks, would be very helpful. – Dilawar May 6 at 13:45

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.