How to display 'current working directory' in status bar only if it differs from current file path?

21 views
Skip to first unread message

Igor

unread,
Dec 15, 2019, 1:39:03 PM12/15/19
to vim_use
In Vim 8.2 I have two information in my status line:
- full path to my current opened file
- full path to current working directory

I have set the following commands:
set laststatus=2                              " always display status line
set statusline=Opened_file:                   " label
set statusline+=%{resolve(expand('%:p'))}     " display full file path of opened file
set statusline+=\ \                           " two spaces
set statusline+=Current_working_directory:    " label
set statusline+=%{getcwd()}\ \                " display current working directory


Sample status line:
Opened_file:/home/myname/myfile.txt  Current_working_directory:/home/myname

What I would like to do is, to display current_working_directory only if it
is different then opened file.

In above example I would like to have status line (without second part; from above cmd list without last three cmd's):
Opened_file:/home/myname/myfile.txt

But if my current working directory is /opt, then I would like to have displayed
both information:
Opened_file:/home/myname/myfile.txt  Current_working_directory:/opt


Lifepillar

unread,
Dec 15, 2019, 2:45:36 PM12/15/19
to vim...@googlegroups.com
I'd do it as follows:

fun! MyStatusLine()
return "Opened_file: %{resolve(expand('%:p'))}"
\ . "%{getcwd() == resolve(expand('%:p:h'))"
\ . "? ''"
\ . ": ' Current_working_directory:'.getcwd()}"
endf

set statusline=%!MyStatusLine()

When a status line's definition becomes a tad complicated, I prefer to
use a function.

Hope this helps,
Life.


Reply all
Reply to author
Forward
0 new messages