1

I have a list of items that I want to add a method call to. An example is easiest. Here's what I have now:

 assists: 12,
 level: 14,
 deaths: 5,
 ...

I want to change that list to look like this:

 assists: build_average(:assists),
 level: build_average(:level),
 deaths: build_average(:deaths),
 ...

Is it possible to add that method call to the end of every line with the name of the key as the argument with a neat Vim expression?

6
  • 1
    It is possible - record a macro for the first line, and then apply it to the rest of the lines. Commented Sep 17, 2015 at 12:39
  • Of course it is. What did you try? Commented Sep 17, 2015 at 12:46
  • i'm working on the macro solution that @Kenney suggested now :) I am a novice in vim, I admit, so I'm afraid I didn't know where to start Commented Sep 17, 2015 at 12:47
  • 1
    Regex would be best but vim regexes are often not standard.. My vim keystrokes are (starting at the first line): qq0yypk/:ENTERd$abuild_average(ESCJ/ENTERld$a),ESCj2q Commented Sep 17, 2015 at 12:53
  • 1
    @Kenney: You should put that into an answer. It is also a valid (and more intuitive, in my opinion) solution. Commented Sep 17, 2015 at 12:59

3 Answers 3

5

More of a regular expression:

:%s/\(\w\+\):.\+/\1: build_average(:\1),/

Note that this applies to all lines in your file. To only replace in a region, select the region (using V) and then use :s (which results in :<,>s/...).

Using more complex regular expressions in VIM can be tricky, because metacharacters are different from "normal" regular expression syntax (you need to write \+ instead of +, but can use . without escaping it, for example). I found this guide very handy to refer to the special VIM-syntax of regular expressions: http://vimregex.com/#pattern

Sign up to request clarification or add additional context in comments.

3 Comments

I get a E486: pattern not found: \(\w+\):.+
Sorry, always getting the escaping wrong. I fixed it in the answer. Thanks for pointing out!
hi Sven, that's working for me as well as Kenney's macro, thanks for the help :)
4

Alternatively you can record a macro:

q                   // record macro
q                   // assign it to letter 'q'
0                   // go to start of line
/:<ENTER>           // search for ':'
l                   // move cursor 1 position to the right
d$                  // delete to end of line (line is now 'assists:')
yyp                 // duplicate current line (cursor moves 1 line down)
k                   // move cursor up
A build_average(    // append " build_average("
<ESC>               // exit edit mode
J                   // join next line
A ),                // append " ),"
<ESC>               // exit edit mode
j                   // move 1 line down
q                   // stop recording macro
2@q                 // execute macro 'q' 2 times

6 Comments

kenney, you responded first and this is a really nice answer, so i'll give the check to you - Sven your answer is also very helpful, so thanks to you both
Here is a shorter one: 0wyw/:ENTERllvt,pa)ESCbibuild_average(:ESCj ;)
:) Or this: 0vw"aywwcwbuild_average()<ESC>h"apj
I'd use f: instead of /:<Enter> myself :-)
I had hoped that my comment would provoke this kind of responses. :)
|
2

More regexp gymnastics:

:%s/\v(\w+):\s+\zs.*\ze,/build_average(:\1)/

Decrypting it:

  • :help \v
  • :help \w
  • :help \s
  • :help \zs
  • :help \ze
  • :help \1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.