30
\$\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\$
11
  • 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
    Commented Feb 23, 2015 at 19:37
  • 5
    \$\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
    Commented Feb 24, 2015 at 10:16
  • 2
    \$\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
    Commented Feb 24, 2015 at 10:16
  • 1
    \$\begingroup\$ @Optimizer So there is a www.IntelliJgolf.com as well? :P \$\endgroup\$
    – Hjulle
    Commented Feb 24, 2015 at 10:22
  • 3
    \$\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
    Commented Feb 24, 2015 at 10:28

2 Answers 2

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\$
2
  • 3
    \$\begingroup\$ Replace yy with Y to save a character. \$\endgroup\$
    – Doorknob
    Commented Feb 23, 2015 at 12:50
  • 2
    \$\begingroup\$ Wow, that r command is powerful \$\endgroup\$
    – justhalf
    Commented Apr 27, 2016 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\$
3
  • \$\begingroup\$ Same comment as on @rcrmn's answer: Replace yy with Y to save a character. \$\endgroup\$
    – Doorknob
    Commented Feb 23, 2015 at 12:51
  • \$\begingroup\$ I believe it's actually 14 keystrokes, as you have to hit Intro to execute the replace command \$\endgroup\$
    – ror3d
    Commented Feb 23, 2015 at 12:54
  • \$\begingroup\$ @rcrmn Oh, good catch. \$\endgroup\$ Commented Feb 23, 2015 at 14:36

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.