Interesting. Can you please elaborate more on this?
Interesting. Can you please elaborate more on this?
i = foo(a, 42, 1)
j = bar(b, c, 100, 2) # this is a comment (well, duh)
bazbar(etc, 3)
You want to edit the last parameter in each function call -- change it to 'hello'. I have two ways of approaching this: macros (interactive) or :norm (less interactive).1. Put your cursor on the first byte in the first line, type qq to record a macro of your change, q to finish. Select the rest of the lines in visual line mode and type :'<,'>norm @q to run the macro on each of the remaining lines.
2. Select the lines in visual line mode and type, for instance, :'<,'>norm %F,ws'hello' This is the "non-interactive" version of the above since the macro keystrokes are given directly to :normal. Pro: ability to undo keystrokes unlike when recording a macro. Con: cannot actually see the effect of the keystrokes (but with practice, this is not a problem).
(Note that colon in visual mode automatically inserts the :'<,'> so you only have to type :norm ...)
I find the declarative nature of regexes/:s to be too restrictive -- I much prefer the operational nature of macros/:normal, since that lets me make the change I want directly, without me having to phrase the change as a regular expression.
I could also use CTRL-A in normal mode to increment the last argument in each line: :'<,'>norm %b^A (where ^A is the literal, typed CTRL-V CTRL-A)
I feel like a wizard whenever I use :normal in a non-trivial manner.
:%s/.)/hello)/
simpler? Maybe that's because I know nothing about macros. Thanks for the explanation.
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."As a side note, I love how Vim is basically a functional programming language for text editing.
{ 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!)
I normally include a move to next line part in my macro and then guess at how many times to run it (20@q, repeat until I get it right). It's actually the main reason I often don't bother using macros.
This is such a great tip. Thanks!
:'<,'>g/foobar/norm @q
will run the macro only on the subset of selected lines that match the regex "foobar". :g/.../ will match the regex against all lines in the file.Another tip: when you forget to add the move to next line to your macro, you don't have to start over. You can append to a macro, just use the upper-case register name instead. So to append a down and move to beginning of line to macro 'a', you'd type qAj0
Basically, I achieved my wizard-level C++, Vim and Git knowledge during my three years as a part-time student programmer in the basic research center MADALGO. I took the time to study the documentation (respectively the C++11 draft, :help and the git man pages) whenever I was curious. For Vim in particular, I made sure to eliminate all repeated keysmashing in my daily workflow, which "unfortunately" required me to learn about macro wizardry.
My only advice is to keep practicing, to keep trying to spot inefficiencies in your own workflows, and to actively eliminate these inefficiencies by studying the Vim help pages.
" search. cw (or cs, c whatever) to replace/fix. esc. n.n.n.n.
vnoremap <silent> s //e<C-r>=&selection=='exclusive'?'+1':''<CR><CR>
\:<C-u>call histdel('search',-1)<Bar>let @/=histget('search',-1)<CR>gv
omap s :normal vs<CR>
A really simple, dumb example: http://showterm.io/040c49ac158cdda15fbcaI usually use the asterisk to search for the token under the cursor, edit the sought token using cw and then n.n.n.n. to replace later occurrences. I don't see how the mapping makes this more efficient.