User Tools

Site Tools


neovim

IDE Neovim for Go

La idea original de esta selección de plugins es de Mario Carrión en su video Neovim configuration for Golang Development (2023). Este es su repo de GitHub.

Los cambios que se le han hecho son:

  • En lua/plugins/setup.lua se comenta la carga de “nvim-telescope/telescope-fzf-native.nvim” al no haber plugin nativo para OpenBSD ni Termux.
  • En lua/plugins/barbar.lua se comenta la opción “closable = false” para desactivar el botón de cerrado de las pestañas ya que da el siguiente error: “The barbar.nvim option 'closable' is deprecated, use 'icons.button' and 'icons.modified.button' instead. See :h deprecated

FIXME <leader> t Me abre una ventana inferior con comentarios sobre el códig o

Atajos útiles

Aquí está la lista de atajos de teclado en Vim que podemos usar en Neovim.

Algunos ejemplos:

  • :help [comando]
  • CTRL+W nos permite gestionar varias vistas del fichero que estamos editando. Por ejemplo CTRL+W v divide la ventana en vertical.
  • :command [comando] visualiza la lista de comandos disponibles o info sobre uno en concreto

Colapsar funciones con nvim-treesitter

  • zo - Open (despliega el bloque colapsado)
  • zc - Close (colapsa el bloque)

Directorios y ficheros

~/.config/nvim/ Ficheros de configuración
~/.local/share/nvim/ Código de los plugins

Plugins (y sus dependencias) definidos en ~/.config/nvim/init.lua

Tipo Repositorio y Dependencias Descripción
Paquetes wbthomason/packer.nvim use-package inspired plugin/package management for Neovim.
Look and Feel catppuccin/nvim Provides theme support for other plugins in the Neovim ecosystem and extended Neovim functionality through integrations
Productivity romgrk/barbar.nvim A tabline plugin with re-orderable, auto-sizing, clickable tabs, icons, nice highlighting, sort-by commands and a magic jump-to-buffer mode.
Plus the tab names are made unique when two filenames match.
nvim-tree/nvim-web-devicons Lua `fork` of vim-web-devicons for neovim.
nvim-lualine/lualine.nvim A blazing fast and easy to configure neovim statusline plugin written in pure lua.
nvim-tree/nvim-tree.lua A file explorer tree for neovim written in lua.
nvim-tree/nvim-web-devicons Lua `fork` of vim-web-devicons for neovim.
nvim-telescope/telescope.nvim A highly extendable fuzzy finder over lists.
nvim-lua/plenary.nvim A Lua module for asynchronous programming using coroutines.
BurntSushi/ripgrep Recursively searches directories for a regex pattern while respecting your gitignore
Filetypes chrisbra/csv.vim A Filetype plugin for csv files.
Development lewis6991/gitsigns.nvim Git integration for buffers.
nvim-treesitter/nvim-treesitter The goal of nvim-treesitter is both to provide a simple and easy way to use the interface for tree-sitter in Neovim and to provide some basic functionality such as highlighting based on it.
nvim-treesitter/nvim-treesitter-textobjects Syntax aware text-objects, select, move, swap, and peek support.
rhysd/vim-clang-format Vim plugin for clang-format, a formatter for C, C++, Obj-C, Java, JavaScript, and so on.
fatih/vim-go Go development plugin for Vim.
sirver/UltiSnips The ultimate snippet solution for Vim.
hrsh7th/cmp-nvim-lsp nvim-cmp source for neovim's built-in language server client (LSP).
hrsh7th/nvim-cmp A completion plugin for neovim coded in Lua.
nvim-lspconfig Quickstart configs for Nvim LSP.
onsails/lspkind-nvim
quangnguyen30192/cmp-nvim-ultisnips UltiSnips completion source for nvim-cmp.
williamboman/nvim-lsp-installer FIXME Neovim plugin that allow you to manage LSP servers (servers are installed inside :echo stdpath(“data”) by default). It works in tandem with lspconfig1 by registering a hook that enhances the PATH environment variable, allowing neovim's LSP client to locate the server executable installed by nvim-lsp-installer.
numToStr/Comment.nvim Smart and powerful comment plugin for neovim. Supports treesitter, dot repeat, left-right/up-down motions, hooks, and more.
kylechui/nvim-surround Add/change/delete surrounding delimiter pairs with ease. Written with ❤️ in Lua.

