Whether you're new to Linux or you've been using it for years, you may have some bad command-line habits. If you can break these habits, you'll stop using the Linux terminal like a beginner and start using it much more efficiently.
Moving through commands with the arrow keys
Tapping can take you far too long
This was something I was very guilty of when I first started using the terminal. If I wanted to edit part of a command, I would tap away with the arrow keys, moving through the command a character at a time until I got to the place that I wanted. If it were a long command, I could be tapping away over and over until I reached the appropriate location.
There are some useful keyboard shortcuts that you should commit to memory, as they can save you a lot of unnecessary tapping. These shortcuts can help you quickly navigate around a long command or delete parts of commands without having to do it one character at a time. Here are some of the most useful:
-
Ctrl+A: jump to the start of a line
-
Ctrl+E: jump to the end of a line
-
Alt+B: move back one word
-
Alt+F: move forward one word
-
Ctrl+W: delete the previous word
-
Alt+D: delete the next word
-
Ctrl+U: delete from the cursor to the start of the line
-
Ctrl+K: delete from the cursor to the end of the line
There are other useful shortcuts, but these are a good starting point. They can help you navigate and edit long commands far more quickly.
These shortcuts may vary depending on the shell you're using . On macOS, Alt/Optionwon't work for these shortcuts by default. You'll need to check the Use Option as Meta Keycheckbox in Terminal > Settings > Profiles > Keyboard.
Re-typing recent commands
The rest is history
Kris Henges / How-To Geek
If you find yourself retyping the same commands over and over, you're doing it wrong. There's a much quicker way to access the most recent commands from your history. This saves you from having to type out long commands all over again, and using the shortcuts above, you can quickly edit a previous command if necessary.
One of the most useful keyboard shortcuts for the Linux terminal is the Up Arrow. Tapping the Up Arrowwill show the last command you entered, and if you continue to tap it, you can cycle through your terminal history until you find the command that you need.
This is especially useful for long and complex commands. Using the history, you don't have to worry about making a typo when typing out the command again from scratch.
Scrolling through history one command at a time
Your arrow keys will thank you
If you already knew about the Up Arrow trick, you may think that it's the only way to access a command from your history. You can then spend a lot of time hammering the Up Arrow to get to the command that you want.
There's a much quicker way to find a specific command if it wasn't one of the most recent ones you've used. Ctrl+Rallows you to search your command history using reverse-i-search. You can then type part of the command you're looking for, and the most recent match will appear.
If it's not the command that you want, press Ctrl+Ragain to see the next most recent matching command, and keep pressing Ctrl+Runtil you find the one you want.
An alternative option is to typehistoryto see a numbered list of your commands . Typing!followed by the command number, such as!996, will run that command.
Ignoring Tab completion
You complete me
If you hate having to type out long paths in the Linux terminal, you're not alone. The good news is that very often, you don't have to. You can use tab completion to do a lot of the work for you.
If you want to change the directory to a folder with a long name, for example, you can typecdand then start to type the name of the folder. Instead of having to type the whole thing, however, if you press Tab, the name of the folder should automatically appear . If it doesn't, there's more than one possible completion for the letters you've typed.
In some shells, pressing Tabagain will list the possible matches. You can then see which combination of letters you need to type to narrow it down to a single option.
Using absolute paths
Relative paths for the win
Typing out the absolute path of a specific file or folder can take a long time, especially if it's buried deep in your folder structure. You can use relative paths to make things easier.
You can use.to represent the current directory and..to represent the parent directory without having to type out the full path. For example, if you're in/home/user/projects/siteand want to change directory to/home/user/projects/app, you don't need to type the full path. You can just usecd ../app.
Relative paths are also useful when working with files. If you're in/home/user/projects/site/cssand want to openindex.htmlfrom the parent folder, you can usenano ../index.htmlrather than typing the absolute path or navigating your way to the relevant folder.
Copying and pasting commands you don't understand
Learn as you go
Dedi Grigoroiu/Shutterstock.com
Perhaps the most important habit to break is copying and pasting commands into the terminal without knowing what they can do. There's always some joker on a forum who recommends using commands such asrm -rfto do something trivial like changing the directory. If you don't know what these commands do, you could end up deleting everything by mistake .
Every time you use a command you've found elsewhere, take the time to look it up and learn what it does before you use it. You'll pick up a lot before you know it.
The terminal isn't your enemy
If you've only ever used a GUI, the terminal can feel a little daunting. It's like some secret language that you don't know, and trying to move the cursor around with your mouse simply doesn't work. Once you get the hang of it, however, you soon find that it's not nearly as scary as it first appears.
