4

Tmux has an option to have the #{pane_current_path} as a name for the current pane in the status line. This is great but has two short falls for me:

  1. $HOME is not displayed as ~.
  2. I only really need the last directory name, not the full path from /. So, for example /home/ook/thingy/src would only be displayed as either src or ~/t/src.

Is there a Tmux command that does this?

Bonus points if it works with powerline as well.

2 Answers 2

4

I'm using something similar to what you want:

setw -g window-status-format "#[underscore]#{?#{==:#{window_panes},1},,+}#[bold]#W#[nobold]:#{=|-24|…;s|$HOME|~|:pane_current_path}"

Stripping that down a bit to focus on the pane_current_path mangling:

"#{=|-24|…;s|$HOME|~|:pane_current_path}"

That replaces the home dir with ~ and removes all but the right-most 24 chars. Done this way, though, I note that the leading ~ is removed for long paths.

For just the leaf of the path:

"#{s|$HOME|~|;s|.*/||:pane_current_path}"

That time I put the ~ replacement first, so that when you're at home it displays ~ rather than <username>.

So to get closer to your ~/t/src example:

"#{s|$HOME|~|;s|/.*/|/…/|:pane_current_path}"

will render as ~/…/src. You should be able to use a modified pattern and a a backref, maybe combined with the #{= truncator, to get closer, but I'm not sure about getting all that right.

1

I tried to find something similar recently, in my case, for setting the window information (for the Tmux command choose-window) to something more informative than the default.

In the man page, the relevant information seems to be in the FORMATS section, which is also used by the status-{left,right} commands:

FORMATS

Certain commands accept the -F flag with a format argument. This is a string which controls the output format of the command. Replacement variables are enclosed in ‘#{’ and ‘}’, for example ‘#{session_name}’. The possible variables are listed in the table below, or the name of a tmux option may be used for an option's value. Some variables have a shorter alias such as ‘#S’, and ‘##’ is replaced by a single ‘#’.

Conditionals are available by prefixing with ‘?’ and separating two alternatives with a comma; if the specified variable exists and is not zero, the first alternative is chosen, otherwise the second is used. For example ‘#{?session_attached,attached,not attached}’ will include the string ‘attached’ if the session is attached and the string ‘not attached’ if it is unattached, or ‘#{?automatic-rename,yes,no}’ will include ‘yes’ if automatic-rename is enabled, or ‘no’ if not.

A limit may be placed on the length of the resultant string by prefixing it by an ‘=’, a number and a colon. Positive numbers count from the start of the string and negative from the end, so ‘#{=5:pane_title}’ will include at most the first 5 characters of the pane title, or ‘#{=-5:pane_title}’ the last 5 characters. Prefixing a time variable with ‘t:’ will convert it to a string, so if ‘#{window_activity}’ gives ‘1445765102’, ‘#{t:window_activity}’ gives ‘Sun Oct 25 09:25:02 2015’. The ‘b:’ and ‘d:’ prefixes are basename(3) and dirname(3) of the variable respectively. A prefix of the form ‘s/foo/bar/:’ will substitute ‘foo’ with ‘bar’ throughout.

In addition, the first line of a shell command's output may be inserted using ‘#()’. For example, ‘#(uptime)’ will insert the system's uptime. When constructing formats, tmux does not wait for ‘#()’ commands to finish; instead, the previous result from running the same command is used, or a placeholder if the command has not been run before. Commands are executed with the tmux global environment set (see the ENVIRONMENT section).

After some attempts, I can't seem to make the built-in sed-like method work with the #{pane_current_path} directory name. Since you can also use any shell command or environmental variables, though, you can simply use some variation of

set status-left "#(basename ${PWD})"

I don't know about Powerline compatibility, since at the moment I'm not using it.

1

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.