Go LSP: gopls

Ficheros de configuración

Esta es la lista de ficheros de configuración en ~/.config/nvim/lua/plugins:

barbar.lua
bufferline.lua
catppuccin-theme.lua
comment.lua
default.lua
gitsigns.lua
lualine.lua
nvim-cmp.lua
nvim-surround.lua
nvim-tree.lua
nvim-treesitter.lua
setup.lua
telescope.lua

Veamos el contenido de cada fichero de configuración:

~/.config/nvim/init.lua

  1. require("plugins.setup")
  2. require("plugins.default")
  3. require("plugins.lualine")
  4. require("plugins.nvim-tree")
  5. require("plugins.comment")
  6. require("plugins.telescope")
  7. require("plugins.nvim-treesitter")
  8. require("plugins.gitsigns")
  9. require("plugins.nvim-cmp")
  10. require("plugins.catppuccin-theme")
  11. require("plugins.barbar")
  12. require("plugins.nvim-surround")

barbar.lua

  1. local setup, bufferline = pcall(require, "bufferline")
  2. if not setup then return end
  3.  
  4. bufferline.setup({
  5. -- closable = false, -- Enable/disable close button
  6. clickable = false, -- Enables/disable clickable tabs
  7. tabpages = true, -- Enable/disable current/total tabpages indicator (top right corner)
  8. icons = "none", -- Enable/disable icons
  9. icons = { buffer_index = true, filetype = { enabled = true } }
  10. })
  11.  
  12. local map = vim.api.nvim_set_keymap
  13. local opts = { noremap = true, silent = true }
  14.  
  15. -- Move to previous/next
  16. map('n', '<A-,>', '<Cmd>BufferPrevious<CR>', opts)
  17. map('n', '<A-.>', '<Cmd>BufferNext<CR>', opts)
  18. -- Re-order to previous/next
  19. map('n', '<A-<>', '<Cmd>BufferMovePrevious<CR>', opts)
  20. map('n', '<A->>', '<Cmd>BufferMoveNext<CR>', opts)
  21. -- Goto buffer in position...
  22. map('n', '<A-1>', '<Cmd>BufferGoto 1<CR>', opts)
  23. map('n', '<A-2>', '<Cmd>BufferGoto 2<CR>', opts)
  24. map('n', '<A-3>', '<Cmd>BufferGoto 3<CR>', opts)
  25. map('n', '<A-4>', '<Cmd>BufferGoto 4<CR>', opts)
  26. map('n', '<A-5>', '<Cmd>BufferGoto 5<CR>', opts)
  27. map('n', '<A-6>', '<Cmd>BufferGoto 6<CR>', opts)
  28. map('n', '<A-7>', '<Cmd>BufferGoto 7<CR>', opts)
  29. map('n', '<A-8>', '<Cmd>BufferGoto 8<CR>', opts)
  30. map('n', '<A-9>', '<Cmd>BufferGoto 9<CR>', opts)
  31. map('n', '<A-0>', '<Cmd>BufferLast<CR>', opts)
  32. -- Pin/unpin buffer
  33. map('n', '<A-p>', '<Cmd>BufferPin<CR>', opts)
  34. -- Close buffer
  35. map('n', '<A-c>', '<Cmd>BufferClose<CR>', opts)
  36. -- Wipeout buffer
  37. -- :BufferWipeout
  38. -- Close commands
  39. -- :BufferCloseAllButCurrent
  40. -- :BufferCloseAllButPinned
  41. -- :BufferCloseAllButCurrentOrPinned
  42. -- :BufferCloseBuffersLeft
  43. -- :BufferCloseBuffersRight
  44. -- Magic buffer-picking mode
  45. map('n', '<C-p>', '<Cmd>BufferPick<CR>', opts)
  46. -- Sort automatically by...
  47. map('n', '<Space>bb', '<Cmd>BufferOrderByBufferNumber<CR>', opts)
  48. map('n', '<Space>bd', '<Cmd>BufferOrderByDirectory<CR>', opts)
  49. map('n', '<Space>bl', '<Cmd>BufferOrderByLanguage<CR>', opts)
  50. map('n', '<Space>bw', '<Cmd>BufferOrderByWindowNumber<CR>', opts)
  51.  
  52. -- Other:
  53. -- :BarbarEnable - enables barbar (enabled by default)
  54. -- :BarbarDisable - very bad command, should never be used

