Hacker News new | past | comments | ask | show | jobs | submit login

Isn't

:%s/.)/hello)/

simpler? Maybe that's because I know nothing about macros. Thanks for the explanation.




Pardon, it might have been a trivial example. I managed to find a real-world example I encountered in February this year.

The PAPI library has a utility program papi_avail which prints a table of supported performance counters. The lines may look like this:

    PAPI_VEC_SP  0x80000069  Yes  Single precision vector/SIMD instructions
    PAPI_VEC_DP  0x8000006a  Yes  Double precision vector/SIMD instructions
    PAPI_REF_CYC 0x8000006b  No   Reference clock cycles
For a course project, I wanted to turn this into a C-array like this:

    {PAPI_VEC_SP, "Single precision vector/SIMD instructions"},
    {PAPI_VEC_DP, "Double precision vector/SIMD instructions"},
    {PAPI_REF_CYC, "Reference clock cycles"},
Using :norm, this transformation can be achieved in the following way:

    :'<,'>norm I{^[eldedecw, "^[A"},
(where ^[ is a literal escape, typed CTRL-V ESC.)

Anecdotally, it is faster for me to type up such a line than an equivalent regular expression, since I use vi normal mode commands much more than regular expressions.


  /^\(\S*\)\s*\S*\s*\S*\s*\(\p*\)
  :%s//{\1, "\2"},
"Now they have two problems."


I would have piped this through awk, but this is probably better done with macros. Thanks for taking the time to elaborate. I will definitely give macros a try.


I just spent minutes to parse that instruction. Mind blown. Thank you for this enlightenment.


You need to know how dw, de, cw, ce relate to each other, when the cursor is on a token character vs. a whitespace character, and so on, in order for this to be truly useful -- but luckily, ordinary practice with those commands in Vim can lead to an efficient internal idea of how it works. "You get used to it, though. Your brain does the translating. I don't even see the code. All I see is blonde, brunette, redhead."


this is awesome, can you please suggest some resources for me, a long-time basic user of vim, ( know a lot of commands for actions, but not the underlying relationships?


Something i've been using more lately is Ctrl-R + Register. Great for inserting text in command mode. For instance, you can do this to insert your recorded macro (if it's in register q) as a norm command: :norm Ctrl-R q


I would have almost simply started recording a macro, convert the first one using as many idempotent normal mode commands in place of insert mode commands as possible, and simply replayed it. Thanks for the idea.

As a side note, I love how Vim is basically a functional programming language for text editing.


You can do this with a few shortcuts in sublime. It requires about the same number of keystrokes, but far less cognitive overhead.

    { down { down {
to insert all three leftmost brackets.

    up up up
to go to first bracket.

    shift+left
to select first bracket.

    cmd+d cmd+d cmd+d
to select all three next brackets including the first.

    ctrl+shift right
to move all three cursors to the end of word.

    comma space "
to type your characters with all three cursors.

    cmd+right
to go to end of line with all three cursors.

    " } ,
to type your characters with all three cursors.

21 presses (29 keys), compared to your 32. And all I had to think about was where the cursor was.

(Not to be pedantic, of course!)


Well you're leaving the middle columns there, then on your first keystrokes it would be acutally {+down+left as Sublime does not put cursor back at beginning of line when going down. In all I used 26 keystrokes in Sublime, and 27 in Vim. Also, if you understand Vim's vocabulary, the "cognitive overhead" is, as you put it "far less" in Vim's favor. IMO.


I guess that would change the comment : "duh)" would become "hello)", whereas the macro does not.


No, not without a 'g' at the end.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: