Vim and git

Vim is a very useful and helpful editor for creating and editing different types of files more efficiently. Many new features are added in this editor that makes it a powerful editor. Many plugins are developed by many coders for this editor to increase and configure its core functionalities. Some of them are Pathogen, Syntastic, indent guides, Fugitive, Git Gutter, etc. Git is a distributed version control system (DVCS) that helps the developers to manage the modified source codes over time. It is totally free to use. Using git command, the track changes and the revision history of the source codes can be easily traced. Git command works in the command line interface. The vim plugin named fugitive plugin is developed by Tim pope which is used to work with the git tool without terminating the editor. So, vim and git can work together by using the fugitive plugin. How you can install and use this plugin for vim is shown in this tutorial.

Install git

Before starting this tutorial, you have to check the git is installed on the system or not. Git is not installed by default on Ubuntu operating system. Run the following command to install git on Ubuntu.

$ sudo apt-get install git

Install Fugitive Plugin

Run the following command to install the fugitive plugin on Ubuntu.

$ sudo apt-get install vim-fugitive

Initialize git repository

It is necessary to initialize git repository before executing any git command. ‘git init’ command is used to initialize a git repository for any existing or new project. Run the following command from the terminal to initialize an empty git repository.

$ git init

Adding file in git repository

A text file named employee.txt is used to test the uses of the fugitive plugin in this tutorial. The content of this file is given below.


11001    Sales           45      $3000
11002    HR              32      $1500
11003    Marketing       26      $1200
11004    HR              25      $2500
11005    Sales           30      $1000

git add <file>’ command is used to add modification in the current working directory and add the file in vim buffer. It informs git command to add the update in a particular file after running ‘git commit’ command.

$ git add employee.txt

The command, ‘git commit’ will require to run for saving the updates of any file in the local repository. Run the command from the terminal.

$ git commit

Remove the ‘#’ symbol from the line, ‘modified:  employee.txt’ file to save the changes of this file.

Using Gblame

‘git blame’ command is used to show the details of the most recent modification of any file for each line. Run the command from the terminal for the file, employee.txt.

$ git blame employee.txt

It will show a similar output like the following image.

You can get the same output of the above command from vim editor by using ‘:Gblame’ wrapper of fugitive plugin. You don’t need to the terminal the editor to do the task. Open, ‘employee.txt’ file in the vim editor by running the following command.

$ vim employee.txt

Press ESC and :Gblame to retrieve the recent modification information of the file in the editor.

: Gblame

You will get the following formatted output after pressing Enter key.

‘:Gdiff’ wrapper is used as the alternative of ‘git diff’ command. When it used for any particular file then it displays the difference between the previous content and present content of the file after current commit. ‘:Gdiff’ can be used to display the difference between any revision of the file. It can take an argument of any revision to retrieve the particular version of the file. There are two other related commands for searching the difference between any two versions of the file by adding or retrieving any diff section. These commands are ‘:diffput’ and ‘:diffget’. The use of ‘:Gdiff’ command is shown in the next section of this tutorial.

Open employee.txt file and press ‘i’ to enable the INSERT mode. Modify the file by adding an entry for the employee, ‘11006’. Press ‘ESC’ and ‘:x’ to save and close the file.

Re-open the file, employee.txt in the vim editor and type ‘:Gdiff’ and press Enter to find out the difference between the current commit and the previous version of the file.

: Gdiff

The following output will display after executing the ‘:Gdiff’ command. The newly inserted record will be displayed as highlighted text to show the difference.

‘:Gread’ command works as the alternative of ‘git checkout <file>’ command. It can also take the argument of any revision file like ‘:Gdiff’ command.

: Gread

When this command is executed for the current version of the employee.txt file then it will show the following output. The file contains 6 employee records and the output shows the text ‘6 more lines’.

:Gstatus‘ command works as the alternative of ‘git status’ command.  It shows detail information about the status of all files or the current file. There are many custom key to interact with the status window. Some of them are D, cc, <C-p>, <C-n>  etc. Open employee.txt file in the vim editor and run ‘:Gstatus’ command.

$ vim employee.txt

: Gstatus

‘:Gmove’ command works similar to  ‘git mv’ command. It is used to rename any existing file from vim editor. Suppose, you want to rename employee.txt file by emp.txt. Open the file in vim editor and type the following command and press Enter to rename the file.

:Gmove emp.txt

‘:Gremove’ command works similar to ‘git rm’ command. It is used to delete the file from the vim buffer and current working directory. Open the file that you want to delete from the current working directory and run the command with ‘!’ symbol to forcefully delete the file. Here, employee.txt file is selected for deletion.



How the vim users can execute git commands by using vim plugin is shown in this tutorial. If you need to execute git commands and want to use vim editor together then it is better to use fugitive plugin of vim. Hope, after reading this tutorial the reader will be able to use fugitive plugin and run basic git commands from vim editor.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.