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

Forum rules
Before you post please read how to get help

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...
If you can't find an answer at Google, you must be typing in Braille.
User avatar
Habitual
Level 10
Level 10
 
Posts: 3477
Joined: Sun Nov 21, 2010 8:31 pm
Location: Youngstown, Ohio

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
Image
User avatar
xenopeek
Level 23
Level 23
 
Posts: 18174
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. :)
If you can't find an answer at Google, you must be typing in Braille.
User avatar
Habitual
Level 10
Level 10
 
Posts: 3477
Joined: Sun Nov 21, 2010 8:31 pm
Location: Youngstown, Ohio


Return to Scripts & Bash

Who is online

Users browsing this forum: No registered users and 2 guests