Thursday, May 2, 2024
163
rated 0 times [  163] [ 0]  / answers: 1 / hits: 65289  / 2 Years ago, thu, october 20, 2022, 5:16:23

Is there a shortcut to clear the command line when typing in it instead of keeping backspace pressed for a few long seconds?



I ask because I regularly use the Tab key to automatically fill in file names when using bash commands in the Terminal. For example instead of typing cd this_is_a_long_file_name, I will type cd th<Tab>. But then I realize I have filled in the wrong command/filename and I have to hold down backspace to clear everything and repeat the process.


More From » command-line

 Answers
6

Ctrl+U will only clean from the cursor to the beginning of the line, so if the cursor is at the end of the line, all the line will be cleaned, and if not, there will remain the characters after the cursor.



For a complete answer, I will say:




  • Ctrl+U - clear all the current line from the end to the beginning only if the cursor is at the end of the line.




    • You can then recall the cleared line with Ctrl+Y if you need.


  • Ctrl+K - clear all the current line from the beginning to the end only if the cursor is at the beginning of the line.




    • You can then recall the cleared line with Ctrl+Y if you need.


  • Ctrl+W - clear the previous word in the current line. For example if you have typed a command like git diff /path/to/some/file and you want to delete just the last parameter to the command, Ctrl+W is very useful.




    • You can then recall the cleared data using Ctrl+Y if you need.


  • Ctrl+E Ctrl+U - move the cursor to the end of the line and clear all the current line from the end to the beginning.




    • You can then recall the cleared line with Ctrl+Y if you need.


  • Ctrl+A Ctrl+K - move the cursor at the beginning of the line and clear all the current line from the beginning to the end.




    • You can then recall the cleared line with Ctrl+Y if you need.


  • Ctrl+C - my favourite - cancel the current command line, which implies clear all the current line no matter where the cursor is.




    • Disadvantage: you can't recall the cleared line anymore.


  • Alt+Shift+# - comment the current line, keep it in the history and bring up your prompt on a new line.



[#25054] Thursday, October 20, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
arkcker

Total Points: 296
Total Questions: 111
Total Answers: 104

Location: Nepal
Member since Tue, Sep 8, 2020
4 Years ago
;