DEV Community

Konnor Rogers
Konnor Rogers

Posted on

3

Inserting a string on the first line of every file with Vim

Alright here it goes, I needed to add a header to all files. There are roughly ~72 files and I didn't want to do it by hand. The header in question was for test files in Shoelace.

I stumbled across this StackOverflow link:

https://stackoverflow.com/questions/30541582/how-do-i-insert-the-same-line-into-multiple-files-using-vim

Which was 99% of what I wanted, but didnt show how to use "put" without using a register.

So here's the magic few commands that saved me a bunch of time.

:args ./**/*.test.ts
:argdo 1put! = 'import \"../../../dist/shoelace.js\"' | write | update
Enter fullscreen mode Exit fullscreen mode

The first line :args ./**/*.test.ts tells us what files we want to look at. The second line: :argdo 1put! = 'import \"../../../dist/shoelace.js\"' | write | update says: "On the first line, put this statement above whatever is already on the first line, save the file, then update it in Vim.

That's all I got, mostly saving this for future me who may need this!

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (1)

Collapse
 
pbnj profile image
Peter Benjamin (they/them)

Nice use of vim & :arg family.

This is also possible with GNU sed:

sed -i '1i import "../../../dist/shoelace.js"' ./**/*.test.ts
Enter fullscreen mode Exit fullscreen mode

Or with macOS/BSD-variant of sed:

sed -i'' '1i\
import "../../../dist/shoelace.js"
' ./**/*.test.ts
Enter fullscreen mode Exit fullscreen mode

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more