Insert shebang extension for Emacs

Often while writing a BASH or a Python script we need to insert a header line called a shebang line. Although not mandatory but most people like it. It tells the program loader which interpreter to use when the script is executed. I personally hate to write this line for every new script. How about Emacs insert it automatically for you depend upon file extension?

insert-shebang extension for Emacs.

I have a Emacs extension to accomplish this task.

Install it using package.el

M-x package-install RET insert-shebang RET

or install it manually

  • Clone the repository
    git clone
  • Add directory containing insert-shebang.el file to load-path in your ~/.emacs file. For example:
    (add-to-list 'load-path "/path/to/insert-shebang/")
  • To load a package, add:
    (require 'insert-shebang)
  • To enable it globally, add:
    (add-hook 'find-file-hook 'insert-shebang)
  • Optionally, run make to byte-compile the file.

The install instructions are also described in project’s ReadMe file.

By default insert-shebang supports three extension(bash, python and perl) but other interpreters can be added using customize-group, I will get into this detail in a while. It uses an environment env path and appends it with appropriate interpreter.

Instead of using


It puts

#!/usr/bin/env bash

which is easier to change later.


Ones the extension in installed, you don’t have to do much, just try to open a new file and it will insert the shebang line for you.

A new file with shebang inserted.

New file with shebang inserted.

Add support for other interpreters

You can add support for other interpreted languages like csh, php, and =ruby language using customize-group.

M-x customize-group RET insert-shebang RET

and edit Insert Shebang File Types variable. For example if I want to add support for ruby, click INS button and type rb (ruby file extension) in the Extension field and ruby in the Interpreter field. Apply and save the customization.

Add support for compiled language

Optionally if you want to add header line for compiled languages such as C, C++, FORTRAN etc. you need to customize Insert Shebang Custom Headers variable. Of-course this is not a generic way to add header as headers may differ. This is an optional feature. This variable is set to nil by default.

Ignore file extensions

Sometimes you want insert-shebang to ignore certain files like .log, .conf, .tex etc. Customize the variable Insert Shebang Ignore Extensions. By default insert-shebang ignores .txt and .org files.

Track ignored files

It may happen that some existing file don’t have a shebang line like a python script in a django project. In this case insert-shebang will ask you before automatically inserting a shebang line. If you say NO at this point, it will save the path of the ignored file in ~/.insert-shebang.log. The log file name can be customize using Insert Shebang Track Ignored Filename variable.

Open log buffer

You may also open ~/.insert-shebang.log file for editing using

M-x insert-shebang-open-log-buffer


What if my program already has a shebang line?

If the program file already has a shebang line, insert-shebang will open it as it is.

What if my program has some other line say a comment at the top?

First of all insert-shebang will ask you to insert a shebang line. If you choose YES, it will insert it as a top most line with your comment below it.

Comments and suggestions are most welcome.


2 thoughts on “Insert shebang extension for Emacs

  1. Aaron says:

    I’ll stick with `C-c :’ (the command sh-set-shell) and skeleton mode for everything else since I need to auto-insert more than just a shebang.


    • Agreed, but what about support for Python, Perl etc. One can customize ‘insert-shebang’ group for all scripting languages and to some extend compiled languages. Also many python developers don’t put shebang line in the scripts say for example the Django framework. These file can be safely ignored.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s