Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have about ninety configuration files that I need to add an almost identical block of additional code to, with only two instances of a single word differing each time. I would very much like to repeat as little effort as possible.

Is it possible, in VIM, to write a function that generates a block of text depending on an argument and either feeds it into the buffer or pastes it after the current cursor position? And if so, what documentation should I be looking into?

share|improve this question

2 Answers 2

up vote 2 down vote accepted

I like snipMate pretty much, it can be used to, for example, write newconf, press Tab which expands newconf to some specified template and places the caret in one position (and in the next ones by subsequent Tab presses). Hart to explain, apparently this video explains it (I guess, no plugin here). Not sure if it's the best solution, but on the whole it's quite handy.

Maybe sed, patch or even Coccinelle ("semantic patching") might help, too.

share|improve this answer
    
Absolutely perfect for what I need. Thanks! –  Shadur Oct 28 '11 at 15:26
    
i think snipMate adds some nice functionality over what vim alone can do. Thanks for the tip. –  Tim Kennedy Oct 28 '11 at 16:22

You can do this directly from within VIM, by customizing your .vimrc file, too, using abbreviations.

in .vimrc:

:ab _mytxt ################################################################################^M# This is a comment^Mkey1=value1^Mkey2=value2^M#^Mecho "Hello, World!"^M################################################################################

in vi, in insert mode, just type _mytxt, and vim will automatically replace it with the unabbreviated text.

_mytxt

becomes

################################################################################
# This is a comment
key1=value1
key2=value2
#
echo "Hello, World!"
################################################################################

Just read up on the .vimrc file, and customizing it. You can also look at customations for the .exrc file, which are often usable by VIM.

share|improve this answer
    
Also interesting, but I think snipMate's closer to what I need. –  Shadur Oct 28 '11 at 17:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.