Chapter 7
In This Chapter
historycomplete and uncomplete commands for per-command rules.cshrcYou've covered the heavy machinery now — files, variables, pipes. The remaining ergonomics are smaller but matter just as much. A good prompt, tab completion that does the right thing, and a history you can step through quickly turn cshw from "shell I use" into "shell I enjoy."
This chapter is short on theory and heavy on small wins. By the end you'll have a .cshrc that makes the shell feel a little more yours.
cshw remembers every command you type, both within the current session and across reboots.
The fastest way to use history is the up and down arrow keys at an empty prompt:
If you go past the most recent entry on the way down, you get an empty line back — your fresh prompt.
history commandTo see your history as a numbered list, run history:
history
You'll get something like:
1 pwd
2 cd Projects
3 ls
4 grep TODO *.cpp
5 history
Pass a number to limit how many entries you see — handy when the list is long:
history 20 # last 20 commands
To wipe the whole list and start fresh, history -c:
history -c # clear all history
When cshw exits, it writes your history to %APPDATA%\cshw\history. Next time you launch cshw, that file is read back in, so the up arrow at a fresh prompt recalls what you typed yesterday morning.
You can control how many entries get persisted with the history_max shell variable (defaults to a sensible cap). Set it in your .cshrc if you want a different limit:
set history_max = 5000 # remember more (or fewer) entries
Consecutive duplicate commands are de-duped automatically — if you run ls ten times in a row, history records it once. That keeps the list useful for actually finding things later.
TIP
history | grep something is the right way to find an old command without scrolling through pages of arrow-up. Looking for that one long grep pipeline from two weeks ago? history | grep grep will probably surface it.
TECHNICAL STUFF
On Windows, the F7 key brings up the Windows console's own history popup, separate from cshw's. It works, but it shows the console's view of history, which may differ from cshw's after a session resume. Stick with history and the arrow keys; both are under the shell's control.
TRY IT
Run a handful of commands (anything — pwd, dir, echo hello). Then press up arrow a few times. You should see each recent command come back in turn. Then run history; the numbered list shows everything in order.
Tab completion is one of those features you forget exists until you sit at a shell that doesn't have it, and then you suffer. Pressing Tab while typing tells the shell to finish the current word for you.
Out of the box, Tab completes filenames in the current directory. Type the beginning of a name, press Tab, and:
type RE<Tab> # might complete to "README.md"
cd Doc<Tab> # might complete to "Documents\"
completeDefault filename completion is fine, but it gets in the way when the command you're typing wants something else. cd wants directories (not files). unalias wants the name of an alias. The complete command lets you tell cshw exactly what should fill in for each command's arguments.
The basic syntax is complete COMMAND TYPE, where TYPE is one of:
f — files (the default)d — directories onlyc — commands (anything in your PATH or built into cshw)e — environment variablesa — aliasesv — shell variablesExamples:
complete cd d # cd completes directories only
complete unalias a # unalias completes alias names
complete unset v # unset completes shell variable names
complete unsetenv e # unsetenv completes environment variables
complete which c # which completes command names
After running complete cd d, typing cd Doc<Tab> will only consider directories starting with "Doc", skipping any files with similar names.
complete also accepts glob patterns and alternation, letting you scope completion to a specific file extension or a fixed set of words:
complete source "*.cshw" # source completes .cshw files only
complete git "(add|commit|push|pull|status)" # git completes one of these subcommands
The first rule keeps source <Tab> from suggesting binary files or random other things in the current directory. The second is the start of building command-specific completion for external tools.
Run complete with no arguments to see every rule you've defined:
complete # list all completion rules
To remove a rule, uncomplete COMMAND:
uncomplete cd # back to default file completion for cd
uncomplete source git # remove rules for multiple commands at once
After uncomplete, the named command falls back to filename completion.
TIP
A few high-value complete rules to drop in your .cshrc:
complete cd d
complete pushd d
complete unalias a
complete unset v
complete unsetenv e
complete source "*.cshw"
Those are the ones where the default filename completion is most obviously wrong, and the fixes are small enough to set and forget.
REMEMBER
Custom completion rules don't persist between sessions. Put your complete commands in .cshrc so they're set up every time the shell launches.
TRY IT
Run complete cd d at the prompt. Then type cd and press Tab. You should see only directories suggested — files in the current folder are filtered out. Run uncomplete cd to revert.
Your prompt is the first thing you see every time the shell is ready for input. The default works fine — but a customized prompt that shows you exactly what you want to see is one of those small upgrades that pays back every minute of every session.
cshw ships with $J$S$P$S$G as the default prompt format. That expands to CSHW, a space, the current path, another space, and a >:
CSHW C:\Users\YourName >
If you've ever used PROMPT in cmd.exe, the syntax should feel immediately familiar — cshw uses the same $X mini-language that Windows has shipped with for decades.
Inside a prompt string, $ followed by a letter expands to something dynamic. The full code list:
$P — current path (e.g. C:\Users\You\Projects)$G — > character$L — < character$B — | character$A — & character$Q — = character$F — ) character$C — ( character$S — space$$ — literal $$D — current date$T — current time (with seconds and milliseconds)$N — current drive letter$V — cshw version$W — Windows version$J — the string CSHW$_ — newline (for multi-line prompts)$H — backspace (rarely used)$E — escape character (for ANSI sequences, if you're feeling fancy)Use the prompt command followed by your format string:
prompt $P$G # classic cmd.exe: C:\path>
prompt [$T] $P$G # add a timestamp: [14:30:25] C:\path>
prompt $J$S$P$S$G # cshw's default: CSHW C:\path >
prompt $_$P$_$G # multi-line:
# C:\path
# >
prompt $D $T$_$P$G # date and time first line, path/>second
Run prompt with no arguments to see the current format string:
prompt # displays the current prompt format
The $_ newline code is the one to know if your paths get long. A typical "two-line" prompt that keeps your typing area clean even when you're deep in a folder tree:
prompt $P$_$G$S
That gives you the full path on one line, then > and a space on the next, ready for your command:
C:\Users\You\Projects\some-long-project-name
>
The typing area sits flush left on its own line and never wanders to the right edge of the terminal. Many shell users swear by this layout.
The prompt command sets the format for the current session only. To make your custom prompt apply every time you launch cshw, add the prompt line to your .cshrc:
# in your .cshrc
prompt $J $P $G
That way every new shell wakes up with your preferred prompt already in place.
TIP
Running a development build and want to know at a glance which one you're sitting at? Drop $V into your prompt. prompt cshw $V $P $G gives you something like cshw C Shell for Windows version 1.0.8.4 C:\path >. It's wordy — $V expands to the full version banner, not just the number — but there's no mistaking which build you compiled. If that's too long for daily use, tuck it into a devprompt alias you switch on only while you're hacking on cshw itself.
WARNING
A super-elaborate prompt with date, time, version, weather, and the phase of the moon is fun for about an hour. Then it gets distracting. Most people end up settling on a short, fast prompt that gives them exactly the path and a >. The simpler the prompt, the more your commands stand out — which is what you're actually trying to read.
TRY IT
Run prompt [$T] $P$G. Your prompt now has a timestamp. Run a few commands and watch the time tick forward. When you're tired of it, run prompt $J $P $G to restore the default.
cshw's line editor is the cursor-and-history layer you interact with every time you type at the prompt. Here's what's wired up:
The combination means you can recall an old command with the up arrow, jump to the middle with Left or Home, edit in place, and submit — same flow as bash, zsh, or PowerShell.
What's not yet wired up:
If you have muscle memory for Ctrl+A or Ctrl+W from bash, the closest substitute is Home (for Ctrl+A) and Backspace-until-whitespace (for Ctrl+W). Neither is perfect; both work.
TECHNICAL STUFF
The line editor works directly against the Windows console (legacy cmd window, Windows Terminal, ConPTY hosts) — no terminal-specific escape sequences. That's why it stays consistent across host environments. Richer key bindings and a vi-style mode are on the roadmap but not in the current build.
.cshrcPulling threads from this chapter and Chapter 5, here's a .cshrc that gives you a personalized shell within about a minute of typing:
# ~/.cshrc — example
#
# Friendly prompt with current path on its own line
prompt $P$_$G$S
# History tweaks
set history_max = 5000
# Useful completion rules
complete cd d
complete pushd d
complete unalias a
complete unset v
complete unsetenv e
complete source "*.cshw"
# Aliases (from Chapter 5)
alias ll "dir /a"
alias la "dir /a /b"
alias .. "cd .."
alias ... "cd ..\.."
alias rm "rm /recycle" # soft-delete by default, see Chapter 4
# Make sure my tools are in PATH
setenv PATH "C:\Tools;%PATH%"
# A friendly hello so I know the file ran
echo "Welcome back, $USERNAME."
Save that as %USERPROFILE%\.cshrc (or %APPDATA%\cshw\cshwrc), open a new shell, and you've got a prompt that's yours, history that survives reboots, completion that does the right thing, and aliases for the commands you type most. Plus a small greeting when you start, just because.
If you want to test changes without restarting, source ~/.cshrc re-runs the startup file in your current shell. Edit, source, see the result — much faster than closing the window each time.
That's Part II finished. You can navigate the filesystem, manipulate files, compose pipelines, manage variables, and customize your environment. The shell is now genuinely your shell.
Part III moves from one-liners to scripts — saving sequences of commands into reusable .cshw files, parameterizing them, adding control flow, and turning them into proper little programs. We'll start in Chapter 8 with the absolute basics — what a script file looks like, how to run it, and how to make Windows associate .cshw files with cshw so you can double-click them. By the end of Part III you'll be writing real tools.