When working with multiple terminal sessions, quickly identifying which process is running in each window becomes critical. By dynamically setting the window title to include the current running foreground process and its arguments, developers, system administrators, and power users can improve their workflows and multitasking.
Our terminal of choice is WezTerm, a powerful cross-platform terminal emulator and multiplexer written in Rust, extensible via Lua, and of course, fully open source.
WezTerm allows dynamic window titles using the format-window-title event which is emitted when the text for the window title needs to be recomputed.
The tricky part was getting the foreground process info from the currently active panel within the currently active tab. Apparently, the pane object that is passed to the event does not include that information and we had to get it in a different way. Check the code below to see how we get it via the main WezTerm object.
Of course you need to set this up in your WezTerm config file properly as detailed in the official docs.
-- Set terminal window title to tabs index, count, and title as well as the
-- tricky to get running process name and its arguments. We had to get the mux
-- object for the full pane object instead of the basic one passed as argument.
wezterm.on('format-window-title', function(tab, pane, tabs, panes, config)
-- Init title with optional tabs info.
local title = ''
if #tabs > 1 then
title = string.format('[%d/%d] ', tab.tab_index + 1, #tabs)
end
-- Init running process name including all its args.
local proc_title = ''
local mux_pane = wezterm.mux.get_pane(tab.active_pane.pane_id)
if mux_pane then
local proc_info = mux_pane:get_foreground_process_info()
if proc_info then
proc_title = table.concat(proc_info.argv, ' ')
end
end
-- Append active tab title if it's not in the start of process title.
local tab_title = tab.active_pane.title
if proc_title:sub(1, #tab_title) ~= tab_title then
title = title .. tab_title .. ' '
end
-- Append process name and args then return.
return title .. proc_title
end)
This was written while developing the main solution above and here we present the lessons learned from it.
The below code was used before we discover we could just get the pane object inside format-window-title handler. The pane object passed in the arguments is a simplified version of the full object and does not support the method called get_foreground_process_info() which is needed.
This was working well but of course the latest version works much better and is improved and it just uses wezterm.mux.get_pane(tab.active_pane.pane_id) to solve the issue we were trying to work around here.
Kept as reference to see how you can use arrays and how to avoid using zero based indices in Lua which is an edge case to always keep in mind.
-- NOTE: Do not use the below code as it is just for reference.
-- The solution presented above delivers better performance and enhanced safety.
local current_title = {}
wezterm.on('update-right-status', function(window, pane)
local info = pane:get_foreground_process_info()
local wid = window:window_id()
if info then
local args = table.concat(info.argv, ' ')
window:set_right_status(args)
current_title['w' .. wid] = args
else
window:set_right_status ''
current_title['w' .. wid] = ''
end
end)
wezterm.on('format-window-title', function(tab, pane, tabs, panes, config)
local wid = tab.window_id
local exec_title = current_title['w' .. wid]
local index = ''
if #tabs > 1 then
index = string.format('[%d/%d] ', tab.tab_index + 1, #tabs)
end
return exec_title .. ' ' .. index .. tab.active_pane.title
end)
Happy coding!
Founder of SOFTKUBE, lead developer, and getting things done addict. Passionate about open source, user interface design, business development, and the tech world.
A small team of experts developing simple, usable, and high-quality web solutions. We blog about business, entrepreneurship, web development, and technology.
Displaying the Current Running Process and its Arguments in the Window Title Bar of WezTerm
How to Inspect Dynamic and Disappearing UI Elements with the DevTools Debugger
Gaining Access to a Legacy Google Apps Account When Phone Verification Fails
Custom Theme Migration from Drupal 9 to Drupal 10
Business Cheat Sheets CLI Code Design Development Downloads Drupal Email Google Apps HID Keyboards Multilingualism Open Source Philosophy PHP Pointing Devices Productivity Quotes Science Security SEO Technology Thoughts Windows Zend Framework