Vim commands for improved productivity

Introduction

I would like to de­scribe my favourite Vim com­mands that I use on a dai­ly ba­sis, in or­der to share some tips that could help you if you are new in this ed­i­tor, or to im­prove your ex­pe­ri­ence even if you use it.

  • J : Use­ful when or­ga­niz­ing code, this will join the line be­low to the cur­rent one.

  • ci) (“change in­side ‘)’): Ac­tu­al­ly, the clos­ing brack­et could be changed by any oth­er thing (like ‘]’, ‘}’, etc.). This will erase ev­ery­thing with­in the brack­ets and set you in in­sert mode (the c could al­so be changed for d for ex­am­ple if you just want to delete). Again, this is very use­ful when refac­tor­ing code, if you want to change the pa­ram­e­ters of a func­tion def­i­ni­tion, or what­ev­er is in a block, etc.

  • (s­e­lect some code with vis­ual mode and then) zf : will fold the se­lect­ed code. zd for un­fold­ing.

  • % : alone or along with some oth­er op­er­a­tor, is use­ful for op­er­at­ing with match­ing brack­ets in the code. It will match the clos­ing brack­et of the one you have the cur­sor in.

  • C or D : if you want to change or delete from the cur­rent po­si­tion up to the end of the line, re­spec­tive­ly.

  • t, (or any oth­er char­ac­ter in­stead of com­ma) will point you until that char­ac­ter. The good about this, is that is pos­si­ble to chain it with oth­er com­mand­s, for ex­am­ple: ”ct,” will change all the con­tent un­til the next com­ma.

  • < or > will in­dent the code fol­low­ing the “ar­row” di­rec­tion (ac­cord­ing to what is set in shiftwidth).

  • = Au­to­mat­i­cal­ly in­dents code (use­ful when high­light­ing code in vis­ual mod­e).

  • w, e or b will point you to the next wor­d, to the end of the word, or back to the pre­vi­ous word, re­spec­tive­ly. The nice thing about these op­er­a­tors is when they work com­bined with oth­er­s, for ex­am­ple:

    • cw will change the next word.

    • db will delete the pre­vi­ous word.

  • { or } for mov­ing up or down through para­graph­s, re­spec­tive­ly.

In ad­di­tion, note that you do not need to know all pos­si­ble com­mand­s, just those that will help you with your nor­mal ac­tiv­i­ties. This means that is could be enough with a small sub­set of all the fea­tures (the list I wrote is very short in­deed). And this is pre­cise­ly the idea be­hind this post: to show how some few com­mands ap­plied in the right con­tex­t, might make you ed­it faster.

All in al­l, Vim is a great ed­i­tor, full of amaz­ing fea­tures. Learn­ing it is ac­tu­al­ly worth it, in the sense that you will get an amaz­ing pro­duc­tiv­i­ty in re­turn (y­ou’ll type and ed­it code faster).