vi[m] hook to make .sh files automatically executable

Forum rules
Before you post please read this

vi[m] hook to make .sh files automatically executable

Postby Habitual on Mon Jun 25, 2012 3:08 pm

I found this today and thought I'd share.
add the following lines to the end of your ~/.vimrc file

Code: Select all
au BufWritePost * if getline(1) =~ "^#!" | if getline(1) =~ "/bin/" | silent !chmod a+x <afile> | endif | endif


This code will automatically change the file to executable if the first line contains both "#!" and "/bin/".
Once you add #!/bin/sh (for example) to the start of a file and save it, the file will be immediately executable.
755 upon exiting vim.

YMMV...
What's a landing but a take off in reverse?
User avatar
Habitual
Level 8
Level 8
 
Posts: 2213
Joined: Sun Nov 21, 2010 8:31 pm
Location: LM17Q-Xfce

Linux Mint is funded by ads and donations.
 

Re: vi[m] hook to make .sh files automatically executable

Postby xenopeek on Wed Jun 27, 2012 6:16 am

Cool :D
User avatar
xenopeek
Level 21
Level 21
 
Posts: 14996
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Re: vi[m] hook to make .sh files automatically executable

Postby Habitual on Tue Jul 10, 2012 7:22 am

Vincent Vermeulen wrote:Cool :D


Well, I knew you would appreciate it. :)
What's a landing but a take off in reverse?
User avatar
Habitual
Level 8
Level 8
 
Posts: 2213
Joined: Sun Nov 21, 2010 8:31 pm
Location: LM17Q-Xfce


Return to Scripts & Bash

Who is online

Users browsing this forum: No registered users and 2 guests