Skip to content

Component snippets

shadmansaleh edited this page Nov 13, 2021 · 14 revisions

Here we list various snippets for components that can be useful for users. Most of these are functions that can be used as regular function component in any section of lualine. If you have some nice component snippets that you want to share. You can add proposal to get them added here.

Trailing whitespaces

Shows 'TW:line' in lualine when there are trailing white space in current buffer.

function()
  local space = vim.fn.search([[\s\+$]], 'nwc')
  return space ~= 0 and "TW:"..space or ""
end

Mixed indent

Shows 'MI:line' in lualine when both tab and spaces are used for indenting current buffer.

function()
  local space_pat = [[\v^ +]]
  local tab_pat = [[\v^\t+]]
  local space_indent = vim.fn.search(space_pat, 'nwc')
  local tab_indent = vim.fn.search(tab_pat, 'nwc')
  local mixed = (space_indent > 0 and tab_indent > 0)
  local mixed_same_line
  if not mixed then
    mixed_same_line = vim.fn.search([[\v^(\t+ | +\t)]], 'nwc')
    mixed = mixed_same_line > 0
  end
  if not mixed then return '' end
  if mixed_same_line ~= nil and mixed_same_line > 0 then
     return 'MI:'..mixed_same_line
  end
  local space_indent_cnt = vim.fn.searchcount({pattern=space_pat, max_count=1e3}).total
  local tab_indent_cnt =  vim.fn.searchcount({pattern=tab_pat, max_count=1e3}).total
  if space_indent_cnt > tab_indent_cnt then
    return 'MI:'..tab_indent
  else
    return 'MI:'..space_indent
  end
end

keymap

Shows short name of currently active keymap. See :h keymap

local function keymap()
  if vim.opt.iminsert:get() > 0 and vim.b.keymap_name then
    return '' .. vim.b.keymap_name
  end
  return ''
end

Adding window number

This is quite useful to quickly switch between multiple windows.

local function window()
  return vim.api.nvim_win_get_number(0)
end

require'lualine'.setup {
  sections = {
    lualine_a = { window },
  }
}

Using external source for branch

If you have other plugins installed that keep track of branch info . lualine can reuse that info.

  • vim-fugitive
  lualine_b = { {'FugitiveHead', icon = ''}, },
  • gitsigns.nvim
    lualine_b = { {'b:gitsigns_head', icon = ''}, },

Using external source for diff

If you have other plugins installed that keep track of info. lualine can reuse that info. And you don't need to have two separate plugins doing the same thing.

  • gitsigns.nvim
local function diff_source()
  local gitsigns = vim.b.gitsigns_status_dict
  if gitsigns then
    return {
      added = gitsigns.added,
      modified = gitsigns.changed,
      removed = gitsigns.removed
    }
  end
end

require'lualine'.setup {
  sections = {
    lualine_b = { {'diff', source = diff_source}, },
  }
}

Display EOL fileformat as CRLF

Display the fileformat section as CRLF instead of icons or unix/dos/mac

require'lualine'.setup {
  sections = {
    lualine_x = {
      {
        'fileformat',
        icons_enabled = true,
        symbols = {
          unix = 'LF',
          dos = 'CRLF',
          mac = 'CR',
        },
      },
    },
  },
}
Clone this wiki locally