Skip to content

Instantly share code, notes, and snippets.

@qhwa
Last active July 30, 2022 23:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qhwa/9032316910012aef57186f9682036bf4 to your computer and use it in GitHub Desktop.
Save qhwa/9032316910012aef57186f9682036bf4 to your computer and use it in GitHub Desktop.
Neovim config
-- file: lua/init.lua
local execute = vim.api.nvim_command
local fn = vim.fn
local fmt = string.format
local pack_path = fn.stdpath("data") .. "/site/pack"
-- ensure a given plugin from github.com/<user>/<repo> is cloned in the pack/packer/start directory
local function ensure (user, repo)
local install_path = fmt("%s/packer/start/%s", pack_path, repo)
if fn.empty(fn.glob(install_path)) > 0 then
execute(fmt("!git clone https://github.com/%s/%s %s", user, repo, install_path))
execute(fmt("packadd %s", repo))
end
end
-- ensure the plugin manager is installed
ensure("wbthomason", "packer.nvim")
require('packer').startup(function(use)
-- install all the plugins you need here
-- the plugin manager can manage itself
use {'wbthomason/packer.nvim'}
-- lsp config for elixir-ls support
use {'neovim/nvim-lspconfig'}
-- cmp framework for auto-completion support
use {'hrsh7th/nvim-cmp'}
-- install different completion source
use {'hrsh7th/cmp-nvim-lsp'}
use {'hrsh7th/cmp-buffer'}
use {'hrsh7th/cmp-path'}
use {'hrsh7th/cmp-cmdline'}
-- you need a snippet engine for snippet support
-- here I'm using vsnip which can load snippets in vscode format
use {'hrsh7th/vim-vsnip'}
use {'hrsh7th/cmp-vsnip'}
use {'rafamadriz/friendly-snippets'}
-- treesitter for syntax highlighting and more
use {'elixir-editors/vim-elixir'}
use { 'kyazdani42/nvim-web-devicons' }
use {
'ibhagwan/fzf-lua',
-- optional for icon support
requires = { 'kyazdani42/nvim-web-devicons' }
}
use { 'scrooloose/nerdtree' }
use { 'Lokaltog/vim-powerline' }
use { 'mhinz/vim-mix-format' }
use { 'dyng/ctrlsf.vim' }
end)
-- `on_attach` callback will be called after a language server
-- instance has been attached to an open buffer with matching filetype
-- here we're setting key mappings for hover documentation, goto definitions, goto references, etc
-- you may set those key mappings based on your own preference
local on_attach = function(client, bufnr)
local opts = { noremap=true, silent=true }
vim.api.nvim_buf_set_keymap(bufnr, 'n', 'gd', '<cmd>lua vim.lsp.buf.definition()<CR>', opts)
vim.api.nvim_buf_set_keymap(bufnr, 'n', 'gr', '<cmd>lua vim.lsp.buf.references()<CR>', opts)
vim.api.nvim_buf_set_keymap(bufnr, 'n', 'gD', '<cmd>lua vim.lsp.buf.declaration()<CR>', opts)
vim.api.nvim_buf_set_keymap(bufnr, 'n', 'gi', '<cmd>lua vim.lsp.buf.implementation()<CR>', opts)
vim.api.nvim_buf_set_keymap(bufnr, 'n', 'K', '<cmd>lua vim.lsp.buf.hover()<CR>', opts)
vim.api.nvim_buf_set_keymap(bufnr, 'n', '<C-k>', '<cmd>lua vim.lsp.buf.signature_help()<CR>', opts)
vim.api.nvim_buf_set_keymap(bufnr, 'n', '<leader>cr', '<cmd>lua vim.lsp.buf.rename()<CR>', opts)
vim.api.nvim_buf_set_keymap(bufnr, 'n', '<leader>ca', '<cmd>lua vim.lsp.buf.code_action()<CR>', opts)
vim.api.nvim_buf_set_keymap(bufnr, 'n', '<leader>cf', '<cmd>lua vim.lsp.buf.formatting()<CR>', opts)
vim.api.nvim_buf_set_keymap(bufnr, 'n', '<leader>cd', '<cmd>lua vim.diagnostic.open_float()<CR>', opts)
vim.api.nvim_buf_set_keymap(bufnr, 'n', '[d', '<cmd>lua vim.diagnostic.goto_prev()<CR>', opts)
vim.api.nvim_buf_set_keymap(bufnr, 'n', ']d', '<cmd>lua vim.diagnostic.goto_next()<CR>', opts)
end
local capabilities = require('cmp_nvim_lsp').update_capabilities(vim.lsp.protocol.make_client_capabilities())
require('lspconfig').elixirls.setup {
cmd = { "/home/qhwa/tools/elixir-ls/release/language_server.sh" },
on_attach = on_attach,
capabilities = capabilities
}
local cmp = require('cmp')
cmp.setup({
snippet = {
expand = function(args)
-- setting up snippet engine
-- this is for vsnip, if you're using other
-- snippet engine, please refer to the `nvim-cmp` guide
vim.fn["vsnip#anonymous"](args.body)
end,
},
mapping = {
["<Tab>"] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_next_item()
elseif vim.fn["vsnip#available"](1) == 1 then
feedkey("<Plug>(vsnip-expand-or-jump)", "")
elseif has_words_before() then
cmp.complete()
else
fallback() -- The fallback function sends a already mapped key. In this case, it's probably `<Tab>`.
end
end, { "i", "s" }),
["<S-Tab>"] = cmp.mapping(function()
if cmp.visible() then
cmp.select_prev_item()
elseif vim.fn["vsnip#jumpable"](-1) == 1 then
feedkey("<Plug>(vsnip-jump-prev)", "")
end
end, { "i", "s" }),
['<CR>'] = cmp.mapping.confirm({ select = true }),
},
sources = cmp.config.sources({
{ name = 'nvim_lsp' },
{ name = 'vsnip' }, -- For vsnip users.
{ name = 'buffer' }
})
})
-- helper functions
local has_words_before = function()
local line, col = unpack(vim.api.nvim_win_get_cursor(0))
return col ~= 0 and vim.api.nvim_buf_get_lines(0, line - 1, line, true)[1]:sub(col, col):match("%s") == nil
end
local feedkey = function(key, mode)
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes(key, true, true, true), mode, true)
end
cmp.setup({
-- other settings ...
mapping = {
-- other mappings ...
["<Tab>"] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_next_item()
elseif vim.fn["vsnip#available"](1) == 1 then
feedkey("<Plug>(vsnip-expand-or-jump)", "")
elseif has_words_before() then
cmp.complete()
else
fallback()
end
end, { "i", "s" }),
["<S-Tab>"] = cmp.mapping(function()
if cmp.visible() then
cmp.select_prev_item()
elseif vim.fn["vsnip#jumpable"](-1) == 1 then
feedkey("<Plug>(vsnip-jump-prev)", "")
end
end, { "i", "s" })
}
})
:set nocompatible
:set number
:set noeb
:set confirm
:set autoindent cindent
:set tabstop=2 softtabstop=2 shiftwidth=2 expandtab smarttab
:set history=1000
:set nobackup noswapfile
:set ignorecase smartcase hlsearch incsearch
:set gdefault
:set enc=utf-8
:set fencs=utf-8,ucs-bom,shift-jis,gb18030,gbk,gb2312,cp936
:set langmenu=zh_CN.UTF-8 helplang=cn
:set ruler
" 显示空白字符
" 方便团队协作时使用规范的代码间隔
:set listchars=tab:>-,trail:~,extends:>,precedes:<
:set list lcs=trail:·,tab:»·
:set viminfo+=!
:set mouse=a
:set selection=exclusive
:set selectmode=mouse,key
:set report=0
:set shortmess=atl
:set showmatch
:set matchtime=5
:set scrolloff=3
:let mapleader=","
:colorscheme tutticolori
lua require('init')
imap jk <Esc>
inoremap <leader>d <ESC>dd
let g:NERDTreeWinPos = "right"
" Plugin: NERDTree
noremap <F2> :silent NERDTreeToggle \| :silent NERDTreeMirror<CR>
" ZF plugin
nnoremap <leader><space> :FzfLua files<CR>
" key-mapping for CtrlSF plugin
nnoremap <silent> <leader>f :CtrlSF<CR>
let g:mix_format_on_save = 1
au BufRead,BufNewFile *.ex,*.exs set filetype=elixir
au BufRead,BufNewFile *.eex,*.heex,*.leex,*.sface,*.lexs set filetype=eelixir
au BufRead,BufNewFile mix.lock set filetype=elixir
au FileType elixir let $MIX_ENV = 'vim'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment