Script Template in vim

Have you ever tried creating Bash/Python or other Script templates with Vim?

Every time I wrote Bash or Python scripts in Vim, I had to add the Shebang #! /bin/bash or #! /usr/bin/python in the scripts manually.

So how can we automate this …


This content originally appeared on DEV Community and was authored by AliMehraji

Have you ever tried creating Bash/Python or other Script templates with Vim?

Every time I wrote Bash or Python scripts in Vim, I had to add the Shebang #! /bin/bash or #! /usr/bin/python in the scripts manually.

So how can we automate this process with Vim to catch *.sh or *.py extensions and create a new file with our desired template, Let’s go for it :

First, you need to create a template file at $HOME/.vim/sh_template.temp with the contents you want:

#! /bin/bash

# =================================================================

# Author: < Your Name >
# Email: <Your Email>

# Script Name:  
# Date Created: 
# Last Modified: 

# Description: 
# < Description of what is this script for >

# Usage:
# < How to use this script , flag usage or ... >

# =================================================================

# ======================== Start Of Code ==========================

set -xe

Next, you need to configure autocmd in Vim by editing $HOME/.vimrc and adding this line:

" ========= Shell Script Template ========================
au bufnewfile *.sh 0r $HOME/.vim/sh_template.temp 

Note:

  • Comment lines in vim scripting that are applied in .vimrc too, begin with " character .
  • au represents autocmd. autocmd docs
  • bufnewfile event for opening a file that doesn’t exist for editing.
  • *.sh cath all files with .sh extension. For Python scripts, you can replace it with *.py .

Now its time to create a shell script with the desired template:

vi Shell_Script.sh

Conclusion:

  • You can follow the same steps for every script or language you need.
  • Start using vim, you’ll love it ;).
  • There’s even a game to learn, give a try Vim Adventures.


This content originally appeared on DEV Community and was authored by AliMehraji


Print Share Comment Cite Upload Translate Updates
APA

AliMehraji | Sciencx (2025-03-08T23:05:25+00:00) Script Template in vim. Retrieved from https://www.scien.cx/2025/03/08/script-template-in-vim/

MLA
" » Script Template in vim." AliMehraji | Sciencx - Saturday March 8, 2025, https://www.scien.cx/2025/03/08/script-template-in-vim/
HARVARD
AliMehraji | Sciencx Saturday March 8, 2025 » Script Template in vim., viewed ,<https://www.scien.cx/2025/03/08/script-template-in-vim/>
VANCOUVER
AliMehraji | Sciencx - » Script Template in vim. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/03/08/script-template-in-vim/
CHICAGO
" » Script Template in vim." AliMehraji | Sciencx - Accessed . https://www.scien.cx/2025/03/08/script-template-in-vim/
IEEE
" » Script Template in vim." AliMehraji | Sciencx [Online]. Available: https://www.scien.cx/2025/03/08/script-template-in-vim/. [Accessed: ]
rf:citation
» Script Template in vim | AliMehraji | Sciencx | https://www.scien.cx/2025/03/08/script-template-in-vim/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.