Automatically Insert Author Metadata in Vim with a Single F4 Shortcut
This guide shows how to configure Vim so that pressing F4 automatically adds or updates a header containing author details, file name, and modification timestamp, using custom functions and a mapping, making it easy for programmers to embed metadata into their scripts.
Last Friday Derek shared a Vim mind map; today we continue with a practical tip.
Many programmers want to embed author information in their scripts; here's how to achieve it in Vim.
Simply add the following code to your vimrc.
# Add author information with F4
map <F4> ms:call TitleDet()<cr>'s
function AddTitle()
call append(0,"##############################################")
call append(1,"#")
call append(2,"#Author: wangshenghui - [email protected]")
call append(3,"#")
call append(4,"#QQ:417685417")
call append(5,"#")
call append(6,"#Last modified: ".strftime("%Y-%m-%d %H:%M"))
call append(7,"#")
call append(8,"#Filename: ".expand("%:t"))
call append(9,"#")
call append(10,"#Description: ")
call append(11,"##############################################")
call append(12,"#!/bin/bash")
echohl WarningMsg | echo "Successful in adding the copyright." | echohl None
endfunction
# Update file modification time and name
function UpdateTitle()
normal m'
execute '/#*Last modified:/s@:.*$@\=strftime(":%Y-%m-%d %H:%M")@'
normal ''
normal mk
execute '/#*Filename:/s@:.*$@\=":".expand("%:t")@'
execute "noh"
normal 'k
echohl WarningMsg | echo "Successful in updating the copy right." | echohl None
endfunction
# Detect if author header exists in first 10 lines
function TitleDet()
let n=1
while n < 10
let line = getline(n)
if line =~ '^\#\s*\S*Last\smodified:\S*.*$'
call UpdateTitle()
return
endif
let n = n + 1
endwhile
call AddTitle()
endfunctionIsn't it easy? Give it a try!
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
