Live data from Hacker News

How to boost your Vim productivity

sheerun.net

1–10 of 130 posts

Re: How to boost your Vim productivity

#3
In my 6 years of coding and writing LaTeX in Vim, I have found that the greatest boost to my Vim productivity is learning all about the vi and ex foundation of Vim. My Vim knowledge applies 100% to any default Vim installation on a modern distro, and my vimrc contains mostly trivial tweaks.

This means using :normal and macros instead of :s for most of my search/replace actions; using H, M, L, {, } to navigate quickly in the lines visible on the screen, and using f/F/t/T/;/,/% to navigate quickly within a line.

I would say that 98% of my autocomplete needs are fulfilled by token completion (:help i_CTRL-N) and line completion (:help i_CTRL-X_CTRL-L).

I frequently use the command-line window (:help c_CTRL-F) and listing matching names (:help c_CTRL-D).

I specifically don't have mappings that involve the leader key, and I don't use the Ctrl-P plugin or a package manager or anything like that -- I honestly don't think that mapping w to :w will make me any more productive in Vim.

Re: How to boost your Vim productivity

#4
post #3

In my 6 years of coding and writing LaTeX in Vim, I have found that the greatest boost to my Vim productivity is learning all about the vi and ex foundation of Vim. My Vim knowledge applies 100% to any default Vim installation on a modern distro, and my vimrc contains mostly trivial tweaks. This means using :normal and macros instead of :s for most of my search/replace actions; using H, M, L, {, } to navigate quickly…

> This means using :normal and macros instead of :s for most of my search/replace actions

Interesting. Can you please elaborate more on this?

Re: How to boost your Vim productivity

#5
post #3

In my 6 years of coding and writing LaTeX in Vim, I have found that the greatest boost to my Vim productivity is learning all about the vi and ex foundation of Vim. My Vim knowledge applies 100% to any default Vim installation on a modern distro, and my vimrc contains mostly trivial tweaks. This means using :normal and macros instead of :s for most of my search/replace actions; using H, M, L, {, } to navigate quickly…

> This means using :normal and macros instead of :s for most of my search/replace actions Interesting. Can you please elaborate more on this?

Suppose a range of lines contains function calls in some programming language:

    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.

Re: How to boost your Vim productivity

#6
post #3

In my 6 years of coding and writing LaTeX in Vim, I have found that the greatest boost to my Vim productivity is learning all about the vi and ex foundation of Vim. My Vim knowledge applies 100% to any default Vim installation on a modern distro, and my vimrc contains mostly trivial tweaks. This means using :normal and macros instead of :s for most of my search/replace actions; using H, M, L, {, } to navigate quickly…

> This means using :normal and macros instead of :s for most of my search/replace actions Interesting. Can you please elaborate more on this?

Another little "trick" for replacing:

  " search. cw (or cs, c whatever) to replace/fix. esc. n.n.n.n.
  vnoremap  s //e=&selection=='exclusive'?'+1':''
      \:call histdel('search',-1)let @/=histget('search',-1)gv
  omap s :normal vs
A really simple, dumb example: http://showterm.io/040c49ac158cdda15fbca

Re: How to boost your Vim productivity

#7
post #5

Earlier quoted context omitted.

> This means using :normal and macros instead of :s for most of my search/replace actions Interesting. Can you please elaborate more on this?

Suppose a range of lines contains function calls in some programming language: 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…

Isn't

:%s/.)/hello)/

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

Re: How to boost your Vim productivity

#8
post #6

Earlier quoted context omitted.

> This means using :normal and macros instead of :s for most of my search/replace actions Interesting. Can you please elaborate more on this?

Another little "trick" for replacing: " search. cw (or cs, c whatever) to replace/fix. esc. n.n.n.n. vnoremap s //e =&selection=='exclusive'?'+1':'' \: call histdel('search',-1) let @/=histget('search',-1) gv omap s :normal vs A really simple, dumb example: http://showterm.io/040c49ac158cdda15fbca

Can you explain what the mappings do?

I 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.

Re: How to boost your Vim productivity

#9
post #5

Earlier quoted context omitted.

> This means using :normal and macros instead of :s for most of my search/replace actions Interesting. Can you please elaborate more on this?

Suppose a range of lines contains function calls in some programming language: 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…

Lord, that's so useful.

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!

Re: How to boost your Vim productivity

#10
post #5

Earlier quoted context omitted.

Suppose a range of lines contains function calls in some programming language: 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…

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.

Post reply on HN