Vim Tips Wiki
(Move categories to tip template)
(Remove html character entities)
Line 16: Line 16:
 
<pre>
 
<pre>
 
" automatically give executable permissions if filename is *.sh
 
" automatically give executable permissions if filename is *.sh
au BufWritePost *.sh :!chmod a+x &lt;afile&gt;
+
au BufWritePost *.sh :!chmod a+x <afile>
 
" automatically insert "#!/bin/sh" line for *.sh files
 
" automatically insert "#!/bin/sh" line for *.sh files
 
au BufEnter *.sh if getline(1) == "" | :call setline(1, "#!/bin/sh") | endif
 
au BufEnter *.sh if getline(1) == "" | :call setline(1, "#!/bin/sh") | endif
 
" automatically give executable permissions if file begins with #!/bin/sh
 
" automatically give executable permissions if file begins with #!/bin/sh
au BufWritePost * if getline(1) =~ "^#!/bin/[a-z]*sh" | silent !chmod a+x &lt;afile&gt; | endif
+
au BufWritePost * if getline(1) =~ "^#!/bin/[a-z]*sh" | silent !chmod a+x <afile> | endif
 
</pre>
 
</pre>
   
Line 34: Line 34:
 
<pre>
 
<pre>
 
" Python header
 
" Python header
function! &lt;SID&gt;PythonHeader()
+
function! <SID>PythonHeader()
 
:call setline(1, "#! /bin/sh")
 
:call setline(1, "#! /bin/sh")
 
:call append(1, "# vim: filetype=python")
 
:call append(1, "# vim: filetype=python")
Line 49: Line 49:
 
<pre>
 
<pre>
 
" Shell header
 
" Shell header
function! &lt;SID&gt;ShellHeader()
+
function! <SID>ShellHeader()
 
:call setline(1, "#! /bin/sh")
 
:call setline(1, "#! /bin/sh")
 
:call append(1, "")
 
:call append(1, "")
Line 76: Line 76:
   
 
<pre>
 
<pre>
au BufWritePost * if getline(1) =~ "^#! ?/bin/[a-z]*sh" | silent !chmod a+x &lt;afile&gt; | endif
+
au BufWritePost * if getline(1) =~ "^#! ?/bin/[a-z]*sh" | silent !chmod a+x <afile> | endif
 
</pre>
 
</pre>
   
Line 82: Line 82:
   
 
<pre>
 
<pre>
au BufWritePost * if getline(1) =~ "^#! ?/bin/[a-z]*sh" | silent !chmod a+x &lt;afile&gt;
+
au BufWritePost * if getline(1) =~ "^#! ?/bin/[a-z]*sh" | silent !chmod a+x <afile>
 
au BufWritePost * | endif
 
au BufWritePost * | endif
 
</pre>
 
</pre>

Revision as of 09:15, 29 September 2008

Tip 709 Printable Monobook Previous Next

created May 2, 2004 · complexity basic · author Yakov Lerner · version 6.0


If you create lots of shell scripts, then you'll find this useful:

" automatically give executable permissions if filename is *.sh
au BufWritePost *.sh :!chmod a+x <afile>
" automatically insert "#!/bin/sh" line for *.sh files
au BufEnter *.sh if getline(1) == "" | :call setline(1, "#!/bin/sh") | endif
" automatically give executable permissions if file begins with #!/bin/sh
au BufWritePost * if getline(1) =~ "^#!/bin/[a-z]*sh" | silent !chmod a+x <afile> | endif

Comments

I adopted it for python files:

au BufEnter *.py if getline(1) == "" | :call setline(1, "#!/usr/bin/env python") | endif

Might be useful for other languages as well, maybe an idea for a plugin for multiple languages.


Here's something a little better for python:

" Python header
function! <SID>PythonHeader()
  :call setline(1, "#! /bin/sh")
  :call append(1, "# vim: filetype=python")
  :call append(2, "\"\"\":\"")
  :call append(3, "exec python $0 ${1+\"$@\"}")
  :call append(4, "\"\"\"")
  :call append(5, "")
  exe 6
endfunction
au BufEnter *.py if getline(1) == "" | call s:PythonHeader() | endif

Or for shell:

" Shell header
function! <SID>ShellHeader()
  :call setline(1, "#! /bin/sh")
  :call append(1, "")
  exe 2
endfunction
au BufEnter *.sh if getline(1) == "" | call s:ShellHeader() | endif

I did not want the script to keep doing the chmod even when the file is already excutable, so this is what I have in my vimrc:

" Define a function that can tell me if a file is executable
function! FileExecutable (fname)
  execute "silent! ! test -x" a:fname
  return v:shell_error
endfunction
" Automatically make Perl and Shell scripts executable if they aren't already
au BufWritePost *.sh,*.pl,*.cgi if FileExecutable("%:p") | :!chmod a+x % ^@ endif

Note that the ^@ is actually Ctrl-V Ctrl-J (an embedded new line), because you cannot use | as a separator after an external command.


For the problem with " | endif" after a shell command - e.g.:

au BufWritePost * if getline(1) =~ "^#! ?/bin/[a-z]*sh" | silent !chmod a+x <afile> | endif

I noticed that the above works fine for csh / tcsh, but not in a bash shell. I got this to work fine for a bash shell:

au BufWritePost * if getline(1) =~ "^#! ?/bin/[a-z]*sh" | silent !chmod a+x <afile>
au BufWritePost * | endif