Brendan Dawes
Analogue + Digital

Process

Automatic Creation of .gitignore using the Command Line and Vim

I recently chanced upon a great little service called gitignore.io which provides an easy way to make .gitignore files for pretty much any language. You simply put your language of choice inside the text box and on pressing create it instantly makes a nice .gitignore file.

Even better is that you can do this on the command line, saving you having to visit the actual website. On Mac OS if you run this command in the terminal it will add a function called gi() to your .bash_profile

echo "function gi() { curl -L -s https://www.gitignore.io/api/\$@ ;}" >> ~/.bash_profile && source ~/.bash_profile

Now whenever you need to create a .gitignore file for your project you can simply navigate to you project directory and then type:

gi processing >> .gitignore

Which will automatically create a .gitignore file for Processing projects that will look like this:


# Created by https://www.gitignore.io/api/processing

### Processing ###
.DS_Store
applet
application.linux32
application.linux64
application.windows32
application.windows64
application.macosx

# End of https://www.gitignore.io/api/processing

I'm just using Processing as an example but you can simply change to your programming language of choice.

Integration with Vim

As Vim can read in anything executed on the command line, I took things a stage further and created a little function in Vim that will automatically create the correct .gitignore file for the language I'm currently working in.

In my .vimrc I added this function:

function! GetGitIgnore()
    let filename = expand("%:t")
    if filename =~ ".cpp"
        execute "! curl -L -s https://www.gitignore.io/api/openframeworks >> .gitignore"
    endif
    if filename =~ ".pde"
        execute "! curl -L -s https://www.gitignore.io/api/processing >> .gitignore"
    endif
endfunction

This GetGitIgnore() function looks at the current file name extension and if it's .cpp it creates an openFrameworks .gitignore file. If it's a .pde extension it makes a Processing .gitignore. I only have this set up for these two frameworks at the moment as that's pretty much all I need but you get the idea.

I have this mapped in my .vimrc to this key combo:

nnoremap <leader>gi :call GetGitIgnore()<cr>

So now when I press ,gi the correct .gitignore file will automatically be generated.

Of course I mostly have things setup in global gitignore but this does come in handy for specific use cases.