bufferline.lua

  1. vim.opt.termguicolors = true
  2.  
  3. require("bufferline").setup()

catppuccin-theme.lua

  1. local present, catppuccin = pcall(require, "catppuccin")
  2. if not present then return end
  3.  
  4. vim.opt.termguicolors = true
  5.  
  6. catppuccin.setup {
  7. flavour = "mocha",
  8. term_colors = true,
  9. transparent_background = false,
  10. no_italic = false,
  11. no_bold = false,
  12. styles = {
  13. comments = { "italic" },
  14. conditionals = {},
  15. loops = {},
  16. functions = { "italic" },
  17. keywords = {},
  18. strings = {},
  19. variables = {},
  20. numbers = {},
  21. booleans = {},
  22. properties = {},
  23. types = { "bold" },
  24. },
  25. color_overrides = {
  26. mocha = {
  27. base = "#171717", -- background
  28. surface2 = "#9A9A9A", -- comments
  29. text = "#F6F6F6",
  30. },
  31. },
  32. highlight_overrides = {
  33. mocha = function(C)
  34. return {
  35. NvimTreeNormal = { bg = C.none },
  36. CmpBorder = { fg = C.surface2 },
  37. Pmenu = { bg = C.none },
  38. NormalFloat = { bg = C.none },
  39. TelescopeBorder = { link = "FloatBorder" },
  40. }
  41. end,
  42. },
  43. }
  44.  
  45. vim.cmd.colorscheme "catppuccin"

comment.lua

  1. local setup, comment = pcall(require, "Comment")
  2. if not setup then return end
  3.  
  4. comment.setup()

default.lua

  1. vim.g.mapleader = ","
  2.  
  3. vim.opt.encoding="utf-8"
  4.  
  5. vim.opt.compatible=false
  6. vim.opt.hlsearch=true
  7. vim.opt.relativenumber = true
  8. vim.opt.laststatus = 2
  9. vim.opt.vb = true
  10. vim.opt.ruler = true
  11. vim.opt.spelllang="es_es"
  12. vim.opt.autoindent=true
  13. vim.opt.colorcolumn="120"
  14. vim.opt.textwidth=120
  15. vim.opt.mouse="a"
  16. vim.opt.clipboard="unnamed"
  17. vim.opt.scrollbind=false
  18. vim.opt.wildmenu=true

gitsigns.lua

  1. local setup, gitsigns = pcall(require, "gitsigns")
  2. if not setup then return end
  3.  
  4. gitsigns.setup()

lualine.lua

  1. local setup, lualine = pcall(require, "lualine")
  2. if not setup then return end
  3.  
  4. lualine.setup({
  5. options = {
  6. theme = 'ayu_dark',
  7. },
  8. })

