0

I am using vim for both programming and competitive coding.

Now, since I want to insert specific macros whenever I open a source file when I am doing competitive coding. I just copy paste the entire macros into the vim editor manually from the place where I saved them.

Is there any way I can do this more efficiently in vim editor, like say I press CCand the macros get inserted at the top of the source file automatically ?

If yes, then what changes do I need into the .vimrc file to do so ?

3

There are various solutions to this explained in an existing post:

https://stackoverflow.com/questions/690386/writing-a-vim-function-to-insert-a-block-of-static-text

1

If you mean inserting boilerplate code or reusable code fragments, you can define mappings, insert mode abbreviations, or use one of the more powerful snippets plugins.

snippets are like the built-in :abbreviate on steroids, usually with parameter insertions, mirroring, and multiple stops inside them. One of the first, very famous (and still widely used) Vim plugins is snipMate (inspired by the TextMate editor); unfortunately, it's not maintained any more; though there is a fork. A modern alternative (that requires Python though) is UltiSnips. There are more, see this list on the Vim Tips Wiki.

There are also plugins that automatically populate an empty file with a template.


Macros usually mean keystrokes / command sequences recorded in a register, or mappings derived from that. If you want them only for your competitive coding sessions, you can put those into a separate macros.vim script, and :source that on demand (vs. putting them directly into ~/.vimrc). Even sourcing can be automated:

Local config with built-in functionality

If you always start Vim from the project root directory, the built-in

:set exrc

enables the reading of a .vimrc file from the current directory.

Local config through plugin

Otherwise, you need the help of a plugin; there are several on vim.org; I can recommend the localrc plugin, which even allows local filetype-specific configuration.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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