我的 Neovim 设置用于 React、TypeScript、Tailwind CSS 等
Takuya 的点文件
大家好,我是 Takuy a。 你可能知道,我主要使用 Neovim 编写我的应用程序 Inkdrop ,这是一款跨平台的 Markdown 笔记应用程序。 它使用 Electron 构建桌面版,使用 React Native 构建移动平台。距离我上次发布 我的 Neovim 设置 已经过去一年了 。Neovim 及其插件已经发展得非常好了。 所以,我想分享我最新的基于 React 和 TypeScript 的应用程序编码设置。 主要区别在于配置文件现在是用 Lua 编写的。 我从 vim-plug 换到了 Packer。 我还制作了一个教程视频,介绍如何在新款 M2 MacBook Air 上从头开始设置 Neovim。 如果你已经有自己的点文件,你可以选择我的配置。 希望你喜欢!
原料
以下是我的设置的简要概述:
这是我的点文件存储库:
Takuya 的点文件
警告 :除非您了解我的设置会导致什么后果,否则请勿盲目使用。使用风险自负!
正在寻找 Markdown 笔记应用程序?
查看我的应用程序 Inkdrop
内容
vim(Neovim)配置
tmux 配置
git 配置
鱼配置
PowerShell 配置
Neovim 设置
要求
Shell 设置(macOS 和 Linux)
教程视频:
VIDEO
先决条件 — iTerm2 和修补的 Nerd 字体
iTerm2 是一款适用于 macOS 的快速终端模拟器。安装 Nerd Fonts 之一 ,即可在终端上显示精美的字形。 我目前选择的是 Hack 。 并在终端应用中使用它。例如,在 iTerm2 上:
通过 Homebrew 安装 Neovim
brew install neovim
Enter fullscreen mode
Exit fullscreen mode
目录结构
Neovim 遵循 XDG 基础目录 结构。以下是我的配置文件结构:
📂 ~/.config/nvim
├── 📁 after
│ └── 📁 plugin
├── 📂 lua
│ └── 🌑 base.lua
├── 📁 plugin
└── 🇻 init.lua
Enter fullscreen mode
Exit fullscreen mode
Neovim 首先加载 $HOME/.config/nvim/init.vim
或 init.lua
而不是 $HOME/.vimrc
。 查看快速入门指南了解更多详细信息:
https://github.com/nanotee/nvim-lua-guide
安装插件管理器:Packer
通过运行以下命令 安装 Packer :
git clone --depth 1 https://github.com/wbthomason/packer.nvim \
~/.local/share/nvim/site/pack/packer/start/packer.nvim
Enter fullscreen mode
Exit fullscreen mode
然后, ./.config/nvim/lua/plugins.lua
像这样:
local status , packer = pcall ( require , "packer" )
if ( not status ) then
print ( "Packer is not installed" )
return
end
vim . cmd [[packadd packer.nvim]]
packer . startup ( function ( use )
use 'wbthomason/packer.nvim'
-- Your plugins go here
end )
Enter fullscreen mode
Exit fullscreen mode
然后, init.lua
像这样需要它:
require('plugins')
Enter fullscreen mode
Exit fullscreen mode
配色方案:Neosolarized
我使用 svrana/neosolarized.nvim 并进行了一些定制。
local status , n = pcall ( require , "neosolarized" )
if ( not status ) then return end
n . setup ({
comment_italics = true ,
})
local cb = require ( 'colorbuddy.init' )
local Color = cb . Color
local colors = cb . colors
local Group = cb . Group
local groups = cb . groups
local styles = cb . styles
Color . new ( 'black' , '#000000' )
Group . new ( 'CursorLine' , colors . none , colors . base03 , styles . NONE , colors . base1 )
Group . new ( 'CursorLineNr' , colors . yellow , colors . black , styles . NONE , colors . base1 )
Group . new ( 'Visual' , colors . none , colors . base03 , styles . reverse )
local cError = groups . Error . fg
local cInfo = groups . Information . fg
local cWarn = groups . Warning . fg
local cHint = groups . Hint . fg
Group . new ( "DiagnosticVirtualTextError" , cError , cError : dark (): dark (): dark (): dark (), styles . NONE )
Group . new ( "DiagnosticVirtualTextInfo" , cInfo , cInfo : dark (): dark (): dark (), styles . NONE )
Group . new ( "DiagnosticVirtualTextWarn" , cWarn , cWarn : dark (): dark (): dark (), styles . NONE )
Group . new ( "DiagnosticVirtualTextHint" , cHint , cHint : dark (): dark (): dark (), styles . NONE )
Group . new ( "DiagnosticUnderlineError" , colors . none , colors . none , styles . undercurl , cError )
Group . new ( "DiagnosticUnderlineWarn" , colors . none , colors . none , styles . undercurl , cWarn )
Group . new ( "DiagnosticUnderlineInfo" , colors . none , colors . none , styles . undercurl , cInfo )
Group . new ( "DiagnosticUnderlineHint" , colors . none , colors . none , styles . undercurl , cHint )
Enter fullscreen mode
Exit fullscreen mode
状态行:Lualine
nvim-lualine/lualine.nvim 提供了一种灵活的方式来配置状态行。
local status , lualine = pcall ( require , "lualine" )
if ( not status ) then return end
lualine . setup {
options = {
icons_enabled = true ,
theme = 'solarized_dark' ,
section_separators = { left = '' , right = '' },
component_separators = { left = '' , right = '' },
disabled_filetypes = {}
},
sections = {
lualine_a = { 'mode' },
lualine_b = { 'branch' },
lualine_c = { {
'filename' ,
file_status = true , -- displays file status (readonly status, modified status)
path = 0 -- 0 = just filename, 1 = relative path, 2 = absolute path
} },
lualine_x = {
{ 'diagnostics' , sources = { "nvim_diagnostic" }, symbols = { error = ' ' , warn = ' ' , info = ' ' ,
hint = ' ' } },
'encoding' ,
'filetype'
},
lualine_y = { 'progress' },
lualine_z = { 'location' }
},
inactive_sections = {
lualine_a = {},
lualine_b = {},
lualine_c = { {
'filename' ,
file_status = true , -- displays file status (readonly status, modified status)
path = 1 -- 0 = just filename, 1 = relative path, 2 = absolute path
} },
lualine_x = { 'location' },
lualine_y = {},
lualine_z = {}
},
tabline = {},
extensions = { 'fugitive' }
}
Enter fullscreen mode
Exit fullscreen mode
Lspconfig
Neovim 内置了 LSP 支持。 你可以使用 neovim/nvim-lspconfig 轻松配置它。 例如,在 Neovim 上启用 TypeScript 语言服务器:
local status , nvim_lsp = pcall ( require , "lspconfig" )
if ( not status ) then return end
local protocol = require ( 'vim.lsp.protocol' )
local on_attach = function ( client , bufnr )
-- format on save
if client . server_capabilities . documentFormattingProvider then
vim . api . nvim_create_autocmd ( "BufWritePre" , {
group = vim . api . nvim_create_augroup ( "Format" , { clear = true }),
buffer = bufnr ,
callback = function () vim . lsp . buf . formatting_seq_sync () end
})
end
end
-- TypeScript
nvim_lsp . tsserver . setup {
on_attach = on_attach ,
filetypes = { "typescript" , "typescriptreact" , "typescript.tsx" },
cmd = { "typescript-language-server" , "--stdio" }
}
Enter fullscreen mode
Exit fullscreen mode
不要忘记安装 TypeScript 语言服务器本身:
npm i -g typescript-language-server
Enter fullscreen mode
Exit fullscreen mode
自动完成:Lspkind 和 cmp
为了获得具有精美象形图的 LSP 感知自动完成功能,我使用了以下插件:
像这样配置它:
local status , cmp = pcall ( require , "cmp" )
if ( not status ) then return end
local lspkind = require 'lspkind'
cmp . setup ({
snippet = {
expand = function ( args )
require ( 'luasnip' ). lsp_expand ( args . body )
end ,
},
mapping = cmp . mapping . preset . insert ({
[ '<C-d>' ] = cmp . mapping . scroll_docs ( - 4 ),
[ '<C-f>' ] = cmp . mapping . scroll_docs ( 4 ),
[ '<C-Space>' ] = cmp . mapping . complete (),
[ '<C-e>' ] = cmp . mapping . close (),
[ '<CR>' ] = cmp . mapping . confirm ({
behavior = cmp . ConfirmBehavior . Replace ,
select = true
}),
}),
sources = cmp . config . sources ({
{ name = 'nvim_lsp' },
{ name = 'buffer' },
}),
formatting = {
format = lspkind . cmp_format ({ with_text = false , maxwidth = 50 })
}
})
vim . cmd [[
set completeopt=menuone,noinsert,noselect
highlight! default link CmpItemKind CmpItemMenuDefault
]]
Enter fullscreen mode
Exit fullscreen mode
语法突出显示:Treesitter
Treesitter 是一款流行的语法高亮语言解析器。 首先,安装它:
brew install tree-sitter
Enter fullscreen mode
Exit fullscreen mode
使用 Packer安装 nvim-treesitter/nvim-treesitter 并进行如下配置:
local status , ts = pcall ( require , "nvim-treesitter.configs" )
if ( not status ) then return end
ts . setup {
highlight = {
enable = true ,
disable = {},
},
indent = {
enable = true ,
disable = {},
},
ensure_installed = {
"tsx" ,
"toml" ,
"fish" ,
"php" ,
"json" ,
"yaml" ,
"swift" ,
"css" ,
"html" ,
"lua"
},
autotag = {
enable = true ,
},
}
local parser_config = require "nvim-treesitter.parsers" . get_parser_configs ()
parser_config . tsx . filetype_to_parsername = { "javascript" , "typescript.tsx" }
Enter fullscreen mode
Exit fullscreen mode
自动标记和自动配对
对于 React 应用程序,您通常希望快速关闭标签。windwp /nvim-ts-autotag 正是您想要的。
local status , autotag = pcall ( require , "nvim-ts-autotag" )
if ( not status ) then return end
autotag . setup ({})
Enter fullscreen mode
Exit fullscreen mode
windwp/nvim-autopairs 用于关闭括号。
local status , autopairs = pcall ( require , "nvim-autopairs" )
if ( not status ) then return end
autopairs . setup ({
disable_filetype = { "TelescopePrompt" , "vim" },
})
Enter fullscreen mode
Exit fullscreen mode
模糊探测器:望远镜
telescope.nvim 提供了一个基于列表的交互式模糊查找器,它基于最新的 Neovim 功能构建。 我还使用 telescope-file-browser.nvim 作为文件管理器。
它非常实用,因为你可以在查看文件内容的同时搜索文件,而无需实际打开它们。它支持各种来源,例如 Vim 、 文件 、 Git 、 LSP 和 Treesitter 。查看 Telescope 的 展示 。
安装 kyazdani42/nvim-web-devicons 以获取 Telescope、状态行和其他支持的插件上的文件图标。
配置看起来如下:
local status , telescope = pcall ( require , "telescope" )
if ( not status ) then return end
local actions = require ( 'telescope.actions' )
local builtin = require ( "telescope.builtin" )
local function telescope_buffer_dir ()
return vim . fn . expand ( '%:p:h' )
end
local fb_actions = require "telescope" . extensions . file_browser . actions
telescope . setup {
defaults = {
mappings = {
n = {
[ "q" ] = actions . close
},
},
},
}
-- keymaps
vim . keymap . set ( 'n' , ';f' ,
function ()
builtin . find_files ({
no_ignore = false ,
hidden = true
})
end )
vim . keymap . set ( 'n' , ';r' , function ()
builtin . live_grep ()
end )
vim . keymap . set ( 'n' , ' \\\\ ' , function ()
builtin . buffers ()
end )
vim . keymap . set ( 'n' , ';t' , function ()
builtin . help_tags ()
end )
vim . keymap . set ( 'n' , ';;' , function ()
builtin . resume ()
end )
vim . keymap . set ( 'n' , ';e' , function ()
builtin . diagnostics ()
end )
Enter fullscreen mode
Exit fullscreen mode
使用望远镜浏览器扩展:
telescope . setup {
defaults = {
mappings = {
n = {
[ "q" ] = actions . close
},
},
},
extensions = {
file_browser = {
theme = "dropdown" ,
-- disables netrw and use telescope-file-browser in its place
hijack_netrw = true ,
mappings = {
-- your custom insert mode mappings
[ "i" ] = {
[ "<C-w>" ] = function () vim . cmd ( 'normal vbd' ) end ,
},
[ "n" ] = {
-- your custom normal mode mappings
[ "N" ] = fb_actions . create ,
[ "h" ] = fb_actions . goto_parent_dir ,
[ "/" ] = function ()
vim . cmd ( 'startinsert' )
end
},
},
},
},
}
telescope . load_extension ( "file_browser" )
vim . keymap . set ( "n" , "sf" , function ()
telescope . extensions . file_browser . file_browser ({
path = "%:p:h" ,
cwd = telescope_buffer_dir (),
respect_gitignore = false ,
hidden = true ,
grouped = true ,
previewer = false ,
initial_mode = "normal" ,
layout_config = { height = 40 }
})
end )
Enter fullscreen mode
Exit fullscreen mode
标签:缓冲线
我使用 akinsho/nvim-bufferline.lua 来优化标签页的显示效果。 使用 Solarized 主题进行一些自定义,使其更加美观:
local status , bufferline = pcall ( require , "bufferline" )
if ( not status ) then return end
bufferline . setup ({
options = {
mode = "tabs" ,
separator_style = 'slant' ,
always_show_bufferline = false ,
show_buffer_close_icons = false ,
show_close_icon = false ,
color_icons = true
},
highlights = {
separator = {
guifg = '#073642' ,
guibg = '#002b36' ,
},
separator_selected = {
guifg = '#073642' ,
},
background = {
guifg = '#657b83' ,
guibg = '#002b36'
},
buffer_selected = {
guifg = '#fdf6e3' ,
gui = "bold" ,
},
fill = {
guibg = '#073642'
}
},
})
vim . keymap . set ( 'n' , '<Tab>' , '<Cmd>BufferLineCycleNext<CR>' , {})
vim . keymap . set ( 'n' , '<S-Tab>' , '<Cmd>BufferLineCyclePrev<CR>' , {})
Enter fullscreen mode
Exit fullscreen mode
LSP 页面:Lspsaga
glepnir/lspsaga.nvim 是我最喜欢的 LSP 插件之一。 它为各种 LSP 相关功能(例如悬停文档、定义预览和重命名操作)提供了精美的 UI。 我的配置很简单:
local status , saga = pcall ( require , "lspsaga" )
if ( not status ) then return end
saga . init_lsp_saga {
server_filetype_map = {
typescript = 'typescript'
}
}
local opts = { noremap = true , silent = true }
vim . keymap . set ( 'n' , '<C-j>' , '<Cmd>Lspsaga diagnostic_jump_next<CR>' , opts )
vim . keymap . set ( 'n' , 'K' , '<Cmd>Lspsaga hover_doc<CR>' , opts )
vim . keymap . set ( 'n' , 'gd' , '<Cmd>Lspsaga lsp_finder<CR>' , opts )
vim . keymap . set ( 'i' , '<C-k>' , '<Cmd>Lspsaga signature_help<CR>' , opts )
vim . keymap . set ( 'n' , 'gp' , '<Cmd>Lspsaga preview_definition<CR>' , opts )
vim . keymap . set ( 'n' , 'gr' , '<Cmd>Lspsaga rename<CR>' , opts )
Enter fullscreen mode
Exit fullscreen mode
代码格式化程序:Prettier 和 null-ls
我非常依赖 Prettier 来格式化 TypeScript/JavaScript/CSS 文件。 使用 jose-elias-alvarez/null-ls.nvim 和 MunifTanjim/prettier.nvim 来实现。
首先,你需要 prettierd :
brew install prettierd
Enter fullscreen mode
Exit fullscreen mode
然后,按如下方式配置 null-ls:
local status , null_ls = pcall ( require , "null-ls" )
if ( not status ) then return end
null_ls . setup ({
sources = {
null_ls . builtins . diagnostics . eslint_d . with ({
diagnostics_format = '[eslint] #{m}\n(#{c})'
}),
null_ls . builtins . diagnostics . fish
}
})
Enter fullscreen mode
Exit fullscreen mode
更漂亮:
local status , prettier = pcall ( require , "prettier" )
if ( not status ) then return end
prettier . setup {
bin = 'prettierd' ,
filetypes = {
"css" ,
"javascript" ,
"javascriptreact" ,
"typescript" ,
"typescriptreact" ,
"json" ,
"scss" ,
"less"
}
}
Enter fullscreen mode
Exit fullscreen mode
Git 标记:gitsigns
lewis6991/gitsigns.nvim 为当前缓冲区提供 git 装饰。 它可以帮助您了解当前哪些行发生了更改。 它开箱即用。
require ( 'gitsigns' ). setup {}
Enter fullscreen mode
Exit fullscreen mode
git
我经常在 GitHub 上查看代码。dinhhuy258 /git.nvim 有助于直接从 Neovim 打开 GitHub,并提供 git blame
分屏视图,非常方便。
local status , git = pcall ( require , "git" )
if ( not status ) then return end
git . setup ({
keymaps = {
-- Open blame window
blame = "<Leader>gb" ,
-- Open file/folder in git repository
browse = "<Leader>go" ,
}
})
Enter fullscreen mode
Exit fullscreen mode
LSP工具:mason
如果您需要为特定库提供额外的 LSP 支持,则可能需要 williamboman/mason.nvim 和 williamboman/mason-lspconfig.nvim 。 我使用它们来使 Tailwind CSS 语言服务器在 Neovim 上运行。
local status , mason = pcall ( require , "mason" )
if ( not status ) then return end
local status2 , lspconfig = pcall ( require , "mason-lspconfig" )
if ( not status2 ) then return end
mason . setup ({
})
lspconfig . setup {
ensure_installed = { "sumneko_lua" , "tailwindcss" },
}
Enter fullscreen mode
Exit fullscreen mode
添加 lspconfig:
local nvim_lsp = require "lspconfig"
nvim_lsp . tailwindcss . setup {}
Enter fullscreen mode
Exit fullscreen mode
差不多就是这样了! 希望它能帮助你改善 Neovim 环境。
在线关注我
文章来源:https://dev.to/craftzdog/my-neovim-setup-for-react-typescript-tailwind-css-etc-58fb