Lightline.vim: Path to file?

Created on 6 Nov 2014  路  29Comments  路  Source: itchyny/lightline.vim

Is there a way to show the path to a file if outside the current working dir?
Thanks

configuration

Most helpful comment

Absolute path

let g:lightline = {
      \ 'component_function': {
      \   'filename': 'LightlineFilename'
      \ }
      \ }
function! LightlineFilename()
  return expand('%:p:h')
endfunction

Relative path

let g:lightline = {
      \ 'component_function': {
      \   'filename': 'LightlineFilename'
      \ }
      \ }
function! LightlineFilename()
  return expand('%')
endfunction

All 29 comments

Use expand('%:p:h').

That shows only the dir where the file is, not the filename. How to specify full path including filename?

It would seem that expand('%:f') might do the trick though.
Shows only filename if in current dir, and relative path if not in current dir.

Do not write by your guess. There's not such a modifier :f. See :h expand(). The current directory can be obtained by getcwd().

I didn't write my guess, I looked in your code (autoload/lightline.vim) and found this:

 98   call extend(s:lightline.component, {
 99         \ 'mode': '%{lightline#mode()}',
100         \ 'absolutepath': '%F', 'relativepath': '%f', 'filename': '%t', 'modified': '%M', 'bufnum': '%n',
101         \ 'spell': '%{&spell?&spelllang:"no spell"}',
102         \ 'paste': '%{&paste?"PASTE":""}', 'readonly': '%R', 'charvalue': '%b', 'charvaluehex': '%B',
103         \ 'fileencoding': '%{strlen(&fenc)?&fenc:&enc}', 'fileformat': '%{&fileformat}',
104         \ 'filetype': '%{strlen(&filetype)?&filetype:"no ft"}', 'percent': '%3p%%', 'percentwin': '%P',
105         \ 'lineinfo': '%3l:%-2v', 'line': '%l', 'column': '%c', 'close': '%999X X ' }, 'keep')

From there I deduced that %f would give me relative path. Which it did!

Was that a fault assumption?

Hmm, no, apparently it didn't. I was just lucky, since the % did what I wanted. Sorry 'bout that!

I'll rephrase my initial question: Is there a way to get relative path including filename?

They are different.

  • The %f is an item for 'statusline'. The filename is %t and the full path is %F. See :h 'statusline'.
  • But you cannot use statusline's items in expand() function. The file name is expand('%') and the full path is expand('%:p'). See :h expand().
    So your expand('%:f') works by ignoring the invalid modifier :f. Just expand('%') is the correct answer.

Yes, I'm sorry, see my previous comment.

Sorry but I do not understand relative path including filename. Is it different from expand('%')? Do you prefer _always-relative_ path? (might start with something like ../../../../)

After some more experimenting I see that expand('%') probably does exactly what I want.

Sorry for taking up your time and thanks for the explanations.

Okay. No problem.

It worked for me, thank you for all these tips.

From this thread, I've seen that fnamemodify(expand("%"), ":~:.") let me "reduce the path relative to the home folder if possible":

:pwd
/home/josuah/Downloads

filename: ~/.vim/colors/molokai.vim

:cd ~/.vim
:pwd
/home/josuah/.vim

filename: colors/molokai.vim

Wish it helps someone.

I love lightline design allowing very fine tuning with ease. Thank you for your work!

You're welcome. When I started to work on lightline, I tried finding the design with maximum configurability with minimum core scripts. Enjoy improving your configuration better for you.

Thank you. So will I.

Hmmm, so what exactly needs to go in .vimrc to display the path and filename in the lightline status bar?

Absolute path

let g:lightline = {
      \ 'component_function': {
      \   'filename': 'LightlineFilename'
      \ }
      \ }
function! LightlineFilename()
  return expand('%:p:h')
endfunction

Relative path

let g:lightline = {
      \ 'component_function': {
      \   'filename': 'LightlineFilename'
      \ }
      \ }
function! LightlineFilename()
  return expand('%')
endfunction

That's strange. That doesn't change anything... Do I need additional configuration?

No.

Ahh I see, shinchu/lightline-gruvbox.vim seemed to have override the setting

If I'm seeing this right, instead of redefining the filename component function, one could also just use the relativepath component. At least that seems to work for me.

It does make overriding in different scripts more difficult to handle, though.

Ah, yes... relativepath component will work enough...

@aktau can you please provide an example of how to do it without overriding?

let g:lightline = {
      \ 'active': {
      \   'left': [ [ 'mode', 'paste' ], [ 'readonly', 'relativepath', 'modified' ] ]
      \ }
      \ }

@itchyny Thank you :+1:

I use something like this for nicely abbreviated path-

let g:lightline = {
      \ 'component_function': {
      \   'filename': 'LightLineFilename'
      \ }
      \ }
function! LightLineFilename()
    let name = ""
    let subs = split(expand('%'), "/") 
    let i = 1
    for s in subs
        let parent = name
        if  i == len(subs)
            let name = parent . '/' . s
        elseif i == 1
            let name = s
        else
            let name = parent . '/' . strpart(s, 0, 2)
        endif
        let i += 1
    endfor
  return name
endfunction

Outputs:
image

Thanks for the tips everyone.

What I did was set expand('%:p') in the filename component.

screen shot 2017-09-06 at 10 59 40 am

this is working well so far! cheers <3

I use expand('%:F') to show an absolute based from my current working directory, and that seems to work for me in the first buffer, but once I switch to a second buffer, it shows a complete absolute path.
Any idea why?

Edit: This solution works fine:

let g:lightline = {
      \ 'active': {
      \   'left': [ [ 'mode', 'paste' ], [ 'readonly', 'relativepath', 'modified' ] ]
      \ }
      \ }

Thanks you all for the above replies.

Thanks to them, I was able to solve an issue I am having with a codebase lastly where all main files in a component folder are called index.xyz. As you can imagine, it's very confusing having a lot of index files open a trying to understand who is who.

So, I wrote (my very first) VimL function to solve the issue, by showing, in case of an index file, also its latest folder like folderName/index.xyz.

I hope it can be useful for other people having the same issue.

About that, if @itchyny thinks it's a good configuration option to have for the plugin, I can try to make a PR for integrating it.

Finally, that't my first time writing a VimL function, please, feel free to give me feedback about it, I would really appreciate it. I hope it makes sense 馃槃 .

let g:lightline = {
\    'colorscheme': 'dracula',
\    'active': {
\        'left': [ [ 'mode', 'paste' ], [ 'gitbranch', 'readonly', 'filenameOrLastFolderOfIndex', 'modified' ] ]
\    },
\    'component_function': {
\        'gitbranch': 'fugitive#head',
\        'filenameOrLastFolderOfIndex': 'LightLineFixIndexFiles'
\    },
\}
function! LightLineFixIndexFiles()
    let filenameonly = expand('%:t:r')
    if filenameonly ==? "index"
        return remove(split(expand("%:h"), "/"), -1) . "/" . expand("%:t")
    else 
        return expand("%:t")
    endif
endfunction

@itchyny these snippets should be in the readme

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kbc8090 picture kbc8090  路  3Comments

Blosberg picture Blosberg  路  4Comments

wimstefan picture wimstefan  路  5Comments

JIAKanghao picture JIAKanghao  路  4Comments

kiryph picture kiryph  路  3Comments