27
\$\begingroup\$

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#
########################################
\$\endgroup\$
  • 3
    \$\begingroup\$ 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. \$\endgroup\$ – Optimizer Feb 23 '15 at 19:37
  • 3
    \$\begingroup\$ @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) \$\endgroup\$ – Hjulle Feb 24 '15 at 10:16
  • 1
    \$\begingroup\$ @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. \$\endgroup\$ – shearn89 Feb 24 '15 at 10:16
  • 1
    \$\begingroup\$ @shearn89 Hostile ? What are you talking about ? PPCG is this site. \$\endgroup\$ – Optimizer Feb 24 '15 at 10:23
  • 2
    \$\begingroup\$ 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"? \$\endgroup\$ – Hjulle Feb 24 '15 at 10:28
40
\$\begingroup\$

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)

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

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.

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

protected by Community Jul 8 '15 at 1:04

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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