# Vim - surround comment line with comment character

Today I realised that I often like to define sections in my code like so:

####################
# Helper Functions #
####################


But that it's tedious to do. Assuming I have a line such as this:

# Helper Functions #


What is the shortest set of vim keystrokes to wrap it in a #? Shift does not count as a keystroke in this challenge.

Test cases:

Input: "#test test test#"
Output:
################
#test test test#
################

Input: "#this is a nice block comment#"
Output:
##############################
#this is a nice block comment#
##############################

Input: "# s p a c e s must be supported a l s o#"
Output:
########################################
# s p a c e s must be supported a l s o#
########################################

• I'm voting to close this question as off-topic because "vim keystrokes" basically means running commands of an IDE, like Ctrl - C, or Ctrl - R in ST2. This is not coding in any form. – Optimizer Feb 23 '15 at 19:37
• @Optimizer Vim is a turing complete language (e.g. by using macros) with several different constructs coding and the primary method for editing is based on verbs (actions) an objects (movements). There is already a popular web page for vim golfing: www.vimgolf.com. (But I agree that this particular question is pretty boring) – Hjulle Feb 24 '15 at 10:16
• @Optimizer: is there a more suitable S.E. site the question could be moved to? Given vim's extensive scripting/macro language, some people might consider it a language in itself. – shearn89 Feb 24 '15 at 10:16
• @shearn89 Hostile ? What are you talking about ? PPCG is this site. – Optimizer Feb 24 '15 at 10:23
• Why wouldn't other IDE's be allowed as well if you can write programs in their macro system? What is it that makes it "not programming"? – Hjulle Feb 24 '15 at 10:28

## 118 7 keystrokes

YpVkr#p

Yp - duplicate current line, leaving the cursor at the lower of the two
V - enter visual line mode
k - go up and select both lines
r# - replace every selected character with #. Leaves visual mode and leaves cursor at the upper line.
p - put the yanked line (the original) on the next line.


(thanks to doorknob for reminding Y=yy)

• Replace yy with Y to save a character. – Doorknob Feb 23 '15 at 12:50
• Wow, that r command is powerful – justhalf Apr 27 '16 at 1:39

# 1615 14 keystrokes

Yp
:s/./#/g
<cr>
YkP


The straight-forward approach: duplicate the line, replace all characters with #, copy the the result and paste it above.

I'm counting P and : as one keystroke each (instead of two for Shift+p or Shift+;). That being said, the question specifies to count "commands", where I'm not sure how to count the substitution.

• Same comment as on @rcrmn's answer: Replace yy with Y to save a character. – Doorknob Feb 23 '15 at 12:51
• I believe it's actually 14 keystrokes, as you have to hit Intro to execute the replace command – rorlork Feb 23 '15 at 12:54
• @rcrmn Oh, good catch. – Martin Ender Feb 23 '15 at 14:36