Recent Posts

knowledge is power

Menu

Widgets

Search

Create templates for Vim

Developers should follow not only the rules and processes of quality assurance. They must also have advantages for motivation from QA! As QA, you can already achieve more with less. Templates would be a possibility. This guide shows you how to deploy templates for Vim.

Preparation

In order to provide templates, check if the personal vimrc file exists. Do not use the global vimrc! Whatever you configure in the personal vimrc file will overrule any setting in the global vimrc file.

Shell

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

# start vim

$vim

# the global vimrc file can found by

:echo$VIM

# the home directory can found by

:echo$HOME

# the personal vimrc can found by

:echo$MYVIMRC

# exit vim

:q!

# if no personal vimrc exists

$touch/home/<user>/.vimrc

# create folder for templates

$mkdir/home/<user>/templates

Create some templates

Now place some files with in the templates directory.

html.tpl

XHTML

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

<!DOCTYPE html>

<html lang="">

<head>

<title></title>

<meta charset="UTF-8">

<meta name="description"content="">

<meta name="author"content="">

<meta name="keywords"content="">

<meta name="robots"content="noindex,follow" />

<meta name="viewport"content="width=device-width, initial-scale=1">

<link href=""type="text/css"rel="stylesheet" />

</head>

<body>

<!-- Content goes here... -->

</body>

</html>

py.tpl

Python

1

2

3

4

5

6

7

8

9

10

11

12

13

#!/usr/bin/env python

# -*- coding: utf-8 -*-

"""Missing docstring"""

classLorem(object):

"""Missing docstring"""

def__init__(self):

"""Missing docstring"""

if__name__=='__main__':

and so on… . Open the personal vimrc file and add the following auto-command.

Shell

1

2

3

4

5

6

7

8

9

10

11

12

13

14

# open personal vimrc

$vim/home/<user>/.vimrc

# change into insert mode

i

# add command

autocmd BufNewFile*silent!0r$HOME/templates/%:e.tpl

# exit insert mode

<ESC>

# store change and close

:x

Whenever a Developer create now a new file, Vim looks for a template that matches the extension of the file. If there is no template for the file type, then Vim simply creates an empty file as usual.