Using the echo command to remember what you were doing.


Sometimes you'll start typing a complex command line and then realize that you can't execute it now. Perhaps you're in the wrong directory or you forgot to map the drive. It would be a shame to hit ESC and lose that command line.

What I do is edit the line to insert the word "echo" in front, then hit Enter. (Note: This doesn't work if you have command line redirection.)

This prints the command to the screen, where it can be cut/pasted later. Even more, it enters the line into the command history, so a few uparrows will bring it back.

Comments (43)
  1. Aarrgghh says:

    echo foo | bar | grep "this that" > baz

    Hm.

    rem seems less side-effect-prone.

  2. Raymond Chen says:

    "This doesn’t work if you have command line redirection."

  3. Centaur says:

    Actually, with some command processors, rem will be redirected too.

    Also, some command shells (and I mean 4NT) have a key to store the current command line in the history but not execute is.

    Another solution would be to copy the current command line to the clipboard and optionally paste it into a new instance of Notepad, opened with a shortcut key.

  4. Raymond Chen says:

    rem is probably better but I personally use echo.

  5. DrPizza says:

    "Also, some command shells (and I mean 4NT) have a key to store the current command line in the history but not execute is. "

    I’m a long-time 4NT user, but I did not know that. That would be useful. What is the key?

  6. Jerry Pisk says:

    Am I the only one thinking about simply opening up a new command processor? I wish it was mapped to Win+C and wasn’t resused when started with a keyboard shortcut.

    And Keith – I don’t know if you discovered that, somebody blogged about wildcard completion here few days ago (appologies if it was you).

  7. On a bourne shell, I use "#" a lot to do this trick. Works with anything on the line (it’s a true comment, like // in C++)

    I don’t think any version of either command.com or cmd.exe has true comments, unfortunately (as some already noted, REM honors redirections, like ":" on a bourne shell).

  8. runtime says:

    For my batch file comments, I often use : followed by a space instead of REM. It’s shorter and easier on the eyes than paragraphs of REM REM REM. (As far as I understand) this creates anonymous goto label so the "comment" contents is ignored, not executed. :)

  9. Raymond Chen says:

    Ah, the colon – yes this creates a blank "goto" label and redirections on the line are ignored. This is clearly superior to echo but old habits die hard.

  10. Mike Dunn says:

    The 4NT key to put the current line in the history is Ctrl+K. Check out the help page "Command History and Recall" for more.

    OT but I just had to mention something I love in 4NT, clip: It’s a virtual device name representing the clipboard that you can redirect to/from. Try "list clip:" to see the current clipboard contents.

  11. DrPizza says:

    ^K, eh? I’ll have to give that a go. Yeah, clip: is damn useful.

    on *nix I ^U the link and then ^Y to put it back.

  12. DrPizza says:

    link = line

  13. James says:

    "Am I the only one thinking about simply opening up a new command processor? I wish it was mapped to Win+C and wasn’t resused when started with a keyboard shortcut."

    Check out WinKey, which lets you assign your own Windows-key chords:

    http://www.copernic.com/winkey/

  14. Rob Meyer says:

    This is a great tip; it’s also useful on unix for checking out that your wildcard pattern does what you are expecting before a heavy-duty command:

    $ echo rm -rf o*

    rm -rf olddir1 olddir2 oopsdontdeleteme

    Don’t want to do that, so you just back it up and fix the pattern. If it’s right, then remove the echo and fire away.

  15. Jeremy Morton says:

    I usually use REM [I haven’t discovered any cases where redirections mess it up, but I use echo occasionally when I want to verify environment variables in the command line [usually pasted from a batch file]. If I use echo, I make sure to escape any <, > or | characters with ^.

  16. Keith Moore [exmsft] says:

    Jerry — No, I didn’t see the other blog entry; I just "stumbled" into this feature last night. Pretty cool, though.

    runtime — Using the : for comments is bizarre, but interesting. I like. Thanks!

  17. Mark says:

    I like the colon suggestion! I’ll have to try to use that instead of my current solution:

    prepend an ‘a’ to the command-line and ignore the "xxx is not recognised" error you get for "adir", etc.

  18. Aarrgghh says:

    *Sigh*… first you want people to read the whole thing; next you’ll want them to understand it. Where will it end?

    I still like rem better.

  19. Marc Wallace says:

    I sometimes keep a notepad open for all those temporary commands (or, these days, for SQL). The other nice thing about this is you have no limit (number of lines of command history), and it can persist overnight if you don’t get back to it by the end of the day.

    Of course, you can also just hit Enter and Ctrl-C it really quickly. I haven’t had any problems with that. But then again, my hardware is a bit on the old side…

  20. Keith Moore [exmsft] says:

    Instead of just inserting /echo/ at the beginning of a line you want remember, insert /echo "/ instead. Under WinXP, CMD.EXE appears to be smart enough to handle this correctly, even if the command line you’re saving has embedded double-quotes, redirection, etc.

    While we’re on the subject of command-line tricks, I stumbled into a new trick just yesterday.

    I’ve been using CMD.EXE’s file- and directory-completion feature for years. It’s a great time-saver. However, I only *just* discovered that it does The Right Thing with filename wildcards.

    An example:

    From the command prompt, with C: the current directory, type "TYPE *.SYS", then press <TAB> (or whatever your file completion key happens ot be). On my system, this cycles between the IO.SYS, MSDOS.SYS, pagefile.sys, CONFIG.SYS, and hiberfil.sys files.

    This feature *is* documented, but it’s subtle. According to the "CMD /?" output, "…The completion function takes the path string to the left of the cursor appends a wild card character to it IF NONE IS ALREADY PRESENT and builds up a list of paths that match." (emphasis mine).

  21. The Lanman build team used to use the ":" trick all the time in their build scripts, it drove me up the wall before I finally figured out what they were doing.

    My personal favorite use of echo is for building up quick&dirty batch files:

    echo ":foo" > repeatcmd.bat

    echo "%1 %2 %3 %4 %5" >> repeatcmd.bat

    echo "goto foo" >> repeatcmd.bat

    If you know what you’re going to write ahead of time you don’t need to invoke an editor.

  22. Jeremy Morton says:

    I’ve seen many instances of :: at the start of lines in batch files for comments, but my personal favourite for batch files is @REM show it doesn’t show up when running the batch file, and so people who look at it later know it’s a comment. Many people don’t know that ": " or "::" at the beginning of a line is effectively a comment and I want my batch files to be a least a little maintainable.

    you can also hit Ctrl-C to not run the command you are currently typing into the command prompt. It will give you a new prompt, but leave the command on the screen, however it’s not in the command history, so you’ll have to cut and paste it, which is why I prefer prepending the REM in that situation.

  23. Raymond Chen says:

    Of course this work only if the thing you want to repeat is a single word. If you wanted to store an entire command line, then !$ won’t help you. (Doubly so since !$ recalls from the previous line, but presumably you had to save the line because you needed to execute another line in between.)

    It’s still a handy abbreviation – I used it myself back when I used csh.

  24. Pavel Lebedinsky says:

    Speaking of hidden cmd.exe features… My favorite is this one:

    Windows Registry Editor Version 5.00

    [HKEY_CURRENT_USERConsole]

    "EnableColorSelection"=dword:00000001

    (also available as "Enable selection coloring" in Tweak UI).

    On XP it allows you to change the foreground/background colors of selected text by pressing Alt+[0-9] or Ctrl+[0-9]. This is kinda neat but not very useful.

    On Win2003 however you can also search and highlight text with Shift+Alt+[0-9]. This is an incredibly useful feature when debugging with cdb/ntsd (disassemble a bunch of code, highlight all ‘call’ instructions, or do ~*kb and highlight a certain function/module name, etc).

  25. Johan Thelin says:

    I always wonder why the directory and file name completion keys aren’t set from the start. I always set them both to Tab (using TweakUI). After having spent five years working in Unix terminals (at the university) that, and the middle mouse button copy function, are the functions that I miss the most.

  26. Raymond Chen says:

    Compatibility, of course. Somebody might have a macro that hits the TAB key expecting to insert a TAB character.

    "Aw come on, who would be that crazy?"

    Fire up notepad, type the line

    <TAB>echo hi

    Select it and copy it to the clipboard.

    Go to a command prompt, select Paste.

    Hey, it didn’t paste correctly.

    This actually happened to me.

  27. Spyder says:

    On the topic of unix command line repeating, I use !! all the time myself. !! is replaced with the entire previous command line, which is a real help when you accidentally try a root-only command as a normal user.

    A simple "sudo !!" and away you go.

  28. Centaur says:

    DrPizza,

    I didn’t tell the exact key because every key is configurable. And, maybe, I hoped you would read about it in the manual :)

  29. anon says:

    > $ echo rm -rf o*

    > rm -rf olddir1 olddir2 oopsdontdeleteme

    I prefer —

    $ echo o*

    olddir1 olddir2 oppsdontdeleteme

    $ echo ol*

    olddir1 olddir2

    $ rm -rf !$

    "!$" is replaced with the last argument of the previous command line. using it avoids typos, and i generally find that retyping is faster than editing.

  30. DrPizza says:

    "And, maybe, I hoped you would read about it in the manual :) "

    In the past I’ve found the manual to be execrable; it refuses to yield material I know it to contain.

  31. Cooney says:

    What if the cd fails due to a typo? (The directory name is tmp not temp.) Oops, you just deleted your entire root directory!

    Do you mean that, unlike unix, a failure in one command in a compound doesn’t kill the whole thing? cd temp && rm * is perfectly safe in bash.

  32. Dense? says:

    If you have already typed in the command, then you can already copy it to the clipboard, so what’s the point of echo? Just to avoid having to hold down backspace for a second? Or am I just being amazingly dense today?

  33. Raymond Chen says:

    This assumes that cd sets the error code. True for cd but in general the command may have succeeded in a manner you didn’t want. (And I think the "continue only if successful" punctuation in bash is && not &. & means "execute in the background".) And if the intermediate command is actually two or three commands with some decision points in between, the & trick breaks down.

    The clipboard is too ephemeral for me. The intermediate command I want to execute is probably in another window and I’m going to copy it to the clipboard to paste it into the command prompt.

  34. Ebbe Kristensen says:

    You do know that with CMD.EXE you may have more than one command on the same command line, don’t you?

    So instead of writing:

    c:> echo del *.*

    you write:

    c:> cd temp & del *.*

  35. Marc Wallace says:

    Raymond: "Compatibility, of course. Somebody might have a macro that hits the TAB key expecting to insert a TAB character"

    What if it defaulted to another character? I’d be okay with Ctrl-Backspace. It’s even 7-bit. (oddly enough, Windows lets you type Ctrl-Backspaces into the password box when trying to unlock a workstation. I found this out when my Ctrl key got stuck, and letters did nothing while backspace added a character! Haven’t been brave enough to try changing my password to use one, in case it locks me out…)

    And you’re right, it’s "&&". Think "logical and". You sure wouldn’t want a bitwise and. ;-)

  36. HeadlessCow says:

    In win2k if filename completion is turned on, but you didn’t remap the keys to tab you can already use ctrl-d and ctrl-f for folder and filename completion.

  37. Raymond Chen says:

    "cd temp & del *.*"

    This works too, but it is extremely risky. What if the cd fails due to a typo? (The directory name is tmp not temp.) Oops, you just deleted your entire root directory!

  38. Raymond Chen says:

    There’s a keyboard interface (arrow keys) which is impossibly slow. I’m sure there are utilities that let you echo a command line into the clipboard.

  39. Kim Gräsman says:

    Floating off-topic, but does anybody know if there’s a way to use CMD.exe’s QuickEdit *without the mouse*?

    It’d be so nice to be able to use the clipboard on the prompt without resorting to the mouse.

  40. > Floating off-topic, but does anybody know if there’s a way to use CMD.exe’s QuickEdit *without the mouse*?

    In windowed mode of cmd, Yes. Try pressing Alt-Space to open Control-menu of window. Then simply press E to access the quickedit menu.

Comments are closed.