Skip to content

Instantly share code, notes, and snippets.

@vramana
Last active November 8, 2023 02:36
Show Gist options
  • Save vramana/d312dc94370e004a076b7824b0e63729 to your computer and use it in GitHub Desktop.
Save vramana/d312dc94370e004a076b7824b0e63729 to your computer and use it in GitHub Desktop.
My vim config
if (has("termguicolors"))
set termguicolors
endif
set guifont=monospace:h12
let g:loaded_ruby_provider = 0
let g:loaded_node_provider = 0
let g:loaded_perl_provider = 0
call plug#begin('~/.config/nvim/plugged')
Plug 'folke/tokyonight.nvim', { 'branch': 'main' }
Plug 'tpope/vim-abolish'
Plug 'windwp/nvim-ts-autotag'
Plug 'machakann/vim-highlightedyank'
Plug 'rest-nvim/rest.nvim'
Plug 'numToStr/Comment.nvim'
Plug 'tpope/vim-repeat'
Plug 'tikhomirov/vim-glsl'
Plug 'tpope/vim-surround'
Plug 'jose-elias-alvarez/typescript.nvim'
Plug 'nvim-lualine/lualine.nvim'
Plug 'hrsh7th/cmp-nvim-lsp'
Plug 'hrsh7th/cmp-buffer'
Plug 'hrsh7th/cmp-path'
Plug 'hrsh7th/cmp-cmdline'
Plug 'hrsh7th/nvim-cmp'
Plug 'github/copilot.vim'
Plug 'junegunn/fzf', { 'dir': '~/.fzf', 'do': './install --all' }
Plug 'junegunn/fzf.vim'
Plug 'rust-lang/rust.vim'
" Plug 'ziglang/zig.vim'
Plug 'nvim-treesitter/nvim-treesitter'
Plug 'nvim-treesitter/nvim-treesitter-context'
Plug 'neovim/nvim-lspconfig'
Plug 'williamboman/mason.nvim'
Plug 'williamboman/mason-lspconfig.nvim'
Plug 'folke/trouble.nvim'
Plug 'nvim-lua/plenary.nvim'
Plug 'nvim-pack/nvim-spectre'
" Plug 'ThePrimeagen/harpoon'
call plug#end()
let g:hardtime_default_on = 1
command! -bang -nargs=* Rg call fzf#vim#grep('rg --column --line-number --no-heading --fixed-strings --color "always" '.shellescape(<q-args>), 1, <bang>0)
" let g:ale_linters = {
" \ 'javascript': ['eslint'],
" \ 'sql': ['sql-lint'],
" \}
" Theme
syntax enable
colorscheme tokyonight
set confirm
set relativenumber number
set directory=~/.vimswap
" Currently needed for neovim paste issue
set pastetoggle=<f6>
set nopaste
" Let airline tell me my status
set noshowmode
filetype on
set number
set tabstop=2 shiftwidth=2 expandtab
set hidden
set nowrap
set clipboard+=unnamedplus
set inccommand=split
" For vim-go, Autosaves file upon :GoBuild
set autowrite
set undodir=~/.vimundo
set undolevels=1000 " How many undos
set undofile
set undoreload=10000 " number of lines to save for undo
set foldmethod=indent
set foldlevel=100
augroup ag_trim_trailing_space
autocmd!
autocmd BufWritePre * %s/\s\+$//e
augroup END
" AutoRead
augroup ag_auto_read
autocmd!
au FocusGained,BufEnter * checktime
autocmd BufLeave,FocusLost *.* :up
autocmd BufLeave,FocusLost *.rs :up
augroup END
" Key board mappings
nnoremap <C-h> <C-w>h
nnoremap <C-j> <C-w>j
nnoremap <C-k> <C-w>k
nnoremap <C-l> <C-w>l
inoremap <C-h> <C-w>h
inoremap <C-j> <C-w>j
inoremap <C-k> <C-w>k
inoremap <C-l> <C-w>l
" Does not play well with FZF popup
tnoremap <F3> <C-\><C-n>
inoremap jj <Esc>
nnoremap <C-Tab> za
nnoremap <F2> :noh<CR>
nnoremap <Leader>nh :noh<CR>
nmap Q q
nnoremap K <nop>
let NERDTreeShowHidden=1
" set foldmethod=indent
let g:auto_save = 1 " enable AutoSave on Vim startup
let mapleader=" "
nnoremap <Leader>bb :Buffers<CR>
nnoremap <Leader>bd :bp\|bd #<CR>
nnoremap <Leader>qq :qall<CR>
nnoremap <Leader>qc :qall!<CR>
nnoremap <Leader>wv :vsplit<CR>
nnoremap <Leader>wd :close<CR>
nnoremap <Leader>fs :up<CR>
nnoremap <Leader>fm :NERDTreeToggle<CR>
nnoremap <Leader>pf :FZF<CR>
nnoremap <Leader>fed :e ~/.config/nvim/init.vim<CR>
nnoremap <Leader>so :so ~/.config/nvim/init.vim<CR>
nnoremap <Leader>/ :Rg<Space>
nnoremap <Leader>en :lua vim.diagnostic.goto_next({ severity = vim.diagnostic.severity.ERROR })<CR>
nnoremap <Leader>bh :lua vim.lsp.buf.hover()<CR>
nnoremap <leader>xx <cmd>TroubleToggle<cr>
nnoremap <leader>xw <cmd>TroubleToggle workspace_diagnostics<cr>
nnoremap <leader>xd <cmd>TroubleToggle document_diagnostics<cr>
nnoremap <leader>xq <cmd>TroubleToggle quickfix<cr>
nnoremap <leader>xl <cmd>TroubleToggle loclist<cr>
nnoremap ,rr <Plug>RestNvim<CR>
nnoremap <Leader><Leader>x :source %<CR>
let g:rustfmt_autosave = 1
" let g:rustfmt_options = '--edition 2021'
" https://stackoverflow.com/a/4294176
function s:MkNonExDir(file, buf)
if empty(getbufvar(a:buf, '&buftype')) && a:file!~#'\v^\w+\:\/'
let dir=fnamemodify(a:file, ':h')
if !isdirectory(dir)
call mkdir(dir, 'p')
endif
endif
endfunction
augroup BWCCreateDir
autocmd!
autocmd BufWritePre * :call s:MkNonExDir(expand('<afile>'), +expand('<abuf>'))
augroup END
command! -nargs=1 AddExt execute "saveas ".expand("%:p").<q-args>
function s:ChgExt(ext)
let l:curfile = expand("%:p")
let l:newfile = expand("%:r") . a:ext
echo l:newfile
let v:errmsg = ""
silent! exe "saveas!" . " " . l:newfile
if expand("%:p") !=# l:curfile && filewritable(expand("%:p"))
silent exe "bwipe! " . l:curfile
if delete(l:curfile)
echoerr "Could not delete " . l:curfile
endif
endif
endfunction
command! -nargs=1 ChgExt :call s:ChgExt(<q-args>)
lua <<EOF
vim.lsp.set_log_level("debug")
require('Comment').setup()
require('rest-nvim').setup()
require'nvim-treesitter.configs'.setup {
autotag = {
enable = true,
},
highlight = {
enable = true,
},
ensure_installed = {
"http",
"json",
"tsx",
"html",
"css"
}
}
require'treesitter-context'.setup({
enable = true, -- Enable this plugin (Can be enabled/disabled later via commands)
max_lines = 0, -- How many lines the window should span. Values <= 0 mean no limit.
patterns = { -- Match patterns for TS nodes. These get wrapped to match at word boundaries.
-- For all filetypes
-- Note that setting an entry here replaces all other patterns for this entry.
-- By setting the 'default' entry below, you can control which nodes you want to
-- appear in the context window.
default = {
'class',
'function',
'method',
-- 'for', -- These won't appear in the context
-- 'while',
-- 'if',
-- 'switch',
-- 'case',
},
-- Example for a specific filetype.
-- If a pattern is missing, *open a PR* so everyone can benefit.
-- rust = {
-- 'impl_item',
-- },
},
exact_patterns = {
-- Example for a specific filetype with Lua patterns
-- Treat patterns.rust as a Lua pattern (i.e "^impl_item$" will
-- exactly match "impl_item" only)
-- rust = true,
},
-- [!] The options below are exposed but shouldn't require your attention,
-- you can safely ignore them.
zindex = 20, -- The Z-index of the context window
})
local parser_config = require "nvim-treesitter.parsers".get_parser_configs()
parser_config.liquid = {
install_info = {
url = "https://github.com/Shopify/tree-sitter-liquid.git", -- local path or git repo
files = {"src/parser.c"}, -- note that some parsers also require src/scanner.c or src/scanner.cc
-- optional entries:
branch = "main", -- default branch in case of git repo if different from master
generate_requires_npm = false, -- if stand-alone parser without npm dependencies
requires_generate_from_grammar = false, -- if folder contains pre-generated src/parser.c
},
filetype = "liquid", -- if filetype does not match the parser name
}
require('lualine').setup({
options = {
icons_enabled = true,
theme = 'tokyonight',
}
})
-- Setup nvim-cmp.
local cmp = require'cmp'
cmp.setup({
window = {
-- completion = cmp.config.window.bordered(),
-- documentation = cmp.config.window.bordered(),
},
completion = {
completeopt = 'menu,menuone,noinsert'
},
mapping = cmp.mapping.preset.insert({
['<C-b>'] = cmp.mapping.scroll_docs(-4),
['<C-f>'] = cmp.mapping.scroll_docs(4),
['<C-Space>'] = cmp.mapping.complete(),
['<C-e>'] = cmp.mapping.abort(),
['<CR>'] = cmp.mapping.confirm({ select = true }), -- Accept currently selected item. Set `select` to `false` to only confirm explicitly selected items.
}),
sources = cmp.config.sources({
{ name = 'nvim_lsp' },
-- { name = 'vsnip' }, -- For vsnip users.
-- { name = 'luasnip' }, -- For luasnip users.
-- { name = 'snippy' }, -- For snippy users.
}, {
{ name = 'buffer' },
})
})
require("mason").setup()
require("mason-lspconfig").setup()
local lspconfig = require("lspconfig")
local eslint = {
lintCommand = "eslint_d -f unix --stdin --stdin-filename ${INPUT}",
lintStdin = true,
lintFormats = {"%f:%l:%c: %m"},
lintIgnoreExitCode = true,
}
local prettier_fmt = {
formatCommand = 'prettierd "${INPUT}"',
formatStdin = true,
}
local terraform_fmt = { formatCommand = 'terraform fmt -', formatStdin = true }
lspconfig.efm.setup {
cmd = { "/opt/homebrew/bin/efm-langserver", "-logfile", "/Users/ramana/.local/state/nvim/efm.log", "-loglevel", "10" },
init_options = {documentFormatting = true},
on_attach = function(client, bufnr)
vim.api.nvim_clear_autocmds({ group = augroup, buffer = bufnr })
vim.api.nvim_create_autocmd("BufWritePre", {
group = augroup,
buffer = bufnr,
callback = function()
vim.lsp.buf.format()
end,
})
end,
settings = {
rootMarkers = {".git/", "package.json"},
languages = {
liquid = {prettier_fmt},
markdown = {prettier_fmt},
css = {prettier_fmt},
html = {prettier_fmt},
javascript = {prettier_fmt, eslint},
javascriptreact = {prettier_fmt, eslint},
["javascript.jsx"] = {prettier_fmt, eslint},
typescript = {prettier_fmt, eslint},
["typescript.tsx"] = {prettier_fmt, eslint},
typescriptreact = {eslint},
json = {prettier_fmt},
terraform = {terraform_fmt},
}
},
filetypes = {
"markdown",
"css",
"liquid",
"html",
"json",
"javascript",
"javascriptreact",
"javascript.jsx",
"typescript",
"typescript.tsx",
"typescriptreact",
"terraform",
},
}
lspconfig.rust_analyzer.setup({
on_attach=function(client)
require'completion'.on_attach(client)
end,
settings = {
["rust-analyzer"] = {
imports = {
granularity = {
group = "module",
},
prefix = "self",
},
cargo = {
buildScripts = {
enable = true,
},
},
procMacro = {
enable = true
},
}
}
})
lspconfig.ltex.setup({})
require('typescript').setup {
server = {
on_attach = function(client)
if client.config.flags then
client.config.flags.allow_incremental_sync = true
end
client.server_capabilities.documentFormattingProvider = false
local bufopts = { noremap=true, silent=true, buffer=bufnr }
vim.keymap.set('n', 'K', vim.lsp.buf.hover, bufopts)
vim.keymap.set('n', 'gd', vim.lsp.buf.definition, bufopts)
vim.keymap.set('n', 'gi', vim.lsp.buf.implementation, bufopts)
vim.keymap.set('n', 'gr', vim.lsp.buf.references, bufopts)
vim.keymap.set('n', 'gD', vim.lsp.buf.declaration, bufopts)
vim.keymap.set('n', '<Leader>K', vim.lsp.buf.signature_help, bufopts)
vim.keymap.set('n', 'gt', vim.lsp.buf.type_definition, bufopts)
vim.keymap.set('n', '<Leader>rn', vim.lsp.buf.rename, bufopts)
vim.keymap.set('n', '<Leader>ca', vim.lsp.buf.code_action, bufopts)
vim.keymap.set('n', '<Leader>e', vim.diagnostic.open_float, opts)
end
}
}
require("trouble").setup {
-- your configuration comes here
-- or leave it empty to use the default settings
-- refer to the configuration section below
icons = false
}
EOF
nnoremap ,s :term yarn start<CR>
nnoremap ,t :term yarn cypress:open<CR>
nnoremap ,e :term emacs<CR>
nnoremap ,r :!subl .<CR>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment