I've played with git in the past and I know it's good practice but I never used it because it was just too much of a hassle for my little pet, hobbyist projects.

I'm looking to get back into git and I'm hoping vim can overcome my innate laziness and get me in the good habit of making and documenting my changes to the repository.

The project I have has three branches: master (a forked copy of someone else's work that doesn't work on my machine), working-copy (a version that will actually work on my machine), and rev-engineer (a version where I can experiment and then merge stuff that I know works into the working-copy branch from time to time).

I won't be doing any heavy lifting with git. It's just my own project. All I'll be doing is mostly working in the rev-engineer branch and then merging those changes back into the working-copy once I'm sure it won't break anything.

I also want to get more familiar with Vim so I thought it would be good experience to roll my own. So rather than some heavy plugin, what I'd like is just some basic shortcut keys that will help me make working with git as painless as possible. I don't need any power tools. I just want to be able to do some simple stuff like commit my branch changes, merge my two branches from time to time, and push them out to remote repository branches.

But Vim has many ways to use hotkeys so I'm wondering what the best and easiest .vimrc config commands are for accomplishing something like.

closed as primarily opinion-based by Herb Wolfe, Tommy A, statox, nobe4, akshay Feb 15 '17 at 3:06

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    Although you say you want to write your own code to learn VimL, in this case I would recommend using the fugitive plugin by Tim Pope. This really simplifies interaction with git a lot. Learning vimscript for me mostly makes sense to do things that no one else would ever want or has ever thought of (there will be more than you think), and to configure the plugins to your liking. Good plugins are for the common stuff and take care of many use cases you are not currently thinking of. As you are going through "learning vimscript the hard way" you'll get plenty of ideas of what to change. – Octaviour Feb 13 '17 at 14:49
  • I did look briefly at that plugin but it looked heavy. Does it allow me to do add, commits, pushes and swap branches with a couple of keystrokes like my script below? I mean, that's all I really need for now. I'm using what I wrote below and I love it. It's certainly not professional but it's good enough for now. – StevieD Feb 13 '17 at 14:52
  • 1
    It is kind of big, but does not get in the way. Adds and commits are easy, giving you a vim buffer where you can quickly select the files to add. To swap branches and push you would use a mapping similar to what you have below, so you won't gain much in that area. Also I can recommend a plugin manager such as Pathogen or Vundle. This will allow you to easily install and remove plugins just to try them out. Finally, all plugins are just vim scripts, so if you don't like something you can completely change it (although a good plugin should have enough options to not need changing) – Octaviour Feb 13 '17 at 14:59
  • Yeah, I tried getting heavy into vim last year and installed all kinds of crazy stuff like Pathogen and Vundle but they really got in the way of me learning the basics. I didn't know which behavior was supplied by VIM and which was from a plugin. And my .vimrc was a mess of junk copied and pasted from crap I found on the internet. So I'm taking a different, more anal approach now and just adding in what I need. Once I get really comfortable with VIM, I definitely plan on adding in more bells and whistles from available plugins. Thanks for your suggestions, though. I really appreciate it. – StevieD Feb 13 '17 at 15:04
  • Seems like a good undertaking would be to write a tutorial for idiots like me who want shortcuts to boosting their productivity with VIM but who really have no clue. Maybe I'll take that on when I get some experience. – StevieD Feb 13 '17 at 15:07

So here's what I came up with so far, my first official VIM script. I'm not sure if there are any downsides to my approach. Maybe there's a more straightforward way than autocmds? If anyone has feedback, I'd appreciate it.

augroup misc
  autocmd BufEnter * nmap <buffer> ga :w<CR>:silent :!git add %<CR>:redraw!<CR>
  autocmd BufEnter * nmap <buffer> gs :!git status<CR>
  autocmd BufEnter * nmap <buffer> gp :!git push origin master<CR>
  autocmd BufEnter * nmap <buffer> gc :call GetCommMsg()<CR>
  autocmd BufEnter * nmap <buffer> gl :call PrintLog()<CR>
augroup END

function! PrintLog()
  !echo '================================================================='
  !git log 

function! GetCommMsg()
"  let curline = getline('.')
  call inputsave()
  let msg = input('Enter commit message: ')
  call inputrestore()
  call system("git commit -m '" . msg . "'")
  • 4
    Unless I'm missing something, currently you're adding separate buffer-local mappings to every buffer. You could achieve the same effect (mappings that are globally available) by removing all the autocmd stuff and simply creating the mappings. e.g. nmap gs :!git status<CR> – Rich Feb 13 '17 at 9:37
  • 3
    Also, I'd recommend changing your nmap to nnoremap: learnvimscriptthehardway.stevelosh.com/chapters/05.html – Rich Feb 13 '17 at 9:38
  • 1
    The mappings you've created don't contain anything to further that goalm though. Using e.g. your gs mapping will have the same effect as typing :!git status<CR> would have. Try typing that into different split windows and see what happens. – Rich Feb 13 '17 at 14:24
  • 1
    I think you want is either a single autocmd to update your pwd when you change buffers (but note that this will affect other things in Vim), or to change your mappings first to change pwd before running the git commands. – Rich Feb 13 '17 at 14:25
  • 1
    I think nnoremap gc :!git commit -m ''<left> would be a simpler alternative to your GetCommMsg() function. You are already staring at the command line why not simply use it? – Peter Rincker Feb 13 '17 at 18:13

Not the answer you're looking for? Browse other questions tagged or ask your own question.