Writing a Welcome Message in xterm.js

How to write a welcome message in an xterm.js terminal: server and client setup for a better connection experience.

Mar 29, 2024 · 1 min read · 155 Words · -Views -Comments · Programming
WebShell
Read with AI

Choose an AI assistant

ChatGPT Open in a new tab with the article link and prompt
Claude Copy the prompt, then open Claude

Open Claude
Trouble reading the article?

If the prompt is missing, copy the full prompt below into your AI assistant. If the AI cannot read the article, paste the visible passage you want to discuss and add your question.

Terminals often display a welcome message on login. How do you change it? While building a WebShell recently, I considered how to implement this.

https://static.1991421.cn/2024/2024-03-29-125309.jpeg

Server‑side configuration

The most direct way is server configuration — for example, add echo 'hello world' to bashrc so it prints after WebShell login.

Server‑side drawbacks:

  1. Adding a welcome line affects all terminal logins, not just WebShell.
  2. It requires modifying server config (e.g., editing bashrc), which is heavier.

xterm.js client‑side approach

Alternatively, implement it on the client with xterm.js. After the terminal connects, run:

term.write("\x1B[34;43mmy github is https://github.com/alanhe421 my github is https://github.com/alanhe421 my github is https://github.com/alanhe421 my github is https://github.com/alanhe421\x1B[0m"

              + '\r\n', () => {
            webshell.sendData('resize', {
              rows: term.rows,
              cols: term.cols,
            });
          });

Explanation:

  1. term.write writes directly to the terminal emulator; it is not echoed back from the server.
  2. Since the server didn’t send this content, it doesn’t know the terminal size. Send rows/cols to avoid misaligned output in subsequent interactions.
Authors
Developer, digital product enthusiast, tinkerer, sharer, open source lover