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

Forum rules
Before you post please read how to get help
User avatar
Habitual
Level 12
Level 12
Posts: 4012
Joined: Sun Nov 21, 2010 8:31 pm
Location: Youngstown, Ohio

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

Postby Habitual » 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...
Do you realize how hard it is for me to explain to people that I know you?

User avatar
xenopeek
Level 23
Level 23
Posts: 19032
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

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

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

Cool :D
Image

User avatar
Habitual
Level 12
Level 12
Posts: 4012
Joined: Sun Nov 21, 2010 8:31 pm
Location: Youngstown, Ohio

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

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

Vincent Vermeulen wrote:Cool :D


Well, I knew you would appreciate it. :)
Do you realize how hard it is for me to explain to people that I know you?


Return to “Scripts & Bash”

Who is online

Users browsing this forum: No registered users and 1 guest