nvim-lualine / lualine.nvim

A blazing fast and easy to configure neovim statusline plugin written in pure lua.
MIT License
5.89k stars 459 forks source link

Mode map for modes #614

Closed aniketgm closed 2 years ago

aniketgm commented 2 years ago

Question: Is there a way to create a mode map for the modes ?

Description: I am using vim-airline and thought to give lualine a try. So, in vim-airline, there is a feature to set a map for different modes like this:

" let g:airline_mode_map = {
"   \ '__'     : '-',
"   \ 'c'      : 'C',
"   \ 'i'      : 'I',
"   \ 'ic'     : 'I',
"   \ 'ix'     : 'I',
"   \ 'n'      : 'N',
"   \ 'multi'  : 'M',
"   \ 'ni'     : 'N',
"   \ 'no'     : 'N',
"   \ 'R'      : 'R',
"   \ 'Rv'     : 'R',
"   \ 's'      : 'S',
"   \ 'S'      : 'S',
"   \ 't'      : 'T',
"   \ 'v'      : 'V',
"   \ 'V'      : 'V',
"   \ }

This helps me keep things simple in status bar. I checked the lualine help couldn't find any. Is there something similar available in lualine that I'm not aware ? This is how it looks with the vim-airline mode_map: dummy2

shadmansaleh commented 2 years ago

Once you've a mode map there's really not much you need to have your own mode component that's why lualine doesn't have a separate option for mode map .

For example you can just use a function like this for your mode

local mode_map = {
  ['n']    = 'NORMAL',
  ['no']   = 'O-PENDING',
  ['nov']  = 'O-PENDING',
  ['noV']  = 'O-PENDING',
  ['no'] = 'O-PENDING',
  ['niI']  = 'NORMAL',
  ['niR']  = 'NORMAL',
  ['niV']  = 'NORMAL',
  ['nt']   = 'NORMAL',
  ['v']    = 'VISUAL',
  ['vs']   = 'VISUAL',
  ['V']    = 'V-LINE',
  ['Vs']   = 'V-LINE',
  ['']   = 'V-BLOCK',
  ['s']  = 'V-BLOCK',
  ['s']    = 'SELECT',
  ['S']    = 'S-LINE',
  ['']   = 'S-BLOCK',
  ['i']    = 'INSERT',
  ['ic']   = 'INSERT',
  ['ix']   = 'INSERT',
  ['R']    = 'REPLACE',
  ['Rc']   = 'REPLACE',
  ['Rx']   = 'REPLACE',
  ['Rv']   = 'V-REPLACE',
  ['Rvc']  = 'V-REPLACE',
  ['Rvx']  = 'V-REPLACE',
  ['c']    = 'COMMAND',
  ['cv']   = 'EX',
  ['ce']   = 'EX',
  ['r']    = 'REPLACE',
  ['rm']   = 'MORE',
  ['r?']   = 'CONFIRM',
  ['!']    = 'SHELL',
  ['t']    = 'TERMINAL',
}

require('lualine').setup({
  sections = {
    lualine_a = {function ()
        return mode_map[vim.api.nvim_get_mode().mode] or "__"
    end},
  }
})

Though if I understand correctly all you want is for lualine to show just first character of mode . There's a easier way to do that . In lualine you can apply fmt option to any component. In the fmt option you can manipulate the components result as you like . For example you can show just first character of mode just with this.


require('lualine').setup({
  sections = {
    lualine_a = { {'mode', fmt = function(res) return res:sub(1,1) end} },
  }
})
aniketgm commented 2 years ago

fmt = function(res) return res:sub(1,1) end

Is this a lualine feature or a lua feature altogether ? coz this is really cool. Cool in the sense, for as simple as shortening a mode name, a small function did the whole trick instead of defining the whole mode_map, which I was doing earlier. Thanks. P.S. I don't know lua. I'm still learning.

shadmansaleh commented 2 years ago

Is this a lualine feature or a lua feature altogether ?

Using fmt to manipulate component results in lualine feature the string substring function used in this case is part of lua standered library.

P.S. I don't know lua. I'm still learning

You should take a look at nvim-lua-guide if you haven't already it's a great place to get started with lua in neovim . Also lua x in y minutes page is nice to familiarize with the syntax.

I believe this issue is resolved.

rafgugi commented 1 year ago

I use this

local mode_map = {
  ['NORMAL'] = 'N',
  ['O-PENDING'] = 'N?',
  ['INSERT'] = 'I',
  ['VISUAL'] = 'V',
  ['V-BLOCK'] = 'VB',
  ['V-LINE'] = 'VL',
  ['V-REPLACE'] = 'VR',
  ['REPLACE'] = 'R',
  ['COMMAND'] = '!',
  ['SHELL'] = 'SH',
  ['TERMINAL'] = 'T',
  ['EX'] = 'X',
  ['S-BLOCK'] = 'SB',
  ['S-LINE'] = 'SL',
  ['SELECT'] = 'S',
  ['CONFIRM'] = 'Y?',
  ['MORE'] = 'M',
}
require('lualine').setup({
  sections = {
    lualine_a = { {'mode', fmt = function(s) return mode_map[s] or s end} },
  }
})