Skip to content

Instantly share code, notes, and snippets.

@Alvarz
Last active January 1, 2025 19:45
Show Gist options
  • Save Alvarz/615f2171d97d26056896179c9f0a1040 to your computer and use it in GitHub Desktop.
Save Alvarz/615f2171d97d26056896179c9f0a1040 to your computer and use it in GitHub Desktop.
Setup neovim Mac
-- Load Packer
require('packer').startup(function(use)
use 'wbthomason/packer.nvim' -- Packer manages itself
-- Solve init issue
use "nvim-lua/plenary.nvim"
-- VSCode-like features
use 'numToStr/Comment.nvim'
use 'nvim-tree/nvim-web-devicons'
use 'neovim/nvim-lspconfig' -- LSP support
use 'hrsh7th/nvim-cmp' -- Autocompletion plugin
use 'hrsh7th/cmp-nvim-lsp' -- LSP source for nvim-cmp
use 'nvim-treesitter/nvim-treesitter' -- Syntax highlighting
use 'nvim-telescope/telescope.nvim' -- Fuzzy finder
use 'kyazdani42/nvim-tree.lua' -- File explorer
use {'akinsho/bufferline.nvim', tag = "*", requires = 'nvim-tree/nvim-web-devicons'} -- Buffer tabs
use 'nvim-lualine/lualine.nvim' -- Status line
use 'lewis6991/gitsigns.nvim' -- Git integration
use 'jose-elias-alvarez/null-ls.nvim' -- Linter/Formatter
-- Themes
use 'Mofiqul/vscode.nvim' -- VSCode-like theme
end)
-- LSP Setup
local lspconfig = require('lspconfig')
lspconfig.pyright.setup{} -- Example for Python; replace with your languageis
-- TypeScript Language Server setup
lspconfig.ts_ls.setup({
on_attach = function(client, bufnr)
-- Disable formatting (optional if using another formatter like Prettier)
client.server_capabilities.documentFormattingProvider = false
client.server_capabilities.documentRangeFormattingProvider = false
-- Key mappings for LSP
local bufopts = { noremap = true, silent = true, buffer = bufnr }
vim.keymap.set('n', 'gd', vim.lsp.buf.definition, bufopts) -- Go to definition
vim.keymap.set('n', 'K', vim.lsp.buf.hover, bufopts) -- Show hover docs
vim.keymap.set('n', 'gr', vim.lsp.buf.references, bufopts) -- Show references
vim.keymap.set('n', '<leader>rn', vim.lsp.buf.rename, bufopts) -- Rename
vim.keymap.set('n', '<leader>ca', vim.lsp.buf.code_action, bufopts) -- Code actions
end,
capabilities = require('cmp_nvim_lsp').default_capabilities(), -- Enable completion
})
lspconfig.rust_analyzer.setup({
settings = {
["rust-analyzer"] = {
cargo = {
allFeatures = true,
},
procMacro = {
enable = true,
},
checkOnSave = true,
check = { command = "clippy", features = "all" },
assist = {
importGranularity = 'module',
importPrefix = 'self',
},
diagnostics = {
enable = true,
enableExperimental = true,
},
cargo = {
loadOutDirsFromCheck = true,
features = "all", -- avoid error: file not included in crate hierarchy
},
procMacro = {
enable = true,
},
inlayHints = {
chainingHints = true,
parameterHints = true,
typeHints = true,
},
},
},
})
-- Comment
require('Comment').setup({
toggler = {
line = '<C-/>', -- Ctrl+/ for line comments
block = '<C-/>', -- Ctrl+/ for block comments (can customize if needed)
},
opleader = {
line = '<C-/>', -- Ctrl+/ for line comments in visual mode
block = '<C-/>', -- Ctrl+/ for block comments in visual mode
},
})
-- Autocompletion setup
local cmp = require('cmp')
cmp.setup({
snippet = {
expand = function(args)
require('luasnip').lsp_expand(args.body)
end,
},
mapping = {
-- ['<C-n>'] = cmp.mapping.select_next_item(),
['<tab>'] = cmp.mapping.select_next_item(),
['<C-p>'] = cmp.mapping.select_prev_item(),
['<CR>'] = cmp.mapping.confirm(),
},
sources = {
{ name = 'nvim_lsp' },
},
})
-- Treesitter
require('nvim-treesitter.configs').setup {
ensure_installed = { "lua", "python", "javascript", "rust", "toml" }, -- Add languages
highlight = { enable = true },
}
-- Telescope
require('telescope').setup{}
-- File Explorer
-- disable netrw at the very start of your init.lua
vim.g.loaded_netrw = 1
vim.g.loaded_netrwPlugin = 1
-- optionally enable 24-bit colour
require('nvim-tree').setup{}
-- Status line
require('lualine').setup{ options = { theme = 'vscode' } }
-- Buffer line
vim.opt.termguicolors = true
require("bufferline").setup{}
-- OR setup with some options
require("nvim-tree").setup({
sort = {
sorter = "case_sensitive",
},
view = {
width = 30,
},
renderer = {
group_empty = true,
},
-- filters = {
-- dotfiles = true,
-- },
})
-- NULL ls
local null_ls = require("null-ls")
null_ls.setup({
sources = {
null_ls.builtins.formatting.prettier,
null_ls.builtins.diagnostics.eslint,
null_ls.builtins.completion.spell,
},
})
-- THEME
-- For dark theme (neovim's default)
require('vscode').setup{}
vim.cmd.colorscheme "vscode"
-- Keybindings
vim.api.nvim_set_keymap('n', '<C-p>', ':Telescope find_files<CR>', { noremap = true, silent = true })
vim.api.nvim_set_keymap('n', '<C-f>', ':Telescope live_grep<CR>', { noremap = true, silent = true })
vim.api.nvim_set_keymap('n', '<C-b>', ':NvimTreeToggle<CR>', { noremap = true, silent = true })
local builtin = require('telescope.builtin')
vim.keymap.set('n', '<leader>ff', builtin.find_files, { desc = 'Telescope find files' })
vim.keymap.set('n', '<leader>fg', builtin.live_grep, { desc = 'Telescope live grep' })
vim.keymap.set('n', '<leader>fb', builtin.buffers, { desc = 'Telescope buffers' })
vim.keymap.set('n', '<leader>fh', builtin.help_tags, { desc = 'Telescope help tags' })
-- Map Ctrl-t to navigate tabs
vim.api.nvim_set_keymap('n', 'gt', ':BufferLineCycleNext<CR>', { noremap = true, silent = true })
vim.api.nvim_set_keymap('n', 'gT', ':BufferLineCyclePrev<CR>', { noremap = true, silent = true })
-- Config
-- Enable line numbers
vim.opt.number = true -- Show absolute line numbers
vim.opt.relativenumber = false -- Show relative line numbers
-- Auto format on save
vim.cmd([[
augroup fmt
autocmd!
autocmd BufWritePre *.js,*.ts,*.jsx,*.tsx,*.json,*.css,*.scss,*.html,*.rs,*.py lua vim.lsp.buf.format()
augroup END
]])

1. Install Neovim

brew install neovim

2. Install a Plugin Manager

git clone --depth 1 https://github.com/wbthomason/packer.nvim ~/.local/share/nvim/site/pack/packer/start/packer.nvim

3. Configure Neovim

mkdir -p ~/.config/nvim
nvim ~/.config/nvim/init.lua

4. Install Language Servers for LSP (python and javascript

npm install -g typescript-language-server pyright
rustup component add rust-analyzer

5. Install Fonts

brew install --cask font-hack-nerd-font

6. Install Iterm2

brew install iterm2 --cask

7. Configure ZSH

alias vim="nvim"
export TERM=xterm-256color

8. Customizing iTerm2

Font

  • Type: ⌘,
  • Click the Profiles icon👨.
  • Click on the Text tab.
  • Under the Font label, click and select one of the Nerd fonts you've installed - e.g., Hack Nerd Font - along with the style (e.g., Regular) and size (e.g., 18).

9. Install ripgrep to ignore folders on search

brew install ripgrep

9. Install Plugins

Open Neovim and install plugins with Packer:

:PackerSync
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment