How can I clear the current line of the Windows command prompt?

303

Solution 1

The Escape (Esc) key will clear the input line.

In addition, pressing Ctrl+C will move the cursor to a new, blank line. This may be helpful as the input you just reviewed remains visible while you type the new command.

Solution 2

Aside from the two that Myyrddin covered - Esc and Ctrl+C - there are also two more shortcuts related to clearing the current input in CMD.

Ctrl+Home will clear all characters in the input before the cursor (equivalent to Ctrl+U in Bash)

Ctrl+End will clear all characters in the input after the cursor (equivalent to Bash's Ctrl+K)

Both are fairly useful and, once internalised and gotten used to, could speed up editing in CMD by quite a bit.

Share:
303

Related videos on Youtube

Sundar_Mob
Author by

Sundar_Mob

Updated on September 18, 2022

Comments

  • Sundar_Mob
    Sundar_Mob over 1 year

    Crashing while creating an instance of URLSessionTask with the completion handlers

    func sessionTaskPostRequest (_ urlRequest : URLRequest , responseHandler: @escaping  ResponseHandler) -> URLSessionTask {
        // 5
         let sesstionTask : URLSessionTask = networkSession.dataTask(with: urlRequest, completionHandler: { (data : Data? , urlResponse : URLResponse? , error : NSError? )     in
    
            var json: NSDictionary!
            do {
                json = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions()) as? NSDictionary
            } catch {
                print(error)
            }
    
            // Did the JSONObjectWithData constructor return an error? If so, log the error to the console
            if(error != nil) {
                responseHandler (false , nil , error , nil)
            }
            else {
                // The JSONObjectWithData constructor didn't return an error. But, we should still
                // check and make sure that json has a value using optional binding.
                if let parseJSON = json {
                    let errorJSON = parseJSON ["Err"] as! String
                    if !errorJSON.isEmpty {
                        responseHandler (false , nil , nil , errorJSON)
                    }else {
                        responseHandler (true , parseJSON , nil , nil)
                    }
                    print("Succes: \(parseJSON)")
                }
                else {
                    // Woa, okay the json object was nil, something went worng. Maybe the server isn't running?
                    let jsonStr = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
                    responseHandler (false , nil , error , "Error could not parse JSON")
                    print("Error could not parse JSON: \(jsonStr)")
                }
            }
        } as! (Data?, URLResponse?, Error?) -> Void)
    
        return sesstionTask
    
    }
    

    And created a type alias for response handler that returns the response JSON Object . Type alias as follows

    typealias ResponseHandler = (_ successFlag :Bool , _ data : NSDictionary? , _ errorObject : NSError? , _ errorString : String?) -> Void
    
    • barlop
      barlop about 12 years
      @myrddim has it but that aside, some poor alternatives are keep tapping up until you have something short then hold backspace. another one is make the command be a bad command or filename, or rather, an "'sdfd' is not recognised as an internal or external command" so move the cursor a bit with left arrow and just tap a letter or don't and just tap a letter at the end, mess up the cmd input, then hit ENTER, get that error and the next line will be blank.
    • iglvzx
      iglvzx about 12 years
      @barlop That is indeed a poor workaround! It only takes one keystroke. If you can't use the Esc key, you could use AutoHotkey to simulate it.
    • Avt
      Avt over 7 years
      In what line do you have a crash
    • Sundar_Mob
      Sundar_Mob over 7 years
      } as! (Data?, URLResponse?, Error?) -> Void) in this line
  • iglvzx
    iglvzx about 12 years
    And if you want to clear the whole screen, you can use the cls command. :)
  • Milo Wielondek
    Milo Wielondek over 11 years
    As a side note, on UNIX it's ctrl+u.
  • Myrddin Emrys
    Myrddin Emrys over 11 years
    Which would be relevant if the question wasn't specifically targeted at the Windows 7 command prompt. :-) Also, I believe this is shell specific, as I am reasonably certain some shells have the same behavior as Windows when pressing Ctrl+C.
  • FutuToad
    FutuToad almost 10 years
    and how do you remove all the text so you can't scroll up?
  • Myrddin Emrys
    Myrddin Emrys almost 10 years
    @FutuToad the clear command will do what you ask.
  • CJ7
    CJ7 almost 8 years
    @MyrddinEmrys on Windows it is cls
  • SexyBeast
    SexyBeast almost 8 years
    Note: the CTRL+C command will also exit any command line session you are having, i.e python, and return you back to the command line environment.
  • Myrddin Emrys
    Myrddin Emrys almost 8 years
    @AttitudeMonger That is a common command line utility behavior, but is not actually a feature of the command line itself. Many command line utilities do not exit on receipt of the Ctrl+C keystroke. It will not exit any command, but it is accurate to say it exits many commands.
  • SexyBeast
    SexyBeast almost 8 years
    Oh I see. Thanks, I did not know that!
  • Ervin
    Ervin over 7 years
    I use Ctrl + backspace to clear the current line, or ctrl + Home, if you press just home you go to the start of the line
  • Myrddin Emrys
    Myrddin Emrys over 7 years
    I didn't know about these keystrokes; thank you. May you get the upvotes you deserve.
  • uranusjr
    uranusjr almost 7 years
    Same here. Being used to Bash I always wanted those. OP is my savior.
  • balki
    balki almost 6 years
    Is it possible to set it to ctrl+u like in windows. I have to lift my hand to reach esc
  • Scott - Слава Україні
    Scott - Слава Україні over 5 years
    From bash(1): (Ctrl)+(X), (Rubout) is defined to be backward-kill-line; i.e., kill backward to the beginning of the line; i.e., the same as (Ctrl)+(Home) in Windows CMD. “Rubout” may be Backspace or Delete, so try (Ctrl)+(X), (Backspace) and (Ctrl)+(X), (Delete). Also, (Ctrl)+(U) is unix-line-discard; i.e., kill backward from the insertion point (cursor) to the beginning of the line; i.e., also the same as (Ctrl)+(Home).  (Ctrl)+(K) is defined to be kill-line; i.e., kill the text from the insertion point to the end of the line; i.e., the same as (Ctrl)+(End) in Windows CMD.
  • Scott - Слава Україні
    Scott - Слава Україні almost 5 years
    Just to be clear: (Ctrl)+(U) is a long-standing, general feature of Unix.  It works in old, non-Linux, non-GNU, non-bash systems; it works in vi; it works if you do cat > file123.  The others are specific to the bash command line.
  • ennth
    ennth over 3 years
    ctrl+u on mac, thank you!
  • Admin
    Admin about 2 years
    Shift+Home and Shift+End selects the same as above, instead of deleting it.