nvim-cmp.lua

  1. local cmp_setup, cmp = pcall(require, "cmp")
  2. if not cmp_setup then return end
  3.  
  4. local lspkind_setup, lspkind = pcall(require, "lspkind")
  5. if not lspkind_setup then return end
  6.  
  7. local lspconfig_setup, lspconfig = pcall(require, "lspconfig")
  8. if not lspconfig_setup then return end
  9.  
  10. local cmp_nvim_lsp_setup, cmp_nvim_lsp = pcall(require, "cmp_nvim_lsp")
  11. if not cmp_nvim_lsp_setup then return end
  12.  
  13. cmp.setup({
  14. snippet = {
  15. expand = function(args)
  16. vim.fn["UltiSnips#Anon"](args.body)
  17. end,
  18. },
  19. window = {
  20. completion = cmp.config.window.bordered(),
  21. documentation = cmp.config.window.bordered(),
  22. },
  23. mapping = cmp.mapping.preset.insert({
  24. ['<C-b>'] = cmp.mapping.scroll_docs(-4),
  25. ['<C-f>'] = cmp.mapping.scroll_docs(4),
  26. ['<C-Space>'] = cmp.mapping.complete(),
  27. ['<C-e>'] = cmp.mapping.abort(),
  28. ['<CR>'] = cmp.mapping.confirm({ select = true }), -- Accept currently selected item. Set `select` to `false` to only confirm explicitly selected items.
  29. }),
  30. sources = cmp.config.sources({
  31. { name = 'nvim_lsp' },
  32. { name = 'ultisnips' },
  33. }, {
  34. { name = 'buffer' },
  35. })
  36. })
  37.  
  38. local capabilities = cmp_nvim_lsp.default_capabilities()
  39.  
  40. local on_attach = function(client, bufnr)
  41. -- Enable completion triggered by <c-x><c-o>
  42. vim.api.nvim_buf_set_option(bufnr, 'omnifunc', 'v:lua.vim.lsp.omnifunc')
  43.  
  44. -- Mappings.
  45. -- See `:help vim.lsp.*` for documentation on any of the below functions
  46. local bufopts = { noremap=true, silent=true, buffer=bufnr }
  47. vim.keymap.set('n', 'gD', vim.lsp.buf.declaration, bufopts)
  48. vim.keymap.set('n', 'gd', vim.lsp.buf.definition, bufopts)
  49. vim.keymap.set('n', 'K', vim.lsp.buf.hover, bufopts)
  50. vim.keymap.set('n', 'gi', vim.lsp.buf.implementation, bufopts)
  51. vim.keymap.set('n', '<C-k>', vim.lsp.buf.signature_help, bufopts)
  52. vim.keymap.set('n', '<space>D', vim.lsp.buf.type_definition, bufopts)
  53. vim.keymap.set('n', 'rn', vim.lsp.buf.rename, bufopts)
  54. vim.keymap.set('n', '<space>ca', vim.lsp.buf.code_action, bufopts)
  55. vim.keymap.set('n', 'gr', vim.lsp.buf.references, bufopts)
  56. end
  57.  
  58. lspconfig['gopls'].setup {
  59. capabilities = capabilities,
  60. on_attach = on_attach,
  61. settings = {
  62. gopls = {
  63. analyses = {
  64. assign = true,
  65. atomic = true,
  66. bools = true,
  67. composites = true,
  68. copylocks = true,
  69. deepequalerrors = true,
  70. embed = true,
  71. errorsas = true,
  72. fieldalignment = true,
  73. httpresponse = true,
  74. ifaceassert = true,
  75. loopclosure = true,
  76. lostcancel = true,
  77. nilfunc = true,
  78. nilness = true,
  79. nonewvars = true,
  80. printf = true,
  81. shadow = true,
  82. shift = true,
  83. simplifycompositelit = true,
  84. simplifyrange = true,
  85. simplifyslice = true,
  86. sortslice = true,
  87. stdmethods = true,
  88. stringintconv = true,
  89. structtag = true,
  90. testinggoroutine = true,
  91. tests = true,
  92. timeformat = true,
  93. unmarshal = true,
  94. unreachable = true,
  95. unsafeptr = true,
  96. unusedparams = true,
  97. unusedresult = true,
  98. unusedvariable = true,
  99. unusedwrite = true,
  100. useany = true,
  101. },
  102. hoverKind = "FullDocumentation",
  103. linkTarget = "pkg.go.dev",
  104. usePlaceholders = true,
  105. vulncheck = "Imports",
  106. },
  107. },
  108. }
  109.  
  110. lspconfig['pyright'].setup{}
  111.  
  112. cmp.setup {
  113. formatting = {
  114. format = lspkind.cmp_format({
  115. mode = "symbol_text",
  116. maxwidth = 50,
  117.  
  118. before = function(entry, vim_item)
  119. return vim_item
  120. end
  121. })
  122. }
  123. }

nvim-surround.lua

  1. local setup, surround = pcall(require, "nvim-surround")
  2. if not setup then return end
  3.  
  4. surround.setup()

