📜 ⬆️ ⬇️

Vim in full: Working with Git

Table of contents


  1. Introduction (vim_lib)
  2. Plugin Manager without fatal flaws (vim_lib, vim_plugmanager)
  3. Project level and file system (vim_prj, nerdtree)
  4. Snippets and File Templates (UltiSnips, vim_template)
  5. Compiling and doing anything (vim-quickrun)
  6. Work with Git (vim_git)
  7. Deploy (vim_deploy)
  8. Testing with xUnit (vim_unittest)
  9. The library on which everything is kept (vim_lib)
  10. Other useful plugins

Do you often have to use git? In a sense, do you commit changes every hour or every few minutes? I do this very often and do not follow the purity of the repository, because I consider it to be nothing more than a journal of changes, and not a work of art. This approach requires the editor to integrate well with Git, allowing you to create a new commit in a couple of keystrokes, return to the previous state, switch to another branch, and so on. If you use a modern development environment that integrates with Git, you are very lucky, but what do users of the Vim editor do? Is there a plugin that not only implements Vim commands for GitCommit , GitCheckout and GitBranch , but provides a convenient interface in the best traditions of the editor?

GUI to CLI


When I first thought about integrating Vim with Git, I, of course, tried ready-made plug-ins and solutions, but why do they all only implement the commands of the editor, and do not provide something more? I actively use Git using both the CLI and various GUI programs. Both have advantages and disadvantages, but the following tasks are often accomplished in the work:

Git has a lot more useful, but, as my experience has shown, these four actions are most often used. It is important to note that all of them are by no means trivial. For example, to add several files to a commit, you need to execute two commands and list the addresses of these files, which is quite long (when using CLI). On the other hand, to constantly switch between the editor and the program used with the GUI for working with Git is also quite expensive, since for me personally, it is difficult to switch between the context (read - interface) of the editor (Vim) and GUI (mouse) used. Noticing this, I decided to write my own plugin for Vim, which will be convenient for users of this editor and at the same time is quite simple, even for GUI lovers. By the way, the vim_lib library, about which you have already heard in my previous articles, started from this plugin.

What is the raisin of this plugin? Using hotkeys you can easily index files, commit an index, etc. In turn, the editor's windows are used by the plugin so that it turns out a GUI with the usual control of the Vim user. For example, to delete a branch, just open the branches window, move the cursor to the desired branch and use dd , “like at home”!

Main features


The plugin I want to talk about is called vim_git . The plugin is quite complicated, since I tried to take into account all the needs of the Git user, from which it implements many possibilities, but in this article I will try to tell everyone as simply and clearly as possible.

About windows


For a start, remember: the plugin actively uses the windows (window) of the editor as elements of the graphical interface.
Example


Windows are implemented in such a way that they are used using standard Vim editor commands. That is, to select an element, for example a branch, you need to move the pointer (text) to the name of this branch, to delete it, use the dd command , and to add the a command, and so on everywhere. All windows are accompanied by built-in documentation, which is indicated by a hint like "Branch list (Press? For help)". Pressing?, The tooltip will be expanded in the same window.
Example


Simple enough?

About the plugin API


Also, you should know: all the plugins I create implement the API in the form of Vim functions. So, all APIs of this plugin are present in the form of vim_git # ... functions, for example, vim_git # status , vim_git # tagList , vim_git # branchList , etc. At the same time, most plug-ins implement both Vim menu items that use the plug-in API and commands. Personally, it is more convenient for me to bind keys, this is faster than any menus and commands of the editor, but I do not insist.

Here is an example of how I zabindil keys for the plugin vim_git:
Plugin 'vim_git', { \ 'map': { \ 'status': '<Leader>gs', "   \ 'log': '<Leader>gl', "    \ 'branchList': '<Leader>gb', "   \ 'tagList': '<Leader>gt', "   \ 'addCurrent': '<Leader>ga', "      \ 'addAll': '<Leader>gA', "      \ 'commit': '<Leader>gc', "   \ 'commitAll': '<Leader>gC', "         \ 'pushCurrent': '<Leader>go', " Push \ 'pullCurrent': '<Leader>gi', " Pull \ 'remoteList': '<Leader>gr', "    \ } \} 

About the stack of windows


Many windows vim_git (as well as all my plugins) are implemented in the form of a stack. This means that if you execute a command in a window that causes another window to open (for example, trying to see the differences between two commits), this new window will open in the current window. If you close a new window (for example using : q ), the first window opens. So you use a stack of windows.
If these points are clear to you, then get down to business.

Index


To view the status of a Git index, use vim_git # status (or the gs combination). This window contains detailed information about the status of the repository and the index in the form of a response from Git itself. That is, the plugin does not perform any processing, everything is standard.
Status window


The following commands are implemented in this window (keyboard shortcuts):

Commit


When committing, the plugin will open a new window in which you will need to write a comment. After saving and closing this window (for example using ZZ ), the commit will be created. To cancel the creation of a commit, just close the window without saving ( : q! ). Naturally, only those changes that were added to the index at the time of the commit will be included in the commit.
Commit window


Commit history


To view the commit history, use vim_git # log (or a combination of gl). This window displays the history of commits in two modes:

The following commands are implemented in this window (keyboard shortcuts):

Separately, mention should be made of the filter and the state of the commit.

History filter


The command (keyboard shortcut) f opens a dialog in which you can add various filters for the story, be it the name of the author you are interested in or the date of the creation of a commit.
Filter dialog


State of commit


If we hover over the hash of the commit we are interested in and press s , the commit status window opens, listing all the files changed by the commit.
Commit status window


The following commands are implemented in this window (keyboard shortcuts):

Branches


To view the available branches (local and remote) use vim_git # branchList (or a combination of gb).
Branch window


The following commands are implemented in this window (keyboard shortcuts):

Remote repositories


To view the registered remote repositories, use vim_git # remoteList (or the combination of gr).
Remote repository window


The following commands are implemented in this window (keyboard shortcuts):

Tags


To view tags use vim_git # tagList (or a combination of gt).
Tag window


The following commands are implemented in this window (keyboard shortcuts):

Bye all


The power of the plugin is in a convenient API and GUI, which allows me to actively use Git without leaving the editor. Of course, I didn’t cover all vim_git functions, since the article is already quite long and I don’t want to bore the reader, so you will find many more interesting things in this plugin (for example, using go is easy to push, and using gi pull changes).

')

Source: https://habr.com/ru/post/261783/


All Articles