Async insert templates into file.
-- with lazy.nvim
{'glepnir/template.nvim', cmd = {'Template','TemProject'}, config = function()
require('template').setup({
-- config in there
})
end}
-- lazy load you can use cmd or ft. if you are using cmd to lazyload when you edit the template file
-- you may see some diagnostics in template file. use ft to lazy load the diagnostic not display
-- when you edit the template file.
{
temp_dir -- template directory
author -- your name
email -- email address
project -- template project generates
}
{
project = {
['test'] = {
['lang'] = {
['cpp'] = {
['Tests'] = {
'CMakeLists.txt',
},
},
},
},
['other'] =
{
['default'] =
{
'README.md',
'.gitignore',
},
['lang'] =
{
['lua'] =
{
'.stylua.toml',
},
['cpp'] =
{
'.clang-format',
'main.cpp',
'CMakeLists.txt',
},
['python'] =
{
'requirements.txt',
},
},
},
},
}
TemProject cpp
will generates project for you.
.
├── .clang-format
├── .gitignore
├── CMakeLists.txt
├── README.md
├── Tests
│ └── CMakeLists.txt
└── main.cpp
-
{{_date_}}
insert current date -
{{_cursor_}}
set cursor here -
{{_file_name_}}
current file name -
{{_author_}}
author info -
{{_email_}}
email adrress -
{{_variable_}}
variable name -
{{_upper_file_}}
all-caps file name -
{{_lua:vim.fn.expand(%:.:r)_}}
set by lua script
You need config the temp_dir
first like temp.temp_dir = '~/.config/nvim/template
then create the
a template named main_owner.go
for go language in the temp_dir
// Copyright {{_date_}} {{_author_}}. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package {{_file_name_}}
func main() {
{{_cursor_}}
}
You can use lua script to make template with {{_lua:}}. For example
---
created: {{_lua:os.date("%y/%m/%d %H/%M")_}}
---
above template generates bellow lines.
---
created: 2022/12/29 21:52
---
- Work with exist file
if there has a file main.go
, and open it input Template <Tab>
. select the template main_owner
It will insert template to this file like
// Copyright 2022-07-05 21:05:36 glephunter. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
func main() {
| -- cursor in there
}
- Work with not exist file
use Template test.go <TAB>
, it will create a file named test.go
in current path and auto open
this file insert template.
- Work with not exist file and custom variable
a lua template file named nvim_temp.lua
, content is
local api,fn = vim.api,vim.fn
local {{_variable_}}
{{_cursor_}}
return {{_variable_}}
use Template test.lua <TAB>
then it will auto fill template name nvim_temp
if there
only has one lua template file. if there has _variable_
set then it will pop up an input
then input your variable name.
local api,fn = vim.api,vim.fn
local template
| -- cursor here
return template
- Config a fancy keymap
vim.keymap.set('n', '<Leader>t', function()
return ':Template '
end, { remap = true})
- Find all templates
template.nvim use telescope
. so you need register template telescope extension to telescope
require("telescope").load_extension('find_template')
- Use Telescope to create template or insert template
-- This command will create a template file then show all templates
Telescope find_template name=templatename
-- when you select a template file it will insert this tempalte into current buffer
Telecope find_template type=insert
Then you can use Telescope find_template
to check all templates
If you'd like to support my work financially, buy me a drink through paypal