Vim - surround comment line with comment character

27

2

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#
########################################

shearn89

Posted 2015-02-23T12:00:00.077

Reputation: 381

3I'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 – 2015-02-23T19:37:59.123

3@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 – 2015-02-24T10:16:14.920

1@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 – 2015-02-24T10:16:31.883

Supposedly most other PPCG big shots also think the same as you do. But both the above comments/arguments hold true for any good IDE, like ST2 or IntelliJ or Atom – Optimizer – 2015-02-24T10:18:16.833

@Optimizer I have no idea what PPCG means, but your response sounds surprisingly hostile for what I thought was a reasonable reply to your comment. – shearn89 – 2015-02-24T10:21:49.697

@Optimizer So there is a www.IntelliJgolf.com as well? :P – Hjulle – 2015-02-24T10:22:27.930

1@shearn89 Hostile ? What are you talking about ? PPCG is this site. – Optimizer – 2015-02-24T10:23:10.200

@Hjulle so treat a random webpage as a proof :D – Optimizer – 2015-02-24T10:23:59.333

@Optimizer - ahhh that makes more sense! I interpreted it as you calling myself a "PPCG bigshot", and assumed the PPCG was derogatory. My bad! – shearn89 – 2015-02-24T10:24:21.873

2Why 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 – 2015-02-24T10:28:32.967

I like this question, because I also do this =D – justhalf – 2016-04-27T01:39:58.440

Answers

40

11 8 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)

rorlork

Posted 2015-02-23T12:00:00.077

Reputation: 1 421

3Replace yy with Y to save a character. – Doorknob – 2015-02-23T12:50:31.740

2Wow, that r command is powerful – justhalf – 2016-04-27T01:39:41.723

10

16 15 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.

Martin Ender

Posted 2015-02-23T12:00:00.077

Reputation: 184 808

Same comment as on @rcrmn's answer: Replace yy with Y to save a character. – Doorknob – 2015-02-23T12:51:00.200

I believe it's actually 14 keystrokes, as you have to hit Intro to execute the replace command – rorlork – 2015-02-23T12:54:59.957

@rcrmn Oh, good catch. – Martin Ender – 2015-02-23T14:36:53.357