Vim for Git Users

All you need to know to commit

Janne Kemppainen |

Have you ever typed git commit without -m and felt powerless after being trapped inside of Vim (or Vi) with no way out? I’m here to help you!

I’ve been there and I remember searching for how to exit Vim without even considering the option to actually learn to use it. After finally taking the time to learn the Vim keybindings I’m now using them in other applications too!

The goal of this post is not to make you the ultimate Vim warrior but to let you format your commit messages without fear. I will tell you the things that should be sufficient to make simple edits.

To practice editing with Vim just open the terminal (or Git Bash for Windows) and create a new file with

>> vim test.txt

and the text editor should open. If the command is not found then you can try vi which should come as standard with Unix operating systems, or you can install vim with your package manager (sudo apt install vim, sudo yum install vim, brew install vim, depending on your OS). Vim is Vi-improved so you might want to opt for the installation.

Vim modes

Vim is a modal editor. That might sound like Greek to you but let me explain. Depending on the mode you’re in the keys can do different things. I know that this sounds scary and complicated but there are only three modes that you need to know for creating git commits: insert, normal and command-line.

Insert

Insert mode is exactly what you’d think it is, it lets you insert text. In this mode the keys work as if you were typing with any other editor. Vim tells you that you’re on the insert mode with the text --INSERT-- at the bottom row of the editor. You can exit insert mode by hitting <ESC>. With Vim you’ll be hitting the Esc key a lot.

Normal

Normal mode is where you can use the Vim keybindings to perform actions such as move the cursor or delete lines and words.

Command-line

To enter the command-line mode you need to be in the normal mode first so hit <ESC> if needed. Then to enter command-line type : (colon). The cursor will jump to the bottom of the editor window with a colon before it. Vim is now ready to hear your command such as q<Enter> to quit. Press <ESC> to go back to normal mode.

Movement

Now that you know the modes it’s time for you to learn to walk. Let’s talk about how you can move inside the editor. To move the cursor you need to be in the normal mode so hit <ESC> just to be sure.

The basic movement keys are h, j, k and l and they let you move the cursor left, down, up, and right, respectively. Think of these keys as the arrow keys but with the distinction that they are handily available on the home row of your right hand. These are the only movement keys that you absolutely need to remember so make sure that you learn those.

Here are some other movements that are nice to know but don’t worry about having to learn them all at once:

  • 0 move to the beginning of a row, $ move to the end of a row
  • move one word forward with w and backward with b
  • gg go to the first line, G go to the last line, 12G go to line 12

You can use numbers to modify the movements, for example:

  • 3w move three words to the right
  • 12j move 12 lines down

Insertion

I realize that reading about the movements may not have been that exciting, especially because the file was empty.

Again there are two ways to enter insert mode that I think are important to remember. They are i for insert and a for append. The difference between them is that insert starts before the higlighted character and append after it. If they are capitalized then I will insert at the beginning of the line and A from the end of the line.

Another handy command is C which will replace text until the end of the line, removing any text starting from the cursor and going to the insert mode. You can also use o or O to start a new line after or before the current one.

Now press i or a to enter insert mode and add some lines of text. Split the lines with the enter key because otherwise you’d be writing on a single line even if it wrapped on the editor. After you have typed some lines hit <ESC>to enter normal mode and get a feeling for the h,j,k,l keys by moving around.

Try adding text in the middle of a line and see how i and a behave differently.

Deleting text

If you want to delete text you can always go to the place where you want to delete from, go to insert mode and use the backspace as you would with any other editor. However, Vim gives us ways to delete characters in the normal mode too and they can be really powerful, especially because you can combine them with movements.

  • delete one character with x, three characters with 3x
  • delete one word with dw, three words with 3dw
  • delete one line with dd, three lines with 3dd
  • delete to the end of line D

Copy/paste/cut

Deleting text is actually only cutting and the last piece of text that you have deleted remains available to be pasted again. The options are to put after the cursor with p or before the cursor with P.

Copying in Vim is called yanking. The same movements apply here as with delete.

  • copy one character with yl, three characters with y3l (note you can change the position of the count)
  • copy one word with yw, three words with 3yw
  • copy one line with yy, three lines with 3yy

Undo/redo

Mistakes are always bound to happen and there has to be a way to undo things, right? To undo the last change simply press u while in the normal mode and your last action will be undone. If you went too far with undoing you can redo your actions with Ctrl+r.

You can also do your last action again using .. For example if you delete one word with dw (delete word) you can delete another word simply by hitting . as Vim remembers the last performed action.

Saving and exiting

To save the file you need to go to the command mode (from normal mode) with :, give the write command w and hit enter. Common command mode operations are:

  • :w write file
  • :wq write and exit
  • :q quit without writing
  • :q! force quit without writing

Summary

To summarize, the absolute minimum that you need to know for Git commits are:

  • go to insert mode with i or a
  • go to normal mode with <ESC>
  • (move with h,j,k,l and insert more text if needed)
  • save and exit with :wq

I hope you got some idea of how to use Vim. If you want to learn more I recommend you search for a Vim cheatsheet on Google and just try things out. Practice makes better and after some time it will be like second nature and other editors may even start to feel cumbersome. That is the point where you’ll start searching for plugins that provide Vim keybindings to whatever application you’re using..

Subscribe to my newsletter

What’s new with PäksTech? Subscribe to receive occasional emails where I will sum up stuff that has happened at the blog and what may be coming next.

powered by TinyLetter | Privacy Policy