Share a VIM tip weekly!

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.

gg=G
4 Likes

Awesome thread :+1:

My tip, to change text from the current position to the end of the line use:

c$

2 Likes

Nice, some related useful shortcuts:

  • ct<character> will change up to (but excluding) the typed character
  • cf<character> will change up to (and including) the typed character
  • ci( changes everything inside parenthesis (can substitute other characters as well like ‘{’)
  • ca( changes everything inside plus the parenthesis
2 Likes

I’ll just add

  • dd will cut the current line
  • 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 :wink:

2 Likes

gq{motion} will format text (fix line wraps, etc.)

Not great for code, but I use it in Markdown or to format comments.

I like that ={motion} can indent smaller sections of code, too. =} will indent just the next paragraph (block) of code.

3 Likes

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

1 Like

c$

Or just C. :wink:

2 Likes

Even better :+1:

And:

D

to delete to end of line :slight_smile:

1 Like

Today I just learned about joining lines - so with a list like this:

* item1
* item2
* item3
* item4
* item5

You type:

5gJ

to get this output:

* item1* item2* item3* item4* item5
3 Likes

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.

3 Likes
set relativenumber
set number

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.

3 Likes

My vim tip is to add comments in Javascript on multiple lines without using visual selection:

:10,17s/^////g this will comment out line 10-17

2 Likes

In normal mode you can jump forward and backward between words that match the current word the cursor is on.

To move forward you have:    *   (shift 8)
And to move back you have:   #   (shift 3)

Jump to line by colon line number, e.g.

:154

Will jump to the start of line 154.

1 Like

To enter text multiple lines at a time.

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.

1 Like

In normal mode undo is simply:

u

Redo what was undid with:

CTRL-r

There… now I’m caught up on sharing weekly tips :wink:

1 Like

I always wondered how to undo an undo! (but was too lazy too Google lol)
Thanks :023:

1 Like

You can execute an external command in normal mode with the bang operator.

:!date

You can have the output entered directly into your current buffer with read.

:read !date
3 Likes

Next tip. You can replace text with regex substitution:

:%s/original_string/new_one/g

You can leave the first entry blank if what you want to replace is the last search pattern. To replace the search entries do:

:%s//new_one/g

Remember you can use regex in the substitution command.

1 Like