Each week share a cool VIM editor tip here. Keep it short and simple and we can all be better developers as we improve our in-editor skills. I suggest everyone who uses VIM to participate to keep us all continually in the process of self improvement.
My first tip. In normal mode (hit ESC to get into it) enter the following to auto-indent your current buffers code.
p will insert the line currently cut below the current line
cw will change the current word
I use this every once in a while when doing fix-up git commits. You do
git commit -am "fix up"
git rebase -i master
it gives you a list of all the commits, with your new fix-up commit at the bottom. Then you navigate to that line, do dd to cut the line. Navigate to the commit you want to fix-up. Press p to insert your fix-up commit again. Do a cw to change pick to fixup (or just f) and then of course :wq to save
Everyone be sure to write any of these tips you don’t already know and use onto a card and keep it in front of you at your computer to practice. You learn and master by doing.
So as not to disappoint you with this update notification I’m going to sneak another tip in here.
In normal mode you can indent the current line with:
>>
To indent multiple lines below, including the current line, put a number before it
numberoflines>>
To unindent use the opposite
<<
numberoflines<<
Or simply select multiple lines in visual mode before using either << or >>. Use Ctrl-V (in normal mode) plus directional keys to (block) select lines (remember to use h,j,k, and l for navigation: keeping you hands centered).
This week I’d like to share the Emmet plugin as my VIM tip. It is a HTML and CSS super completer of sorts. It’s officially available in the RubyMine editor but also works well in VIM. The time this saves is very much worth while.
By setting these vim will show you the actual line number the cursor is on and then all other line numbers relative to the current line. Makes jumping around or manipulating multiple lines of text very easy.
In normal mode select a visual block by pressing CTRL-v . Then press up or down however many lines you would like to enter the text on. Once you’ve selected a column of where you’d like to enter text press SHIFT-i . You are now in insert mode at the first point your cursor started the visual block. Type what you want to go in. Then press ESC and all of the other lines will fill in exactly what you just entered.