Skip to content
Related Articles

Related Articles

Improve Article
Creating a C++ template in vim in Linux
  • Difficulty Level : Easy
  • Last Updated : 25 Jun, 2020

Vim allows users to create specific templates for files ending with certain extensions.

General steps to create a template:

step 1: Create a template in ~/.vim/templates/ directory.A template is a skeleton content that can be fit into all the files ending with a certain specific extension.
step 2: Add commands to ~/.vimrc file that directs vim to populate the new file with contents of the specified template.

For example:

  :autocmd BufNewFile  *.c 0r ~/vim/skeleton.c 

Explanation:
– autocmd stands for Auto command.
– BufNewFile is an event that denotes opening of a new file in the buffer.
– *.c denotes all the files with extensions `.c`.
– 0r denotes read a file and insert its content on top of the new file with extension `.c`. This is followed by a path to the skeleton file.

Steps to create a c++ template:

Step 1: Open the terminal.
Step 2: Change directory to ~/.vim/templates/.



$ cd ~/.vim/templates/

Note: If templates subdirectory is not available then create one in .vim folder.

$ mkdir templates && cd templates

Step 3: Open template file.

$ sudo vim skeleton.cpp

Example:

Step 4: Add skeleton/template. Save and close.
Example:

Step 5: Open ~/.vimrc file.

$ sudo vim  ~/.vimrc

Step 6: Add the following line to it.

:autocmd BufNewFile *.cpp 0r ~/.vim/templates/skeleton.cpp

Example:

Step 7: Save and close.

Opening a new cpp file

Example:

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.
My Personal Notes arrow_drop_up
Recommended Articles
Page :