shellunixnpmterminalcommand-prompt

Is it possible to go back and change the last input you entered into a command prompt questionnaire?


If I accidentally entered a typo into a command prompt questionnaire, is there a way to go backwards and edit that input?

For example, when I run npm init, I'm given a bunch questions. If I accidentally write a typo in the package name field and press enter, the questionaire moves on to version:. Is there a way to quickly go back to package name and fix my typo?

enter image description here

My experience with the terminal and npm is limited, but I found out that you cannot undo a command line. However, I'm wonder if you CAN undo an answer to a command prompt, since this is a little different- The command hasn't really run yet until all the prompts are done.

Sat Apr 27 npm Dashie$ npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.

See `npm help json` for definitive documentation on these fields
and exactly what they do.

Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
package name: (npm) oops-typo
version: (1.0.0)

Solution

  • An undo functionality like you are describing would have to be implemented by the developers of the cli tool you are using (in this case, npm).

    I think the best I can offer as a means to fix your mistake on the command line, without having to open an editor and modify the package.json file from there, is to use the json npm package.

    So in the particular case displayed in your question, you would keep going with the install and afterwards you could simply do:

    $ npm i -g json
    $ json -I -f package.json -e "this.name='your package name'"