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

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
Habitual

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

Post by Habitual »

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...
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
User avatar
xenopeek
Level 25
Level 25
Posts: 29605
Joined: Wed Jul 06, 2011 3:58 am

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

Post by xenopeek »

Cool :D
Image
Habitual

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

Post by Habitual »

xenopeek wrote:Cool :D
Well, I knew you would appreciate it. :)
Locked

Return to “Scripts & Bash”