A small Vim plugin that inserts a file header for selected file types and keeps the
Last modified timestamp up to date.
- Automatically inserts a header for new files (
BufNewFile) matching these extensions:*.sh,*.conf,*.service,*.timer,*.info
- Provides a manual command to insert a header into the current buffer:
:AddHeader
- Updates the
Last modified:line on save (BufWritePre) only when:- the buffer is actually modified, and
- a header is present in the file
- Path formatting:
- Files inside your home directory are shown as
~/...(no username shown) - Files outside
$HOMEkeep their absolute path (e.g./etc/...)
- Files inside your home directory are shown as
Create directory-tree and clone the repo:
mkdir -p ~/.vim/pack/helpers/start
cd ~/.vim/pack/helpers/start
git clone https://github.com/hasmaneuroda/vim-fileheader.gitOr manually put the plugin file here:
~/.vim/pack/helpers/start/vim-fileheader/plugin/fileHeader.vim
Vim loads packages under pack/*/start/* automatically on startup.
If you use a plugin manager like vim-plug simply add
the following lines to your ~/.vimrc:
call plug#begin('~/.vim/plugged')
Plug 'hasmaneuroda/vim-fileheader'
call plug#end()If you keep it under pack/*/opt/*, you must load it manually, e.g. in ~/.vimrc:
packadd vim-fileheaderCreate a new file matching one of the configured extensions, e.g.:
vim new-script.shvim my.service
The header is inserted automatically.
Shell scripts:
- If the file already starts with a shebang (
#!...), the header is inserted after it. - If no shebang exists,
#!/usr/bin/env bashis prepended and the header follows.
To insert a header into the current buffer (including existing files), run:
:AddHeaderNo.
This plugin already defines the :AddHeader command inside the plugin file, so you do
not need to define it again in ~/.vimrc when the plugin is installed properly.
You only need a command! line in ~/.vimrc if you are not using the plugin as a
plugin file (for example, if you copied only the function(s) into your ~/.vimrc and
did not install the plugin under pack/.../start/... or otherwise load it).
To change which file types get an automatic header, edit the BufNewFile pattern list
in plugin/fileHeader.vim, for example:
autocmd BufNewFile *.sh,*.conf,*.service,*.timer,*.info call s:InsertHeader()To disable automatic insertion entirely, remove or comment out the BufNewFile autocmd
block.
- The plugin does not use
:substitutefor updating timestamps. This avoids errors related to the "previous substitute pattern" state and works reliably regardless of how the header was inserted. - Header detection looks for
# File:near the top of the file. Timestamp updates only touch files that already contain this header marker.