Hacking Git and Vim together by@claudio.santos.ribeiro

Hacking Git and Vim together

March 3rd 2019 6,369 reads
Read on Terminal Reader
react to story with heart
react to story with light
react to story with boat
react to story with money
Cláudio Ribeiro HackerNoon profile picture

Cláudio Ribeiro

Nowadays, in my job as a programmer, I use Vim and Git a lot. And by a lot, I mean A LOT!

Recent versions of Vim brought the terminal to Vim. Because of that it should be easier to use it as a window manager and have a dedicated buffer for Vim. You do your work, you go to the Vim dedicated buffer and commit all your changes.Better yet, Vim already has a plethora of plugins dedicated to manipulating Git from inside Vim. Plugins like vim-fugitive , git-vim or vim-gitgutter come to mind.

But you know me, I’m kind of a hacker… in the sense that I like to hack things together (not the evil masked password stealer type of hacker).

I very quickly realized that 90% of time I use the same 3 or 4 basic Git commands, and because we can use the bang(!) to execute shell commands from command mode inside Vim I decided to hack together a couple of commands and mappings to help me with basic Git usage without having to leave Vim, without a dedicated terminal buffer and without the overhead of a plugin.

I started off by running basic shell commands inside Vim using the bang operator. The results when running those commands were shown in a new window.

Commands like

_:!git diff :!git commit :!git log_

fell under this category. At this point, I also realized that commands that are used commonly and regularly can and should be mapped. For example:

_map <F3> :!git diff_

These were my first experiments in this hacking project. But I soon realized that commands that involve more than one file like add or commit and commands that may have very long outputs, like diff would cause some problems.

That was the moment I started using the % operator. The % operator contains the path to the current file. As so, commands like:

_:!git diff % :!git commit -m “Commit message nº1” %_

now only took the current file into consideration. This way I could manage my commits much better and checks diffs for very specific cases without much problem.

Another command that really came handy was the :r! command. This command was a little bit hard to understand at first. In the Vim help page, we can see that the :r! command:

Execute {cmd} and insert its standard output below the cursor or the specified line. A temporary file is used to store the output of the command which is then read into the buffer.

I use this command whenever I want to paste some output into a file. I got used to using commands like :r! git diff. This way I had the full diff inside a temporary file.


Storing output into files to parse with shell commands can be really useful.

After some trial and error, I ended up with a couple of mappings that reflected my most basic Git usage. The kind of operations I use all the time. For things like merging branches, creating branches, stashing or branch creation I still like to fire the terminal and actually think while typing.

_map <F1> :! git status map <F2> :! git diff % map <F3> :! git add % map <F4> :! git commit -m “This commit was automatically created with Vim” % map <F5> :! git log — oneline — abbrev-commit_

Using just these 5 lines I was able to speed up my Git usage from inside Vim by a reasonable amount.

This worked for me and it was a great exercise. I definitely recommend you give it a try, but also take a look at some of the plugins created for this effect as they have much more functionality and thought behind them.

In the end, I recommend you to test everything but only use what works for you!


Want to learn more about Vim? Want to learn how to use it as an IDE? Check out my new book An IDE Called Vim. It has everything from basic Vim usage to file finding, auto-completion, file manager and more.

react to story with heart
react to story with light
react to story with boat
react to story with money
. . . comments & more!