Vim for Git Users
All you need to know to commitJanne 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 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 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 mode is where you can use the Vim keybindings to perform actions such as move the cursor or delete lines and words.
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.
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
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:
0move to the beginning of a row,
$move to the end of a row
- move one word forward with
wand backward with
gggo to the first line,
Ggo to the last line,
12Ggo to line 12
You can use numbers to modify the movements, for example:
3wmove three words to the right
12jmove 12 lines down
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 to start a new line after or before the current one.
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
a behave differently.
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
- delete one word with
dw, three words with
- delete one line with
dd, three lines with
- delete to the end of line
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
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
- copy one line with
yy, three lines with
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
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:
:wqwrite and exit
:qquit without writing
:q!force quit without writing
To summarize, the absolute minimum that you need to know for Git commits are:
- go to insert mode with
- go to normal mode with
- (move with
h,j,k,land insert more text if needed)
- save and exit with
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..