nvim-tree.lua

  1. local setup, nvimtree = pcall(require, "nvim-tree")
  2. if not setup then return end
  3.  
  4. vim.cmd([[
  5. nnoremap - :NvimTreeToggle<CR>
  6. ]])
  7.  
  8. local keymap = vim.keymap -- for conciseness
  9. keymap.set("n", "<leader>e", ":NvimTreeToggle<CR>") -- toggle file explorer
  10.  
  11. vim.opt.foldmethod = "expr"
  12. vim.opt.foldexpr = "nvim_treesitter#foldexpr()"
  13. vim.opt.foldenable = false -- " Disable folding at startup.
  14.  
  15. vim.g.loaded_netrw = 1
  16. vim.g.loaded_netrwPlugin = 1
  17.  
  18. vim.opt.termguicolors = true
  19.  
  20. local HEIGHT_RATIO = 0.8 -- You can change this
  21. local WIDTH_RATIO = 0.5 -- You can change this too
  22.  
  23. nvimtree.setup({
  24. disable_netrw = true,
  25. hijack_netrw = true,
  26. respect_buf_cwd = true,
  27. sync_root_with_cwd = true,
  28. view = {
  29. relativenumber = true,
  30. float = {
  31. enable = true,
  32. open_win_config = function()
  33. local screen_w = vim.opt.columns:get()
  34. local screen_h = vim.opt.lines:get() - vim.opt.cmdheight:get()
  35. local window_w = screen_w * WIDTH_RATIO
  36. local window_h = screen_h * HEIGHT_RATIO
  37. local window_w_int = math.floor(window_w)
  38. local window_h_int = math.floor(window_h)
  39. local center_x = (screen_w - window_w) / 2
  40. local center_y = ((vim.opt.lines:get() - window_h) / 2)
  41. - vim.opt.cmdheight:get()
  42. return {
  43. border = "rounded",
  44. relative = "editor",
  45. row = center_y,
  46. col = center_x,
  47. width = window_w_int,
  48. height = window_h_int,
  49. }
  50. end,
  51. },
  52. width = function()
  53. return math.floor(vim.opt.columns:get() * WIDTH_RATIO)
  54. end,
  55. },
  56. -- filters = {
  57. -- custom = { "^.git$" },
  58. -- },
  59. -- renderer = {
  60. -- indent_width = 1,
  61. -- },
  62. })

nvim-treesitter.lua

  1. local setup, treesitter = pcall(require, "nvim-treesitter.configs")
  2. if not setup then return end
  3.  
  4. vim.opt.foldmethod="expr"
  5. vim.opt.foldexpr="nvim_treesitter#foldexpr()"
  6. vim.opt.foldenable=false
  7.  
  8. treesitter.setup {
  9. ensure_installed = {
  10. "dockerfile",
  11. "gitignore",
  12. "go",
  13. "gomod",
  14. "gowork",
  15. "javascript",
  16. "json",
  17. "lua",
  18. "markdown",
  19. "proto",
  20. "python",
  21. "rego",
  22. "ruby",
  23. "sql",
  24. "svelte",
  25. "yaml",
  26. },
  27. indent = {
  28. enable = true,
  29. },
  30. auto_install = true,
  31. sync_install = false,
  32. highlight = {
  33. enable = true,
  34. disable = { "markdown" },
  35. -- -- Setting this to true will run `:h syntax` and tree-sitter at the same time.
  36. -- -- Set this to `true` if you depend on 'syntax' being enabled (like for indentation).
  37. -- -- Using this option may slow down your editor, and you may see some duplicate highlights.
  38. -- -- Instead of true it can also be a list of languages
  39. -- additional_vim_regex_highlighting = false,
  40. },
  41. }

