Skip to content

Instantly share code, notes, and snippets.

@lowercasename
Created December 17, 2021 09:36
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 lowercasename/1c9c7c0f20b0a249ad223252587a170e to your computer and use it in GitHub Desktop.
Save lowercasename/1c9c7c0f20b0a249ad223252587a170e to your computer and use it in GitHub Desktop.
" RAPHAEL'S GOOD INIT.VIM
" Copyleft 2021: All Wrongs Reserved
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"
" Recommended tools:
" ripgrep, fzf, bat, lazygit
"
" PLUGINS
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
call plug#begin(stdpath('data') . '/plugged')
Plug 'neovim/nvim-lspconfig'
Plug 'hrsh7th/cmp-nvim-lsp'
Plug 'hrsh7th/cmp-buffer'
Plug 'hrsh7th/cmp-path'
Plug 'hrsh7th/cmp-cmdline'
Plug 'hrsh7th/nvim-cmp'
Plug 'hrsh7th/cmp-vsnip'
Plug 'hrsh7th/vim-vsnip'
Plug 'ray-x/lsp_signature.nvim'
Plug 'tpope/vim-fugitive'
Plug 'tpope/vim-commentary'
Plug 'tpope/vim-surround'
Plug 'nvim-lualine/lualine.nvim'
Plug 'kyazdani42/nvim-web-devicons'
Plug 'akinsho/bufferline.nvim'
Plug 'tmsvg/pear-tree'
Plug 'psliwka/vim-smoothie'
Plug 'airblade/vim-gitgutter'
Plug 'kyazdani42/nvim-tree.lua'
Plug 'junegunn/fzf', { 'do': { -> fzf#install() } }
Plug 'junegunn/fzf.vim'
Plug 'folke/which-key.nvim'
Plug 'moll/vim-bbye'
Plug 'preservim/vim-pencil'
Plug 'folke/tokyonight.nvim', { 'branch': 'main' }
call plug#end()
" PLUGIN CONFIGURATION
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" For nvim-cmp
set completeopt=menu,menuone,noselect
lua << EOF
-- Setup lspconfig.
vim.lsp.set_log_level("debug")
local capabilities = require('cmp_nvim_lsp').update_capabilities(vim.lsp.protocol.make_client_capabilities())
-- Setup lua-language-server
USER = vim.fn.expand('$USER')
local sumneko_root_path = "/home/" .. USER .. "/.config/nvim/lua-language-server"
local sumneko_binary = "/home/" .. USER .. "/.config/nvim/lua-language-server/bin/Linux/lua-language-server"
require'lspconfig'.sumneko_lua.setup {
cmd = {sumneko_binary, "-E", sumneko_root_path .. "/main.lua"};
settings = {
Lua = {
runtime = {
-- Tell the language server which version of Lua you're using (most likely LuaJIT in the case of Neovim)
version = 'LuaJIT',
-- Setup your lua path
path = runtime_path,
},
diagnostics = {
-- Get the language server to recognize the `vim` global
globals = {'vim'},
},
workspace = {
-- Make the server aware of Neovim runtime files
-- library = vim.api.nvim_get_runtime_file("", true),
library = "${3rd}/love2d/library",
checkThirdParty = false
},
-- Do not send telemetry data containing a randomized but unique identifier
telemetry = {
enable = false,
},
},
},
on_attach = function(client, bufnr)
require "lsp_signature".on_attach()
end,
}
-- Setup nvim-cmp.
local cmp = require'cmp'
cmp.setup({
snippet = {
-- REQUIRED - you must specify a snippet engine
expand = function(args)
vim.fn["vsnip#anonymous"](args.body) -- For `vsnip` users.
-- require('luasnip').lsp_expand(args.body) -- For `luasnip` users.
-- vim.fn["UltiSnips#Anon"](args.body) -- For `ultisnips` users.
-- require'snippy'.expand_snippet(args.body) -- For `snippy` users.
end,
},
mapping = {
['<C-b>'] = cmp.mapping(cmp.mapping.scroll_docs(-4), { 'i', 'c' }),
['<C-f>'] = cmp.mapping(cmp.mapping.scroll_docs(4), { 'i', 'c' }),
['<C-Space>'] = cmp.mapping(cmp.mapping.complete(), { 'i', 'c' }),
['<C-y>'] = cmp.config.disable, -- Specify `cmp.config.disable` if you want to remove the default `<C-y>` mapping.
['<C-e>'] = cmp.mapping({
i = cmp.mapping.abort(),
c = cmp.mapping.close(),
}),
['<CR>'] = cmp.mapping.confirm({ select = true }),
},
sources = cmp.config.sources({
{ name = 'nvim_lsp' },
{ name = 'vsnip' }, -- For vsnip users.
-- { name = 'luasnip' }, -- For luasnip users.
-- { name = 'ultisnips' }, -- For ultisnips users.
-- { name = 'snippy' }, -- For snippy users.
}, {
{ name = 'buffer' },
})
})
-- Use buffer source for `/` (if you enabled `native_menu`, this won't work anymore).
cmp.setup.cmdline('/', {
sources = {
{ name = 'buffer' }
}
})
-- Use cmdline & path source for ':' (if you enabled `native_menu`, this won't work anymore).
cmp.setup.cmdline(':', {
sources = cmp.config.sources({
{ name = 'path' }
}, {
{ name = 'cmdline' }
})
})
require'bufferline'.setup{
options = {
offsets = {
{
filetype = "NvimTree",
text = "File Explorer",
highlight = "Directory",
text_align = "left"
}
}
}
}
require'lualine'.setup()
require'which-key'.setup{}
require'nvim-tree'.setup()
EOF
" GENERAL CONFIGURATION
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Make plugins work
filetype plugin indent on
" Save swap files in a temp directory
set directory^=$HOME/.vim/tmp/
" Enable mouse
set mouse=a
" Autoreload changed files
set autoread
" Set leader to the comma key
let mapleader = ","
" 2. DISPLAY
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" General display options
set number
syntax on
" Color scheme and formatting
" TokyoNight theme settings
set background=dark
let g:tokyonight_style = "storm"
let g:tokyonight_italic_functions = 1
colorscheme tokyonight
" If needed, uncomment these lines to make colors
" work in Alacritty
" let &t_8f = "\<Esc>[38;2;%lu;%lu;%lum"
" let &t_8b = "\<Esc>[48:2;%lu;%lu;%lum"
" Enable cursorline in Insert mode
:autocmd InsertEnter * set cul
:autocmd InsertLeave * set nocul
" Change cursor in Insert mode
let &t_SI = "\e[5 q"
let &t_EI = "\e[2 q"
" Toggle background color (and therefore light/dark theme) with leader-t
if exists("*ToggleBackground") == 0
function ToggleBackground()
if &background == "dark"
set background=light
else
set background=dark
endif
endfunction
command BG call ToggleBackground()
endif
nnoremap <leader>t :BG<CR>
" TEXT ENTRY AND NAVIGATION
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Tab characters count for 8 spaces
" (This is a default, and is good for noticing sneaky tabs)
set tabstop=8
" Indent by 4 spaces
set shiftwidth=4
" Replaces tabs with spaces (Ctrl-V<TAB> to type actual tab)
set expandtab
" Turns on autoindent
set autoindent
" Usually tabs to the right location
set smartindent
" Print shiftwidth number of spaces when tabbing
set smarttab
" Make backspace work predictably
set backspace=indent,eol,start
" Consider hyphens as a part of inner words
" (+= adds a symbol, -= removes it)
set iskeyword+=-
" Shift lines up and down with Alt-J and Alt-K
nnoremap <A-j> :m .+1<CR>==
nnoremap <A-k> :m .-2<CR>==
inoremap <A-j> <Esc>:m .+1<CR>==gi
inoremap <A-k> <Esc>:m .-2<CR>==gi
vnoremap <A-j> :m '>+1<CR>gv=gv
vnoremap <A-k> :m '<-2<CR>gv=gv
" Keep visual mode on after indenting
vnoremap > >gv^
vnoremap < <gv^
" SEARCHING
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Allow case-insensitive and case-smart searches
set ignorecase
set smartcase
" Show number of matching search results in the statusline
set shortmess-=S
" Highlight search results
set hlsearch
" Clear search highlighting by hitting Return
nnoremap <CR> :noh<CR><CR>
" Show replacements live as you type
set inccommand=nosplit
" UI - BUFFERS, TABS, WINDOWS, PANES
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Enhanced tab-completion
set wildmenu
" Hide buffers instead of closing them on buffer switch
set hidden
" Buffer navigation shortcuts (requiring bufferline)
nnoremap <leader>b :BufferLinePick<CR>
nnoremap <PageUp> :BufferLineCyclePrev<CR>
nnoremap <PageDown> :BufferLineCycleNext<CR>
" Close buffers with leader-c
nnoremap <leader>c :Bdelete<CR>
" FZF
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
set rtp+=/usr/local/opt/fzf
" Open FZF with leader-F
nnoremap <leader>f :FZF<CR>
" RIPGREP
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Open ripgrep entries in new tab with C-t
augroup quickfix_tab | au!
au filetype qf nnoremap <buffer> <C-t> <C-w><CR><C-w>T
augroup END
" Open ripgrep with leader-R
nnoremap <leader>r :Rg
" SESSIONS
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Save and restore session
command Save :mksession! ~/.vimsession<CR>
command Load :source ~/.vimsession<CR>
" Save and load sessions with leader-s and leader-l
nnoremap <leader>s :Save<CR>
nnoremap <leader>l :Load<CR>
" PEAR-TREE
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Disable the dot-repeatable pair expansion feature
let g:pear_tree_repeatable_expand = 0
" MISCELLANEOUS SHORTCUTS
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Source .vimrc file
command Source :source ~/.config/nvim/init.vim
" Source .vimrc with leader-o
nnoremap <leader>o :Source<CR>
" Console log from Insert mode; puts focus inside parentheses
" The Ctrl-V prevents the bracket autocloser from autoclosing
imap cll console.log<C-v>();<Esc><S-f>(a
" Console log from visual mode on next line;
" puts visual selection inside parentheses
vmap cll yocll<Esc>p
" Console log from Normal mode, inserted on next line with
" word under cursor inside parentheses
nmap cll yiwocll<Esc>p
" FILETYPES
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Set the filetype based on the file's extension, but only if
" " 'filetype' has not already been set
au BufRead,BufNewFile *.twig set filetype=html
au BufRead,BufNewFile *.html.twig set filetype=html
" NVIMTREE
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Toggle NvimTree with leader-n
nnoremap <leader>n :NvimTreeToggle<CR>
" Hide the statusbar in NvimTree
au BufEnter,BufWinEnter,WinEnter,CmdwinEnter * if bufname('%') == "NvimTree" | set laststatus=0 | else | set laststatus=2 | endif
" VIM-PENCIL
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Toggle vim-pencil with leader-p
nnoremap <leader>p :PencilToggle<CR>
" Remap Q to reformat (reflow) paragraps in hard line break mode
nnoremap Q gqap
xnoremap Q gq
" Set custom vim-pencil indicators
let g:pencil#mode_indicators = {'hard': 'פֿ', 'auto': 'פֿ', 'soft': '﬋', 'off': '',}
" ZOTERO
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
function! ZoteroCite()
" pick a format based on the filetype (customize at will)
let format = &filetype =~ '.*tex' ? 'citep' : 'pandoc'
let api_call = 'http://127.0.0.1:23119/better-bibtex/cayw?format='.format.'&brackets=1'
let ref = system('curl -s '.shellescape(api_call))
return ref
endfunction
noremap <leader>z "=ZoteroCite()<CR>p
inoremap <C-z> <C-r>=ZoteroCite()<CR>
" VSNIP
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Expand
imap <expr> <C-j> vsnip#expandable() ? '<Plug>(vsnip-expand)' : '<C-j>'
smap <expr> <C-j> vsnip#expandable() ? '<Plug>(vsnip-expand)' : '<C-j>'
" Expand or jump
imap <expr> <C-l> vsnip#available(1) ? '<Plug>(vsnip-expand-or-jump)' : '<C-l>'
smap <expr> <C-l> vsnip#available(1) ? '<Plug>(vsnip-expand-or-jump)' : '<C-l>'
" Jump forward or backward
imap <expr> <Tab> vsnip#jumpable(1) ? '<Plug>(vsnip-jump-next)' : '<Tab>'
smap <expr> <Tab> vsnip#jumpable(1) ? '<Plug>(vsnip-jump-next)' : '<Tab>'
imap <expr> <S-Tab> vsnip#jumpable(-1) ? '<Plug>(vsnip-jump-prev)' : '<S-Tab>'
smap <expr> <S-Tab> vsnip#jumpable(-1) ? '<Plug>(vsnip-jump-prev)' : '<S-Tab>'
" Select or cut text to use as $TM_SELECTED_TEXT in the next snippet.
" See https://github.com/hrsh7th/vim-vsnip/pull/50
nmap s <Plug>(vsnip-select-text)
xmap s <Plug>(vsnip-select-text)
nmap S <Plug>(vsnip-cut-text)
xmap S <Plug>(vsnip-cut-text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment