Dev In The Mountain Header
A Developer In The mountains having fun

Vim Templates / Skeletons

Vim File Templates: Effortless Boilerplate Code

Vim's built-in skeleton files feature allows you to automatically populate new files with predefined templates, eliminating the need for external plugins. This powerful functionality leverages Vim's autocmd capabilities to streamline your workflow.

##Creating Templates##

  1. Create a directory for your templates (e.g., ~/skeletons/)
  2. Save your template files in this directory

##Example Templates:##

  • Bash script (~/skeletons/bash.sh):

    #!/usr/bin/env bash
    
  • README (~/skeletons/readme.md):

    # Project Title
    
    ## Description
    
    ## Installation
    
    ## Usage
    
    ## Contributing
    
    ## License
    

##Configuring Vim##

Add the following to your .vimrc:

autocmd BufNewFile readme.md 0r ~/skeletons/readme.md
autocmd BufNewFile *.sh 0r ~/skeletons/bash.sh

##How It Works##

  • autocmd: Triggers the command on a specific event
  • BufNewFile: The event for creating a new file
  • readme.md or *.sh: File pattern to match
  • 0r: Reads the template file into the buffer at line 0
  • ~/skeletons/...: Path to the template file

##Benefits##

  • Saves time by automatically inserting boilerplate code
  • Ensures consistency across projects
  • Customizable for various file types and project needs

##Advanced Usage##

  • Use wildcards for more flexible matching
  • Create project-specific templates
  • Combine with other Vim features for dynamic content insertion
More places to find me
Mental Health
follow me on Mastodon