setup.lua

  1. -- Packer, manually install it:
  2. -- git clone --depth 1 https://github.com/wbthomason/packer.nvim \
  3. -- ~/.config/nvim/pack/packer/start/packer.nvim
  4. -- OR auto install packer if not installed
  5. -- $PWD/.local/share/nvim/ + ..
  6. local ensure_packer = function()
  7. local fn = vim.fn
  8. local install_path = fn.stdpath("data") .. "/site/pack/packer/start/packer.nvim"
  9.  
  10. if fn.empty(fn.glob(install_path)) > 0 then
  11. fn.system({ "git", "clone", "--depth", "1", "https://github.com/wbthomason/packer.nvim", install_path })
  12. vim.cmd([[packadd packer.nvim]])
  13. return true
  14. end
  15. return false
  16. end
  17. local packer_bootstrap = ensure_packer() -- true if packer was just installed
  18.  
  19. return require("packer").startup(function()
  20. use "wbthomason/packer.nvim" -- https://github.com/wbthomason/packer.nvim
  21.  
  22. -- Look and Feel
  23. use { "catppuccin/nvim", as = "catppuccin" } -- https://github.com/catppuccin/nvim
  24.  
  25. -- Productivity
  26. use { "romgrk/barbar.nvim", wants = "nvim-tree/nvim-web-devicons" } -- https://github.com/romgrk/barbar.nvim
  27. use "nvim-lualine/lualine.nvim" -- https://github.com/nvim-lualine/lualine.nvim
  28. use {
  29. "nvim-tree/nvim-tree.lua", -- https://github.com/nvim-tree/nvim-tree.lua
  30. requires = {
  31. "nvim-tree/nvim-web-devicons", -- https://github.com/nvim-tree/nvim-web-devicons
  32. },
  33. }
  34. -- use {
  35. -- "nvim-telescope/telescope-fzf-native.nvim", -- https://github.com/nvim-telescope/telescope-fzf-native.nvim
  36. -- run = "make",
  37. -- }
  38. use {
  39. "nvim-telescope/telescope.nvim", -- https://github.com/nvim-telescope/telescope.nvim
  40. requires = {
  41. "nvim-lua/plenary.nvim",
  42. "BurntSushi/ripgrep",
  43. },
  44. branch = "0.1.x",
  45. }
  46.  
  47. -- Filetypes
  48. use "chrisbra/csv.vim" -- https://github.com/chrisbra/csv.vim
  49.  
  50. -- Development
  51. use "lewis6991/gitsigns.nvim" -- https://github.com/lewis6991/gitsigns.nvim
  52. use {
  53. "nvim-treesitter/nvim-treesitter", -- https://github.com/nvim-treesitter/nvim-treesitter
  54. run = ":TSUpdate"
  55. }
  56. use "nvim-treesitter/nvim-treesitter-textobjects" -- https://github.com/nvim-treesitter/nvim-treesitter-textobjects
  57. use "rhysd/vim-clang-format" -- https://github.com/rhysd/vim-clang-format
  58. use "fatih/vim-go" -- https://github.com/fatih/vim-go
  59. use "SirVer/ultisnips" -- https://github.com/sirver/UltiSnips
  60. use "hrsh7th/cmp-nvim-lsp" -- https://github.com/hrsh7th/cmp-nvim-lsp
  61. use "hrsh7th/nvim-cmp" -- https://github.com/hrsh7th/nvim-cmp
  62. use "neovim/nvim-lspconfig" -- https://github.com/neovim/nvim-lspconfig
  63. use "onsails/lspkind-nvim" -- https://github.com/onsails/lspkind-nvim
  64. use "quangnguyen30192/cmp-nvim-ultisnips" -- https://github.com/quangnguyen30192/cmp-nvim-ultisnips
  65. use "williamboman/nvim-lsp-installer" -- https://github.com/williamboman/nvim-lsp-installer
  66. use "numToStr/Comment.nvim" -- https://github.com/numToStr/Comment.nvim
  67. use { "kylechui/nvim-surround", tag = "*" } -- https://github.com/kylechui/nvim-surround
  68.  
  69. if packer_bootstrap then
  70. require("packer").sync()
  71. end
  72. end)

telescope.lua

  1. local telescope_setup, telescope = pcall(require, "telescope")
  2. if not telescope_setup then return end
  3.  
  4. local actions_setup, actions = pcall(require, "telescope.actions")
  5. if not actions_setup then return end
  6.  
  7. telescope.setup({
  8. defaults = {
  9. mappings = {
  10. i = {
  11. ["<C-k>"] = actions.move_selection_previous, -- move to prev result
  12. ["<C-j>"] = actions.move_selection_next, -- move to next result
  13. ["<C-q>"] = actions.send_selected_to_qflist + actions.open_qflist, -- send selected to quickfixlist
  14. },
  15. },
  16. },
  17. })
  18.  
  19. --- telescope.load_extension("fzf")
  20.  
  21. local builtin = require('telescope.builtin')
  22. vim.keymap.set('n', '<leader>ff', builtin.find_files, {})
  23. vim.keymap.set('n', '<leader>fg', builtin.live_grep, {})
  24. vim.keymap.set('n', '<leader>fb', builtin.buffers, {})
  25. vim.keymap.set('n', '<leader>fh', builtin.help_tags, {})
  26. vim.keymap.set('n', '<leader>fx', builtin.treesitter, {})
neovim.txt · Last modified: 2024/02/06 21:38 by jherrero