1

I need to configure vim to add the shebang #!/bin/bash if the edited file is in the directory /usr/bin/ and if it is empty (or even better if it doesn't exist)

2

Here's one way to add the shebang to any new file in /usr/bin/:

func Shebang(path)
    echo a:path
    if a:path =~ "/usr/bin/[^/]*"
        normal i#!/bin/bash
    endif
endfunc
autocmd BufNewFile /usr/bin/* execute Shebang(expand('<amatch>'))

This will only execute on new files, and also ensures the file isn't in a subdirectory in /usr/bin. You could also modify it so the pattern of *.sh (a new shell file created anywhere) also gets the shebang. If you don't care about subdirectories and only want this to every work in /usr/bin, you could also just do:

autocmd BufNewFile /usr/bin/* normal i#!/bin/bash
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.