{"id":8559,"date":"2021-10-23T17:22:44","date_gmt":"2021-10-23T17:22:44","guid":{"rendered":"http:\/\/TheNextWeb=1370959"},"modified":"2021-10-23T17:22:44","modified_gmt":"2021-10-23T17:22:44","slug":"9-time-saving-tricks-for-your-command-line","status":"publish","type":"post","link":"https:\/\/www.londonchiropracter.com\/?p=8559","title":{"rendered":"9 time-saving tricks for your command line"},"content":{"rendered":"\n<div><img decoding=\"async\" src=\"https:\/\/img-cdn.tnwcdn.com\/image\/tnw?filter_last=1&amp;fit=1280%2C640&amp;url=https%3A%2F%2Fcdn0.tnwcdn.com%2Fwp-content%2Fblogs.dir%2F1%2Ffiles%2F2021%2F10%2FCommand-line-text-colored-hed.jpg&amp;signature=df985ce021ae65b90c39b79e837bbdef\" class=\"ff-og-image-inserted\"><\/div>\n<p>How do you write great code? By being efficient. If you want to create something awesome, you\u2019ll have to eliminate the time dumps that slow you down. With just a few tricks, you can speed up your work and focus on what matters.<\/p>\n<h2>1. Create aliases<\/h2>\n<p>Every shell comes with a file called <code>~\/.bashrc<\/code> or <code>~\/.bash_profile<\/code>. Which one you\u2019ll use depends on your system. This post explains it in detail.<\/p>\n<p>Now think about commands that you use a lot. Typing them each time is arduous, and you can mess things up if you\u2019re prone to making typos. That is what aliases are there for: They replace your original command by a shortcut.<\/p>\n<pre><code>alias $preferredAlias='$commandToAlias'<\/code><\/pre>\n<p>For example, I have a Dropbox folder that I need to access quite often. As I don\u2019t like typing too many characters, I have this alias in my <code>~\/.bash_profile<\/code>:<\/p>\n<p><code>alias topbox=\"cd ~\/Dropbox\/top-eft\/\"<\/code><\/p>\n<p>Some other useful aliases are:<\/p>\n<p><code>alias mv=\"mv -i\"<br \/>alias cp=\"cp -i\"<\/code><\/p>\n<p>The option <code>-i<\/code> indicates that you\u2019ll be prompted before you overwrite a file. If you\u2019ve ever accidentally overwritten something important, you know what I\u2019m talking about.<\/p>\n<p>If you\u2019re using a lot of aliases, it might make sense to create a separate file. Just pack all your aliases into a new file called <code>~\/.bash_aliases<\/code>. Now, you need to tell your original configuration file where the aliases live. For that, add the following lines to the top of your <code>~\/.bash_profile<\/code> or <code>~\/.bashrc<\/code>:<\/p>\n<p><code>if [ -f ~\/.bash_aliases ]; then<br \/>. ~\/.bash_aliases<br \/>fi<\/code><\/p>\n<p>And you\u2019re done!<\/p>\n<p>Whenever you\u2019ve edited your bash file, make sure to run the relevant of the following two commands:<br \/><code>source ~\/.bash_profile<br \/>source ~\/.bashrc<\/code><\/p>\n<p>This way, you\u2019re telling your system to adhere to your changes from now on. If you open a new window, it will source the file automatically.<\/p>\n<h2>2. Pimp your prompt<\/h2>\n<p>Your command line prompt is the first thing you\u2019ll see when you start working, and probably the last thing you look at before you leave. So it makes sense to tailor it to your preferences.<\/p>\n<p>I\u2019m a minimalist, so I only include the current directory in my prompt. Therefore, in my <code>~\/.bash_profile<\/code> I\u2019ve specified:<br \/><code>PS1='[\\W]$ '<\/code><br \/>Here, the <code>\\W<\/code> refers to the current directory, and PS1 is the variable that refers to the prompt.<\/p>\n<p>Other popular options are:<\/p>\n<ul>\n<li>A time stamp helps you trace back your work. Add <code>\\@<\/code> in your file.<\/li>\n<li>Adding your username and hostname make sense if you\u2019re working on a remote server. Add <code>\\u<\/code> and <code>\/<\/code> or <code>\\h<\/code>.<\/li>\n<li>You could add the shell and the shell version if it\u2019s relevant for your work. Add \\s and \/ or \\v to your file.<\/li>\n<li>A dollar sign $ usually marks the end of the prompt.<\/li>\n<\/ul>\n<p>You can also print your prompt in colors. This could help when you\u2019re generating lots of output (or error messages\u2026) and you want to see where the program started.<\/p>\n<h2>3. Make the most of your history<\/h2>\n<p>Of course you don\u2019t want to type the same commands over and over again. Of course there\u2019s the tab completion \u2014 start typing a command, then hit tab to auto-complete it. But what if want to access your past commands? There are a few options:<\/p>\n<ul>\n<li>The up and down arrows let you navigate through your recent history.<\/li>\n<li>If you want to re-execute your last command, type <code>!!<\/code>.<\/li>\n<li>If you want to re-execute the last command starting with foo, type <code>!foo<\/code>. Assume for example that the last time I used my favorite text editor, I opened my configuration file: <code>vim ~\/.bash_profile<\/code>. The next time, I\u2019ll just type <code>!vim<\/code>.<\/li>\n<li>If you want to access the argument of the previous command, you can use <code>!$<\/code>. Say I just opened my configuration file with vim, but now I want to use a different text editor. Then <code>nano !$<\/code> is enough.<\/li>\n<li>What if you remember the middle part of a long command, but not the first letters? You can search for the command using <code>ctrl+R<\/code> and then typing the letters that you know. Once you\u2019ve found the command, hit <code>enter<\/code> as usual.<\/li>\n<li>If you want to see the last 500-or-so commands that you\u2019ve made, just type history. You can change the number of commands that get stored in the history to, say, one million entries by adding <code>HISTSIZE=1000000<\/code> and <code>HISTFILESIZE=1000000<\/code> to your shell configuration. If you want to remove your entire history, type <code>rm ~\/.bash_history.<\/code><\/li>\n<\/ul>\n<h2>4. Use your environment efficiently<\/h2>\n<p>You\u2019ve already encountered a few environment variables \u2014 <code>PS1<\/code>, <code>HISTSIZE<\/code> and <code>HISTFILESIZE<\/code>. In general, these are variables written in CAPITAL LETTERS that define important properties of the system.<\/p>\n<p>You can access the complete list of them with the set command. Another example (of many) is <code>SHELLOPTS<\/code>. It lists all the programs that are set to \u2018on\u2019 upon startup of your terminal session. A full documentation of the preset variables can be found in the GNU documentation.<\/p>\n<p><code>5. Profit from shell options<\/code><br \/>You can customize your shell in a number of ways with shell options. To display all options, run the following commands in your terminal:<br \/><code>bash -O<br \/>bash -o<\/code><\/p>\n<p>The option <code>-O<\/code> refers to options that are specific to the bash shell, while <code>-o<\/code> refers to all other options.<\/p>\n<p>In the displayed list, you\u2019ll also see whether an option is toggled on or off. You can change the default setting by adding a line in your configuration file. Some handy examples are:<br \/><code># Correct spelling<br \/>shopt -q -s cdspell<br \/># Cd into directory with only its name<br \/>shopt -s autocd<br \/># Get immediate notification of background job termination<br \/>set -o notify<\/code><\/p>\n<p>The first option listed here makes the shell more robust to typing mistakes. The second saves you from typing <code>cd<\/code> every time you want to change your directory. And if you have a lot of background jobs running, you might want to get notified by adding the third option to your environment file.<\/p>\n<h2>6. Find files by name<\/h2>\n<p>Say you\u2019re searching for the file foo.txt, but you have no idea where you\u2019ve put it. Then from your home directory, type:<br \/><code>find . -name foo.txt<\/code><\/p>\n<p>Here, the . stands for the current working directory, and you specify the file name with the option -name. You can also use wildcards. For example, this command will return all files in txt format:<br \/><code>find . -name *.txt<\/code><\/p>\n<h2>7. Search for files by content<\/h2>\n<p>Say you want to search for all occurrences of <code>bar<\/code> in foo.txt. Then grep is your friend:<br \/><code>grep bar foo.txt<\/code><\/p>\n<p>If you want search multiple files, you can add them like this:<br \/><code>grep bar foo.txt foo2.txt foo3.txt<\/code><\/p>\n<p>And if you want to search for bar in all files of the directory dirfoo, you can use the recursive mode:<br \/><code>grep -r bar dirfoo<\/code><\/p>\n<p>For more options, you can check out the <code>manual<\/code> page. Just hit <code>man grep<\/code> in your terminal.<\/p>\n<h2>8. Deal with lots of output<\/h2>\n<p>Sometimes you\u2019ll want to grep for something, but the output is too lengthy to be printed on the screen. That\u2019s where piping with the symbol <code>|<\/code> comes in handy.<\/p>\n<p>Say for example that there are a thousand files in the directory dirfoo that contain bar. You might want to sift through the output and cherry-pick the files that interest you. Then you can type:<br \/><code>grep -r bar dirfoo | less<\/code><\/p>\n<p>This will show the output in a more digestible way. You can then take note of the files you want, and close the display by typing <code>q<\/code>. This will leave your command line interface uncluttered.<\/p>\n<p>You can also use <code>grep<\/code> in the inverse way. Say you want to run a program <code>fooprog<\/code> which produces a lot of output. But you\u2019re only interested in the part that contains <code>bar<\/code>.<\/p>\n<p>To make this output more understandable, you might want to add the three lines preceding each occurrence of bar, and the five lines following it. You can then type:<br \/><code>.\/fooprog | grep -B3 -A5 -i bar<\/code><\/p>\n<p>Here, <code>-B3<\/code> refers to the three lines before bar, and <code>-A5<\/code> to the five after it. This way, you can ensure that you only get meaningful information printed in your terminal.<\/p>\n<h2>9. Move around in text<\/h2>\n<p>You thought your terminal was all about keystrokes? Well, here\u2019s one: you can alt-click in the middle of a line in your terminal. It\u2019s a bit clunky, but it\u2019ll be enough to impress your friends.<br \/>What will save you tons of time, though, are keyboard shortcuts. To get started, I suggest you start adopting the following:<\/p>\n<ul>\n<li>Hop to the beginning of a line with <code>ctrl+a<\/code>.<\/li>\n<li>Hop to the end of a line with <code>ctrl+e<\/code>.<\/li>\n<li>Delete everything from the beginning of the line until the cursor with <code>ctrl+u<\/code>.<\/li>\n<li>Delete everything from the cursor to the end of the line with <code>ctrl+k<\/code>.<\/li>\n<li>You can adopt other shortcuts using the full list of <a href=\"https:\/\/support.apple.com\/guide\/terminal\/keyboard-shortcuts-trmlshtcts\/mac\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Apple\u2019s keyboard shortcuts<\/a>. It works pretty well on a Linux command line, too.<br \/>Of course you can\u2019t learn all keyboard shortcuts at once. I suggest starting with a couple of them and then moving on to the next couple once you\u2019ve mastered the first.<br \/>The bottom line: Get fast, become amazing.<br \/>You\u2019ve learnt how customize your terminal with aliases, the prompt, environment variables and shell options. You can now access its history, sift through vast amounts of files and navigate to the meaningful parts.<br \/>Coding is an ongoing learning process. It\u2019s never too late to get efficient!<\/li>\n<\/ul>\n<p> <a href=\"https:\/\/thenextweb.com\/news\/9-time-saving-tricks-command-line-syndication\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>How do you write great code? By being efficient. If you want to create something awesome, you\u2019ll have to eliminate the time dumps that slow you down. With just a few tricks,&#8230;<\/p>\n","protected":false},"author":1,"featured_media":8560,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/www.londonchiropracter.com\/index.php?rest_route=\/wp\/v2\/posts\/8559"}],"collection":[{"href":"https:\/\/www.londonchiropracter.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.londonchiropracter.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.londonchiropracter.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.londonchiropracter.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=8559"}],"version-history":[{"count":0,"href":"https:\/\/www.londonchiropracter.com\/index.php?rest_route=\/wp\/v2\/posts\/8559\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.londonchiropracter.com\/index.php?rest_route=\/wp\/v2\/media\/8560"}],"wp:attachment":[{"href":"https:\/\/www.londonchiropracter.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=8559"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.londonchiropracter.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=8559"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.londonchiropracter.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=8559"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}