Live data from Hacker News

How to boost your Vim productivity

sheerun.net

11–20 of 130 posts

Re: How to boost your Vim productivity

#11
Why don't people seem to use the F keys? I have mappings for very frequent functions on F1-F5, and it really helps:

    map   :w|      "Fast save
    imap  :w|      "Fast save
    map   :make|    "Fast compile
    map   :bn
    map   :bp
    " Same mapping for gnome-terminal ( see     http://stackoverflow.com/q/12813126/164171 )
    map ^[O1;2R :bp

    map   :bd|          "Close buffer

    map   :cnext
    map   :cprev
    imap   :cnext
    imap   :cprev

Re: How to boost your Vim productivity

#12
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.

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

Re: How to boost your Vim productivity

#13
I'm all for bending your text editor to your will, but a few of these tips gloss over the core of vim: the shortcut language it offers and the composability of its parts of speech [1].

For example, remapping the paste key to paste + move to end is cool, but `ppppp` to paste 5 times circumvents vim's killer repetition. Want to paste 100 copies? (hopefully you do this rarely). Just type `100p` and you're done. I won't bother typing out the obvious sequence of repeated `p` presses, but you get the drift.

Also, remapping v to progressively select larger surrounding text objects will keep you from learning that you can perform any action on the 3rd parent curly braces by following the action with the text object `3a{` or `3a}`. Select it: `v3a{`, delete it: `d3a{`, comment it out `gc3a{` ([2]), whatever. If there's an action in vim you can perform it on a text object which can usually start with some count.

I really do mean language, too. When I'm using vim, I feel a lot more like I'm communicating with my computer than trying to figure out how to do what I want to do.

The actions are there: delete, change, select. The direct objects are there: braces, this line, line number N, end of this word, the next character C, this paragraph. The prepositions are there: inside, outside, up to (inclusive, exclusive).

Heck, the sensible defaults feel a whole lot like the "understood" words that you can leave out of sentences. Instead of "navigate to line 30", you can just use the shortcut for "line 30" (30G) and be done.

One of my favorite things about vim is the number of times I've literally been surprised that vim didn't read my mind and go where I was looking. Once you're familiar with the different pieces, it's so good that you might expect it to read your mind.

[1] http://ferd.ca/vim-and-composability.html

[2] https://github.com/tpope/vim-commentary

Re: How to boost your Vim productivity

#14

Why don't people seem to use the F keys? I have mappings for very frequent functions on F1-F5, and it really helps: map :w | "Fast save imap :w | "Fast save map :make | "Fast compile map :bn map :bp " Same mapping for gnome-terminal ( see http://stackoverflow.com/q/12813126/164171 ) map ^[O1;2R :bp map :bd | "Close buffer map :cnext map :cprev imap :cnext imap :cprev

One of my favorite bits for saving my own butt: commit more.

    "Function to commit.
    function! GitCommit ()
      execute 'write'
      let message = input('Enter commit message: ')
      execute '!git add ' . bufname('%') . ' && git commit -m ''' . message . ''';'
    endfunction
    
    imap  :call GitCommit()
    map  :call GitCommit()

Re: How to boost your Vim productivity

#15
post #10

Earlier quoted context omitted.

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…

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.

Re: How to boost your Vim productivity

#16

Why don't people seem to use the F keys? I have mappings for very frequent functions on F1-F5, and it really helps: map :w | "Fast save imap :w | "Fast save map :make | "Fast compile map :bn map :bp " Same mapping for gnome-terminal ( see http://stackoverflow.com/q/12813126/164171 ) map ^[O1;2R :bp map :bd | "Close buffer map :cnext map :cprev imap :cnext imap :cprev

I map a few of them:

    " Filesystem browser
    map  :Explore

    " Show whitespace
    set listchars=eol:↵,tab:>·,trail:~,extends:>,precedes: :set list!

Re: How to boost your Vim productivity

#17

Why don't people seem to use the F keys? I have mappings for very frequent functions on F1-F5, and it really helps: map :w | "Fast save imap :w | "Fast save map :make | "Fast compile map :bn map :bp " Same mapping for gnome-terminal ( see http://stackoverflow.com/q/12813126/164171 ) map ^[O1;2R :bp map :bd | "Close buffer map :cnext map :cprev imap :cnext imap :cprev

One of my favorite bits for saving my own butt: commit more. "Function to commit. function! GitCommit () execute 'write' let message = input('Enter commit message: ') execute '!git add ' . bufname('%') . ' && git commit -m ''' . message . ''';' endfunction imap :call GitCommit() map :call GitCommit()

OMG, I need to add this to my .vimrc. Thanks :D

Re: How to boost your Vim productivity

#18

Why don't people seem to use the F keys? I have mappings for very frequent functions on F1-F5, and it really helps: map :w | "Fast save imap :w | "Fast save map :make | "Fast compile map :bn map :bp " Same mapping for gnome-terminal ( see http://stackoverflow.com/q/12813126/164171 ) map ^[O1;2R :bp map :bd | "Close buffer map :cnext map :cprev imap :cnext imap :cprev

I'd guess, for the same reason I use Ctrl-[ instead of the Esc key. F keys put you out of the home row (at least in full size keyboards).

Re: How to boost your Vim productivity

#19
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…

...% to navigate quickly within a line.

"%" works fine for moving around, but when I use it to edit it always surprises me, which direction it goes. I almost think vim is psychic, because "%" goes the wrong direction for me at least 80% of the time. There must be a deterministic rule governing this, but can anyone explain to me what it is?

Post reply on HN