Configure Nvim with Lua: The statusline (Lualine) and open tabs (bufferline) (6)

4 min read, Published on Sep 27, 2022

An important piece in our editor/IDE is the statusline bar, where we can see important information such as the mode it is in (insert, normal, visual, etc), Also, we can see what position on the page we are in, the type of file, etc.. By default the status bar shows the file path and cursor position, and the mode. All this can be customized, even create your own plugin. For now, you can start familiarizing yourself with its documentation if you are curious:

  :help statusline

There are some plugins that add more features as a statusline:

Today, we going to test lualine.nvim, because it is written in Lua. But, you can experiment with the others, and use the one you wish.

lualine.nvim, is _a blazing fast and easy to configure Neovim statusline written in Lua_. This plugin have a high performance, you can see here: lualine.nvim: performance

Use lualine.nvim

  1. The first step: we add the plugin lualine.nvim to the plugins.lua file (where we are going to put the plugins to install).
  nvim lua/configs/plugins.lua
  1. We add this and save it:
  -- ...others configs...
  return packer.startup(function(use)
    -- ...others plugins...

    use {
      'nvim-lualine/lualine.nvim',
      requires = { 'kyazdani42/nvim-web-devicons', opt = true }
    }

    -- ...others configs...
  end)
  1. Install the packages:
  :PackerInstall
  1. Later, we create the lualine file.
  nvim lua/configs/lualine.lua
  1. We add this code and save it:
  local ok, lualine = pcall(require, 'lualine')
  if not ok then
    return
  end

  -- git branch
  local branch = {
    'branch',
    icon = ''
  }

  -- diagnostics count from your preferred source
  local diagnostics = {
    'diagnostics',
    sources = { 'nvim_diagnostic', 'nvim_lsp' },
    sections = { 'error', 'warn' },
    symbols = { error = ' ', warn = ' ' },
    colored = false,
    update_in_insert = false,
    always_visible = true
  }

  -- vim mode
  local mode = {
    'mode',
    fmt = function(str)
      return '-- ' .. str .. ' --'
    end
  }

  -- git diff status
  local diff = {
    'diff',
    colored = false
  }

  lualine.setup({
    options = {
      theme = 'gruvbox_dark',
      disabled_filetypes = { "dashboard", 'NvimTree', "Outline" }
    },
    sections = {
      lualine_a = { branch, diagnostics },
      lualine_b = { mode },
      lualine_c = {},
      lualine_x = { diff, "encoding", 'filetype' },
      lualine_y = { 'location' },
      lualine_z = { 'progress' }
    },
    extensions = {'nvim-tree'}
  })
  1. Open the option/init.lua file (where we are going to put the basic options).
  nvim lua/configs/options/init.lua
  1. Add this:
  local options = {
    -- ...others options...
    showmode = false,          -- we don't need show mode because we see in the lualine statusline (Insert, Visual, etc)
    -- ...others options...
  }
  1. To use these settings, we need to call the lualine file in the main file (init.lua)
  nvim init.lua
  • Add this:
  require('configs.lualine')
lualine.nvim have other important feature: the tabline. _You can use lualine to display components in tabline. The configuration for tabline sections is exactly the same as that of the statusline._ But _if you want a more sophisticated tabline you can use other tabline plugins with lualine too_:

In this case, we see nvim-bufferline.lua.

Use nvim-bufferline.lua

  1. The first step: we add the plugin nvim-bufferline.lua to the plugins.lua file.
  nvim lua/configs/plugins.lua
  1. We add this and save it:
  -- ...others configs...
  return packer.startup(function(use)
    -- ...others plugins...

    use {
      'akinsho/bufferline.nvim',
      tag="v2.*",
      requires = 'kyazdani42/nvim-web-devicons'
    }

    -- ...others configs...
  end)
  1. Install the packages:
  :PackerInstall
  1. Later, we create the bufferline file.
  nvim lua/configs/bufferline.lua
  1. We add this code and save it:
  local ok, bufferline = pcall(require, 'bufferline')
  if not ok then
    return
  end

  bufferline.setup({
    options = {
      numbers = 'ordinal',
      offsets = { { filetype = "NvimTree", text = "File Explorer", text_align = 'center' } },
    }
  })
  1. Open the option/init.lua file (where we are going to put the basic options).
  nvim lua/configs/options/init.lua
  1. Add this:
  local options = {
    -- ...others options...
    mouse = "a"  -- mouse support for all modes
    -- ...others options...
  }
  1. Open the keymaps.lua file (where we are going to put the custom keymaps).
  nvim lua/configs/keymaps.lua
  1. Add this:
  -- ...others keymaps...

  -- Move to buffer next or previous (Shift + l or Shift + h)
  keymap("n", "<S-l>", ":bnext<CR>", opts)
  keymap("n", "<S-h>", ":bprevious<CR>", opts)

  -- ...others keymaps...
  1. To use these settings, we need to call the bufferline file in the main file (init.lua)
  nvim init.lua
  • Add this:
  require('configs.bufferline')

And that’s it!! You can use lualine and bufferline!

Directory

We have the directory of folders like this:

nvim
├─ init.lua
└─ lua
    └─ configs
        └─ options
        |   └─ init.lua
        └─ keymaps.lua
        └─ plugins.lua
        └─ impatient.lua
        └─ colorscheme.lua
        └─ nvim-tree.lua
        └─ lualine.lua
        └─ bufferline.lua

Conclusion

In this article we learned how to configure lualine.nvim, a neovim statusline and nvim-bufferline.lua, a neovim tabline. Now you can test other statusline/tabline if you wish. I hope it will be helpful for those who want to experiment.

Resources