6

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.

0

3 Answers 3

7

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

6
  • This does not help with the indentation problem I currently have. Commented Jan 10, 2020 at 10:11
  • @Flame_Phoenix "The box" has the same indent as "Public API" had before
    – Matt
    Commented Jan 10, 2020 at 10:15
  • You are correct! Commented Jan 10, 2020 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
    Commented Jan 10, 2020 at 10:39
  • 1
    @filbranden Thank you, I improve my answer.
    – Matt
    Commented Jan 10, 2020 at 12:00
1

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.

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

The LudoPinelli/comment-box.nvim neovim plugin seems to address this use-case.

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.