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
M-x package-install RET insert-shebang RET
or install it manually
- Clone the repository
git clone https://github.com/psachin/insert-shebang.git
- Add directory containing
~/.emacsfile. For example:
(add-to-list 'load-path "/path/to/insert-shebang/")
- To load a package, add:
- To enable it globally, add:
(add-hook 'find-file-hook 'insert-shebang)
- Optionally, run
maketo 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
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.
New file with shebang inserted.
Add support for other interpreters
You can add support for other interpreted languages like
php, and =ruby language using
M-x customize-group RET insert-shebang RET
and edit Insert Shebang File Types variable. For example if I want to add support for
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
.tex etc. Customize the variable Insert Shebang Ignore Extensions. By default insert-shebang ignores
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
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.