brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: gi in Vim returns to where you last typed and starts inserting

Vim
~/tips/tip-vim-gi-resume-insert

gi back to where you stopped typing, in Insert mode

Jumps back to the place where you last left Insert mode and starts typing again there.

When I am typing in a file and jump away to look something up, finding my way back is easy to get wrong. In Vim there is a command for that.

In Normal mode, gi inserts text in the same position as where Insert mode was stopped last time in the current buffer. The Vim help says it uses the '^ mark. So the flow looks like this:

(typing in the middle of a file, press Esc)
gg          jump to the top to read something
/pattern    search a bit more
gi          back to where you stopped typing, in Insert mode

It does more than a jump back: you land in Insert mode and can continue typing where you left off. Compare it with `` or Ctrl-O, which take you back to a previous position but leave you in Normal mode.

The help notes one difference from the key combination `^i: gi differs from it when the mark is past the end of the line, which is what you get after typing at the end of a line.

There is also a capital gI, which inserts text in column 1 instead, and the help says the count repeats the insert. Do not mix them up. gi goes back to the old spot, gI starts at the beginning of the line.

The caveat is in the help text as well. The position is corrected for inserted and deleted lines, but not for inserted or deleted characters. If you edited the same line after the last insert, gi can land a few characters off. It also only knows the last insert in the current buffer. And when the :keepjumps command modifier is used, the mark is not changed, so a command run with it does not move the place that gi returns to.

If you only want to see the old spot without typing, `^ jumps to the mark in Normal mode.

Further reading: Vim help: insert.txt (gi)

Going deeper: Guide: advanced tmux, a config that grows with you, formats, layouts and more

More Vim tips :%s/<Ctrl-r><Ctrl-w>/newname/g /oldname find . -name "*.conf" | vim – vim --clean file.txt :set textwidth=72 All Vim tips (45) All topics

Link