# Share a VIM tip weekly!

**URL:** <https://metaruby.com/t/share-a-vim-tip-weekly/254>\
**Category:** General Programming\
**Tags:** vim\
**Created:** [July 15, 2015, 9:10pm UTC](https://metaruby.com/t/share-a-vim-tip-weekly/254 "2015-07-15T21:10:44Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![danielpclark](https://metaruby.com/user_avatar/metaruby.com/danielpclark/32/823_2.png) [@danielpclark](https://metaruby.com/u/danielpclark)\
**Post date:** [July 15, 2015, 9:10pm UTC](https://metaruby.com/t/share-a-vim-tip-weekly/254/1 "2015-07-15T21:10:45Z")

</div>

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
```

---

<div class="post-metadata">

**Author:** ![AstonJ](https://metaruby.com/user_avatar/metaruby.com/astonj/32/8_2.png) [@AstonJ](https://metaruby.com/u/AstonJ)\
**Post date:** [July 15, 2015, 9:52pm UTC](https://metaruby.com/t/share-a-vim-tip-weekly/254/2 "2015-07-15T21:52:45Z")

</div>

Awesome thread 👍

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

`c$`

---

<div class="post-metadata">

**Author:** ![neilberget](https://metaruby.com/user_avatar/metaruby.com/neilberget/32/896_2.png) [@neilberget](https://metaruby.com/u/neilberget)\
**Post date:** [July 16, 2015, 12:29am UTC](https://metaruby.com/t/share-a-vim-tip-weekly/254/3 "2015-07-16T00:29:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Ohm](https://metaruby.com/user_avatar/metaruby.com/ohm/32/828_2.png) [@Ohm](https://metaruby.com/u/Ohm)\
**Post date:** [July 16, 2015, 5:43am UTC](https://metaruby.com/t/share-a-vim-tip-weekly/254/4 "2015-07-16T05:43:56Z")

</div>

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

```auto
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 😉

---

<div class="post-metadata">

**Author:** ![kofno](https://metaruby.com/user_avatar/metaruby.com/kofno/32/159_2.png) [@kofno](https://metaruby.com/u/kofno)\
**Post date:** [July 16, 2015, 12:50pm UTC](https://metaruby.com/t/share-a-vim-tip-weekly/254/5 "2015-07-16T12:50:38Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![kofno](https://metaruby.com/user_avatar/metaruby.com/kofno/32/159_2.png) [@kofno](https://metaruby.com/u/kofno)\
**Post date:** [July 16, 2015, 12:55pm UTC](https://metaruby.com/t/share-a-vim-tip-weekly/254/6 "2015-07-16T12:55:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![danielpclark](https://metaruby.com/user_avatar/metaruby.com/danielpclark/32/823_2.png) [@danielpclark](https://metaruby.com/u/danielpclark)\
**Post date:** [July 16, 2015, 2:59pm UTC](https://metaruby.com/t/share-a-vim-tip-weekly/254/7 "2015-07-16T14:59:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jgaskins](https://metaruby.com/user_avatar/metaruby.com/jgaskins/32/834_2.png) [@jgaskins](https://metaruby.com/u/jgaskins)\
**Post date:** [July 16, 2015, 3:09pm UTC](https://metaruby.com/t/share-a-vim-tip-weekly/254/8 "2015-07-16T15:09:05Z")

</div>

> `c$`

Or just `C`. 😉

---

<div class="post-metadata">

**Author:** ![AstonJ](https://metaruby.com/user_avatar/metaruby.com/astonj/32/8_2.png) [@AstonJ](https://metaruby.com/u/AstonJ)\
**Post date:** [July 16, 2015, 3:30pm UTC](https://metaruby.com/t/share-a-vim-tip-weekly/254/9 "2015-07-16T15:30:59Z")

</div>

Even better 👍

And:

`D`

to delete to end of line 🙂

---

<div class="post-metadata">

**Author:** ![treble37](https://metaruby.com/user_avatar/metaruby.com/treble37/32/884_2.png) [@treble37](https://metaruby.com/u/treble37)\
**Post date:** [July 17, 2015, 11:59pm UTC](https://metaruby.com/t/share-a-vim-tip-weekly/254/10 "2015-07-17T23:59:37Z")

</div>

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
```

---

<div class="post-metadata">

**Author:** ![danielpclark](https://metaruby.com/user_avatar/metaruby.com/danielpclark/32/823_2.png) [@danielpclark](https://metaruby.com/u/danielpclark)\
**Post date:** [July 20, 2015, 5:16pm UTC](https://metaruby.com/t/share-a-vim-tip-weekly/254/11 "2015-07-20T17:16:33Z")

</div>

This week I’d like to share the [Emmet](http://mattn.github.io/emmet-vim/) 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.

---

<div class="post-metadata">

**Author:** ![kofno](https://metaruby.com/user_avatar/metaruby.com/kofno/32/159_2.png) [@kofno](https://metaruby.com/u/kofno)\
**Post date:** [July 21, 2015, 2:23am UTC](https://metaruby.com/t/share-a-vim-tip-weekly/254/12 "2015-07-21T02:23:48Z")

</div>

```auto
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.

 ![](https://metaruby.com/uploads/default/original/1X/c7a35c7cdd51fa788d613481b136b141ca48c85d.png)

---

<div class="post-metadata">

**Author:** ![treble37](https://metaruby.com/user_avatar/metaruby.com/treble37/32/884_2.png) [@treble37](https://metaruby.com/u/treble37)\
**Post date:** [August 10, 2015, 10:45pm UTC](https://metaruby.com/t/share-a-vim-tip-weekly/254/14 "2015-08-10T22:45:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![danielpclark](https://metaruby.com/user_avatar/metaruby.com/danielpclark/32/823_2.png) [@danielpclark](https://metaruby.com/u/danielpclark)\
**Post date:** [August 13, 2015, 4:59pm UTC](https://metaruby.com/t/share-a-vim-tip-weekly/254/15 "2015-08-13T16:59:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![AstonJ](https://metaruby.com/user_avatar/metaruby.com/astonj/32/8_2.png) [@AstonJ](https://metaruby.com/u/AstonJ)\
**Post date:** [August 13, 2015, 5:03pm UTC](https://metaruby.com/t/share-a-vim-tip-weekly/254/16 "2015-08-13T17:03:07Z")

</div>

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

```auto
:154

```

Will jump to the start of line 154.

---

<div class="post-metadata">

**Author:** ![danielpclark](https://metaruby.com/user_avatar/metaruby.com/danielpclark/32/823_2.png) [@danielpclark](https://metaruby.com/u/danielpclark)\
**Post date:** [August 13, 2015, 5:08pm UTC](https://metaruby.com/t/share-a-vim-tip-weekly/254/17 "2015-08-13T17:08:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![danielpclark](https://metaruby.com/user_avatar/metaruby.com/danielpclark/32/823_2.png) [@danielpclark](https://metaruby.com/u/danielpclark)\
**Post date:** [August 13, 2015, 5:11pm UTC](https://metaruby.com/t/share-a-vim-tip-weekly/254/18 "2015-08-13T17:11:27Z")

</div>

In normal mode undo is simply:

```
u

```

Redo what was undid with:

```
CTRL-r

```

_There… now I’m caught up on sharing weekly tips 😉_

---

<div class="post-metadata">

**Author:** ![AstonJ](https://metaruby.com/user_avatar/metaruby.com/astonj/32/8_2.png) [@AstonJ](https://metaruby.com/u/AstonJ)\
**Post date:** [August 13, 2015, 5:42pm UTC](https://metaruby.com/t/share-a-vim-tip-weekly/254/19 "2015-08-13T17:42:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![danielpclark](https://metaruby.com/user_avatar/metaruby.com/danielpclark/32/823_2.png) [@danielpclark](https://metaruby.com/u/danielpclark)\
**Post date:** [August 27, 2015, 9:49pm UTC](https://metaruby.com/t/share-a-vim-tip-weekly/254/20 "2015-08-27T21:49:40Z")

</div>

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
```

---

<div class="post-metadata">

**Author:** ![danielpclark](https://metaruby.com/user_avatar/metaruby.com/danielpclark/32/823_2.png) [@danielpclark](https://metaruby.com/u/danielpclark)\
**Post date:** [August 27, 2015, 9:56pm UTC](https://metaruby.com/t/share-a-vim-tip-weekly/254/21 "2015-08-27T21:56:41Z")

</div>

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

[Next page](https://metaruby.com/t/share-a-vim-tip-weekly/254.md?page=2)
