Skip to content

Instantly share code, notes, and snippets.

@YourAKShaw
Created October 9, 2024 12:35
Show Gist options
  • Save YourAKShaw/819ad666e67346c335b0c32c73e609cc to your computer and use it in GitHub Desktop.
Save YourAKShaw/819ad666e67346c335b0c32c73e609cc to your computer and use it in GitHub Desktop.

To use NeoVim with style, you'll want to configure it for both productivity and aesthetics. Below is a step-by-step tutorial to set up a stylish NeoVim environment.

1. Install NeoVim

First, make sure you have NeoVim installed. You can install it by following the instructions for your system:

  • Linux:
    sudo apt update && sudo apt install neovim
  • macOS:
    brew install neovim
  • Windows: Use [Windows Subsystem for Linux (WSL)] or install NeoVim directly via Chocolatey or Scoop.

2. Install a Plugin Manager (Packer.nvim)

A plugin manager will help you install plugins for customization. We'll use Packer here.

Install Packer:

  1. Open a terminal and run:

    git clone --depth 1 https://github.com/wbthomason/packer.nvim ~/.local/share/nvim/site/pack/packer/start/packer.nvim
  2. Create the init.lua configuration file for NeoVim:

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

3. Configure NeoVim with a Stylish Look

Open the init.lua file (nvim ~/.config/nvim/init.lua) and add the following sections step by step:

Basic Settings

-- Basic Settings
vim.opt.number = true       -- Show line numbers
vim.opt.relativenumber = true  -- Relative line numbers
vim.opt.tabstop = 4         -- Number of spaces for tab
vim.opt.shiftwidth = 4      -- Spaces for auto-indenting
vim.opt.expandtab = true    -- Use spaces instead of tabs
vim.opt.mouse = "a"         -- Enable mouse support
vim.opt.termguicolors = true -- Enable true color support

Install Themes and Plugins with Packer

Add the following configuration for Packer and some stylish plugins:

-- Packer configuration
require('packer').startup(function(use)
  -- Packer can manage itself
  use 'wbthomason/packer.nvim'

  -- Add a theme
  use 'folke/tokyonight.nvim'   -- TokyoNight theme

  -- Fuzzy finder (like VSCode's quick open)
  use 'nvim-telescope/telescope.nvim'

  -- File explorer
  use 'kyazdani42/nvim-tree.lua'

  -- Status line
  use 'nvim-lualine/lualine.nvim'

  -- Autocompletion and Snippets
  use 'hrsh7th/nvim-cmp'  -- Completion plugin
  use 'L3MON4D3/LuaSnip'  -- Snippet engine

  -- LSP and syntax highlighting
  use 'neovim/nvim-lspconfig'  -- Collection of LSP configurations
  use 'nvim-treesitter/nvim-treesitter'  -- Better syntax highlighting
end)

Install Plugins:

In NeoVim, run:

:PackerInstall

Theme and Visual Setup

Next, let's set up the tokyonight theme and configure the status line for a stylish look.

-- TokyoNight theme configuration
vim.cmd[[colorscheme tokyonight]]

-- Lualine status bar setup
require('lualine').setup {
  options = {
    theme = 'tokyonight'
  }
}

-- nvim-tree setup
require('nvim-tree').setup{}

4. Setup File Explorer and Fuzzy Finder

  • nvim-tree (File Explorer): You can toggle the file explorer with <leader>e. Here’s how to configure key bindings:

    vim.api.nvim_set_keymap('n', '<leader>e', ':NvimTreeToggle<CR>', { noremap = true, silent = true })
  • Telescope (Fuzzy Finder): Telescope is a powerful fuzzy finder. You can search files, grep text, and more. Add this to your key bindings:

    vim.api.nvim_set_keymap('n', '<leader>f', ':Telescope find_files<CR>', { noremap = true, silent = true })
    vim.api.nvim_set_keymap('n', '<leader>g', ':Telescope live_grep<CR>', { noremap = true, silent = true })

5. Enable LSP for Intelligent Code Editing

To set up LSP (Language Server Protocol) for code completion and linting, install an LSP server for your language:

  1. Install nvim-lspconfig:

    :PackerInstall
  2. Configure LSP in your init.lua:

    local lspconfig = require('lspconfig')
    
    -- Example for Python
    lspconfig.pyright.setup{}
    
    -- TypeScript LSP configuration
    lspconfig.ts_ls.setup{}
  3. Add key bindings for LSP:

    vim.api.nvim_set_keymap('n', 'gd', '<cmd>lua vim.lsp.buf.definition()<CR>', { noremap = true, silent = true })
    vim.api.nvim_set_keymap('n', 'K', '<cmd>lua vim.lsp.buf.hover()<CR>', { noremap = true, silent = true })

6. Code Autocompletion and Snippets

You can set up nvim-cmp for autocompletion:

-- Setup nvim-cmp
local cmp = require'cmp'

cmp.setup({
  snippet = {
    expand = function(args)
      require('luasnip').lsp_expand(args.body)
    end,
  },
  mapping = {
    ['<Tab>'] = cmp.mapping.select_next_item(),
    ['<S-Tab>'] = cmp.mapping.select_prev_item(),
  },
  sources = {
    { name = 'nvim_lsp' },
    { name = 'luasnip' },
  }
})

7. Additional Plugins for Even More Style

  • Dashboard: A minimalist startup screen.

    use 'glepnir/dashboard-nvim'
  • Git Integration:

    use 'lewis6991/gitsigns.nvim'
  • Indentation Guides:

    use 'lukas-reineke/indent-blankline.nvim'

8. Final Touch: Customizing the Key Bindings

Add some custom key mappings to improve your workflow:

vim.api.nvim_set_keymap('n', '<leader>s', ':w<CR>', { noremap = true, silent = true })  -- Save
vim.api.nvim_set_keymap('n', '<leader>q', ':q<CR>', { noremap = true, silent = true })  -- Quit
vim.api.nvim_set_keymap('n', '<leader>x', ':x<CR>', { noremap = true, silent = true })  -- Save and quit

9. Optional: Use FiraCode Font with Ligatures

If you want stylish fonts with programming ligatures, install FiraCode or another similar font.

  • Install the font from FiraCode GitHub and set it in your terminal emulator.

10. Enjoy Your Stylish NeoVim!

After following these steps, your NeoVim setup will be both stylish and highly functional. Enjoy coding with a modern and elegant experience!

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