Home

Awesome

nvim-cmp Buffer Lines

Contents

Preview

Introduction

nvim-cmp Buffer Lines is a completion source for nvim-cmp that provides a source for all the lines in the current buffer. This is especially useful for C programmers. It uses tree-sitter if you have it installed on your system. tree-sitter is optional but recommended.

Installation

packer.nvim

require "packer".startup(function(use)
    use "amarakon/nvim-cmp-buffer-lines"
end)

Setup

require "cmp".setup {
    sources = {
        {
            name = "buffer-lines",
            option = { … }
        }
    }
}

Options

OptionTypeDefaultDescription
wordsBooleanfalseInclude words
commentsBooleanfalseInclude comments
line_numbersBooleanfalseInclude line numbers in the completion menu (does not apply on selection/confirmation)
line_number_separatorString" "The separator between the line number and the line text (only used if line_numbers is set.
leading_whitespaceBooleantrueInclude leading whitespace in the completion menu (does not apply on selection/confirmation)
max_indentsNumber0Maximum indentation level lines can be shown (0-indexed). For example, lines with one or more indents will not be shown when this is set to 1. Set to 0 to show an unlimited amount of indents.
max_sizeNumber100Maximum file size (in kB) for which this plugin will be activated

Command-line

You can use this source for searching for patterns in the command-line. I recommend using it in conjunction with cmp-buffer for a bread-and-butter combination. The following code block is the configuration I use and recommend.

-- Enable `buffer` and `buffer-lines` for `/` and `?` in the command-line
require "cmp".setup.cmdline({ "/", "?" }, {
    mapping = require "cmp".mapping.preset.cmdline(),
    sources = {
        {
            name = "buffer",
            option = { keyword_pattern = [[\k\+]] }
        },
        { name = "buffer-lines" }
    }
})

Only for certain file types

-- Only enable `buffer-lines` for C and C++
require "cmp".setup.filetype({ "c", "cpp" }, {
    sources = {
        { name = "buffer-lines" }
    }
})

TODO