19 lines
		
	
	
		
			No EOL
		
	
	
		
			1.6 KiB
		
	
	
	
		
			Text
		
	
	
	
	
	
			
		
		
	
	
			19 lines
		
	
	
		
			No EOL
		
	
	
		
			1.6 KiB
		
	
	
	
		
			Text
		
	
	
	
	
	
<p>How does the process of script creation typically go? You create a file (e.g. <code class="language-plaintext highlighter-rouge">script.py</code>),
 | 
						||
you write some code in it, and finally you make the file executable, so you can
 | 
						||
run it directly (e.g. by typing <code class="language-plaintext highlighter-rouge">./script.py</code>). Turns out that Emacs has one very
 | 
						||
specific helper for making a script executable, namely the function
 | 
						||
<code class="language-plaintext highlighter-rouge">executable-make-buffer-file-executable-if-script-p</code>.</p>
 | 
						||
 | 
						||
<p>What the function does is to check if the buffer file has a shebang (e.g. <code class="language-plaintext highlighter-rouge">#!/bin/ruby</code>) in it and then it modifies its permissions, if necessary. The function is meant to be used
 | 
						||
in a hook, most typically <code class="language-plaintext highlighter-rouge">after-save-hook</code>:</p>
 | 
						||
 | 
						||
<div class="language-emacs-lisp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nv">add-hook</span> <span class="ss">'after-save-hook</span>
 | 
						||
          <span class="ss">'executable-make-buffer-file-executable-if-script-p</span><span class="p">)</span>
 | 
						||
</code></pre></div></div>
 | 
						||
 | 
						||
<p>I’ve been using this little trick for ages, and I was reminded of it today
 | 
						||
when I came across <a href="https://github.com/bbatsov/prelude/issues/1343">this Prelude issue</a>. I guess in some cases making a file executable automatically might be
 | 
						||
undesirable, but I’ve never ran into any issues myself.</p>
 | 
						||
 | 
						||
<p>That’s all I have for you today. Feel free to share other tips for working with
 | 
						||
scripts in Emacs in the comments. Keep hacking!</p> |