- Generates print statements for:
- line numbers
- function names
- variable values
- Syntax and context aware
- Automatic format specifier detection
- Customizable (see the Examples section for inspiration)
- Neovim >= 0.8.0
- A tree-sitter parser for C/C++
- The clangd language server
{
'declancm/printf.nvim'
}
require('printf').print_var() -- Print the variable under the cursor
require('printf').print_line() -- Print the current line number
require('printf').print_func() -- Print the name of the enclosed function
require('printf').clean() -- Cleanup generated statements
The Printf
command can be used to access the Lua API as Vim commands
:Printf print_var " equivalent to `:lua require('printf').print_var()`
:Printf print_line " equivalent to `:lua require('printf').print_line()`
:Printf print_func " equivalent to `:lua require('printf').print_func()`
:Printf clean " equivalent to `:lua require('printf').clean()`
vim.keymap.set('n', '<leader>dv', require('printf').print_var, {})
vim.keymap.set('n', '<leader>dl', require('printf').print_line, {})
vim.keymap.set('n', '<leader>df', require('printf').print_func, {})
vim.keymap.set('n', '<leader>dc', require('printf').clean, {})
nnoremap <leader>dv <cmd>Printf print_var<cr>
nnoremap <leader>dl <cmd>Printf print_line<cr>
nnoremap <leader>df <cmd>Printf print_func<cr>
nnoremap <leader>dc <cmd>Printf clean<cr>
Help docs can be accessed with :help printf.nvim
require('printf').setup({
-- Generated function call options
called_function = {
-- Name of the called function
name = 'printf',
-- Add additional arguments before the format string
additional_args = {},
},
-- 'print_var' specific options
print_var = {
-- Automatically dereference supported pointer types
dereference_pointers = false,
-- Format char * variables as strings
char_ptr_strings = true,
},
-- 'print_line' specific options
print_line = {
-- The variable/identifier/macro with the line number integer value
variable = '__LINE__',
},
-- 'print_func' specific options
print_func = {
-- The variable/identifier/macro with the function name string
variable = '__func__',
},
})
-- Install plugins
require('lazy').setup({
'neovim/nvim-lspconfig',
'declancm/printf.nvim'
})
-- Setup the clangd language server
require('lspconfig').clangd.setup({})
-- Setup printf
require('printf').setup()
Config:
require('printf').setup({
called_function = {
name = 'fprintf',
additional_args = { 'stderr' }
}
})
Output:
fprintf(stderr, "example: %d\n", example); // auto-generated printf
Config:
require('printf').setup({
print_func = { variable = '__PRETTY_FUNCTION__' }
})
Output:
printf("function: %s\n", __PRETTY_FUNCTION__); // auto-generated printf