Lightline.vim: Mode is not shown, if there is a long branch name or filename

Created on 17 Jul 2017  路  3Comments  路  Source: itchyny/lightline.vim

Is there any way to wrap branch name and filename, leaving mode indicator on the screen?
Some times I have a very long branch names and can't see if I am in the normal or insert modes, here are some screenshots:

picture1
picture2

The same happens on vertical splits.
My vimrc configuration of the lightline is:

let g:lightline = {
                \ 'colorscheme': 'wombat',
                \ 'active': {
                \   'left': [ [ 'mode', 'paste' ], [ 'fugitive', 'filename' ] ]
                \ },
                \ 'component_function': {
                \   'fugitive': 'LightlineFugitive',
                \   'filename': 'LightlineFilename'
                \ }
                \ }
        function! LightlineModified()
                return &ft =~ 'help\|vimfiler' ? '' : &modified ? '+' : &modifiable ? '' : '-'
        endfunction
        function! LightlineReadonly()
                return &ft !~? 'help\|vimfiler' && &readonly ? 'RO' : ''
        endfunction
        function! LightlineFilename()
                return ('' != LightlineReadonly() ? LightlineReadonly() . ' ' : '') .
                \ (&ft == 'vimfiler' ? vimfiler#get_status_string() :
                \  &ft == 'unite' ? unite#get_status_string() :
                \  &ft == 'vimshell' ? vimshell#get_status_string() :
                \ '' != expand('%:t') ? expand('%:t') : '[No Name]') .
                \ ('' != LightlineModified() ? ' ' . LightlineModified() : '')
        endfunction
        function! LightlineFugitive()
                if &ft !~? 'vimfiler' && exists('*fugitive#head')
                        return fugitive#head()
                endif
                return ''
        endfunction
configuration

Most helpful comment

I had same problem when I set LightLine to display file path (expand('%:~:.')) instead of filename (expand('%:t')). Something like this:

let g:lightline = {
      \ 'active': {
      \   'left': [
      \     ['mode', 'paste'],
      \     ['filename', 'readonly', 'modified']],
      \   'right': [
      \     ['lineinfo'],
      \     ['percent'],
      \     ['filetype', 'fileencoding', 'fileformat']] },
      \ 'component_function': {
      \   'filename': 'LightLineFilename' } }

function! LightLineFilename()
  let l:fname = expand('%:t')
  let l:fpath = expand('%')
  return &filetype ==# 'dirvish' ?
        \   (l:fpath ==# getcwd() . '/' ? fnamemodify(l:fpath, ':~') :
        \   fnamemodify(l:fpath, ':~:.')) :
        \ &filetype ==# 'fzf' ? 'fzf' :
        \ '' !=# l:fname ? fnamemodify(l:fpath, ':~:.') : '[No Name]'
endfunction

So I wanted to truncate file path, leaving mode as is. The key is specifying %< in statusline, see :help statusline. But component_function doesn't interpret %-escaped sequence, so I had to use component instead:

let g:lightline = {
      \ 'active': {
      \   'left': [
      \     ['mode', 'paste'],
      \     ['filename', 'readonly', 'modified']],
      \   'right': [
      \     ['lineinfo'],
      \     ['percent'],
      \     ['filetype', 'fileencoding', 'fileformat']] },
      \ 'component': {
      \   'filename': '%<%{LightLineFilename()}' } }

All 3 comments

The answer depends on which part you want to truncate.
If you want to truncate the components on the right hand side, overwrite the components and check the value of winwidth(0).

let g:lightline = {
      \ 'component_function': {
      \   'fileformat': 'LightlineFileformat',
      \   'filetype': 'LightlineFiletype',
      \   'fileencoding': 'LightlineFileencoding',
      \ },
      \ }

function! LightlineFileformat()
  return winwidth(0) > 70 ? &fileformat : ''
endfunction

function! LightlineFiletype()
  return winwidth(0) > 70 ? (&filetype !=# '' ? &filetype : 'no ft') : ''
endfunction

function! LightlineFileencoding()
  return winwidth(0) > 70 ? (&fenc !=# '' ? &fenc : &enc) : ''
endfunction

If you want to truncate the git branch name, configure as follows.

function! LightlineFugitive()
  if &ft !~? 'vimfiler' && exists('*fugitive#head')
    let branch = fugitive#head()
    if len(branch) < 25
      return branch
    endif
    return branch[:10] . ' .. ' . branch[(len(branch)-10):]
  endif
  return ''
endfunction

Well, I don't want to just truncate. I am looking for a way to make mode always stay on screen. I think I need to dig deeper on this issue https://github.com/itchyny/lightline.vim/issues/108
Thank you.

I had same problem when I set LightLine to display file path (expand('%:~:.')) instead of filename (expand('%:t')). Something like this:

let g:lightline = {
      \ 'active': {
      \   'left': [
      \     ['mode', 'paste'],
      \     ['filename', 'readonly', 'modified']],
      \   'right': [
      \     ['lineinfo'],
      \     ['percent'],
      \     ['filetype', 'fileencoding', 'fileformat']] },
      \ 'component_function': {
      \   'filename': 'LightLineFilename' } }

function! LightLineFilename()
  let l:fname = expand('%:t')
  let l:fpath = expand('%')
  return &filetype ==# 'dirvish' ?
        \   (l:fpath ==# getcwd() . '/' ? fnamemodify(l:fpath, ':~') :
        \   fnamemodify(l:fpath, ':~:.')) :
        \ &filetype ==# 'fzf' ? 'fzf' :
        \ '' !=# l:fname ? fnamemodify(l:fpath, ':~:.') : '[No Name]'
endfunction

So I wanted to truncate file path, leaving mode as is. The key is specifying %< in statusline, see :help statusline. But component_function doesn't interpret %-escaped sequence, so I had to use component instead:

let g:lightline = {
      \ 'active': {
      \   'left': [
      \     ['mode', 'paste'],
      \     ['filename', 'readonly', 'modified']],
      \   'right': [
      \     ['lineinfo'],
      \     ['percent'],
      \     ['filetype', 'fileencoding', 'fileformat']] },
      \ 'component': {
      \   'filename': '%<%{LightLineFilename()}' } }
Was this page helpful?
0 / 5 - 0 ratings

Related issues

mckellyln picture mckellyln  路  5Comments

Blosberg picture Blosberg  路  4Comments

mrbig033 picture mrbig033  路  4Comments

Char-Aznable picture Char-Aznable  路  4Comments

codepushr picture codepushr  路  3Comments