Purchase Linux CDs / DVDs / Flash Drives at OSDisc.com

Welcome to Our Community

While Linux.org has been around for a while, we recently changed management and had to purge most of the content (including users). If you signed up before April 23rd please sign up again. Thanks!

What are your favourite aliases?

Discussion in 'Command Line' started by JasKinasis, Jun 5, 2017.

  1. JasKinasis

    JasKinasis Member

    Joined:
    Apr 25, 2017
    Messages:
    67
    Likes Received:
    80
    The idea of this thread is to post your favourite aliases from your .bashrc.

    Section 1 - What is an alias?
    If you already know what an alias is, feel free to skip this section and check out section 2 containing my aliases, or just go ahead and post some of your favourite aliases.

    But if you don't know what aliases are, I will explain. Please read on.....

    Aliases allow you to create convenient shortcuts for common commands - to save you some typing, and/or to prevent you having to remember a lot of different commands/switches etc.

    So if there are any really long commands that you use often. Instead of having to type the whole command, you can open up your .bashrc file and set up an alias instead. Or if it is just something you will be using in the current terminal session, you can just set up a temporary alias using the built-in alias command.

    Whether you set a temporary alias for the current terminal session, or set a permanent one in your .bashrc file. The syntax to set up an alias is:
    Code:
    alias alias_name='/your/commands/and -options -go -here'
    
    As a practical example:
    ls
    is a command that everybody uses in the terminal and there are lots of different options/switches that can be passed to ls that will allow you to see files listed in different ways. If you always want the output to appear in a certain way, you can set your preferences by setting up an alias for ls.

    So if you always want ls to show all files, in the long-list format, with indicators at the end of file-names ( e.g. / for directories, * for executables), with human-readable file-sizes and you always want to see directories listed first; Then you could set the following alias:
    Code:
    alias ls='ls -alFh --group-directories-first'
    
    Now whenever you type ls, the alias will be invoked and you will see the output in your desired format.
    To invoke the original ls without using your alias, you can bypass the alias by typing:
    Code:
    \ls
    
    That will ignore any aliases for ls and will directly run the ls command in /usr/bin/ instead.

    Alternatively, if an alias is no longer required you can use the unalias command to remove it from the current terminal session, or you could remove it from your .bashrc file if it is a permanent one.
    The syntax for the unalias command is:
    Code:
    unalias name_of_alias_to_remove
    


    Section 2 - My favourite aliases:
    OK, now you know what aliases are - here are a few of my favourites:


    Re-read/Re-apply .bashrc
    This is something I use a lot. Whenever I make changes to my .bashrc, I often want to apply them to any terminals I have open. So I have set-up the following alias:
    Code:
    alias reinit='. ~/.bashrc'
    
    After making changes to my .bashrc, I can use the reinit alias to quickly apply the changes to the current terminal.




    Remove backup files
    As a vim user, I find that I often end up accumulating a lot of backup files. So I have set up a couple of aliases which I periodically use to remove unwanted backup files.
    Code:
    alias rmb='find ./ -type f -iname "*~" -delete &'
    alias rmib='find ./ -type f -iname "*~" -exec rm -i {} \;'
    
    rmb will recursively find and delete all files with names that end with the ~ character. Because of the & at the end of the command, find is ran in the background. That way I can continue working in the terminal while the find command finds and removes backup files.

    rmib will recursively find all files with names that end with the ~ character and will interactively ask whether to remove each file it finds. Because this is an interactive command, I don't run it in the background.


    Listen to internet radio-stations in the terminal
    From looking at my avatar-pic, or reading my profile here you've probably guessed that I'm quite heavily into music.... And heavy music at that.

    When working in the terminal I like to be able to listen to a bit of music. And I don't always want to interrupt my flow by firing up a GUI media player. So I've set up aliases for two of my favourite internet radio stations - Hard Rock Hell Radio and Neue Regel Radio:
    Code:
    alias HRH='cvlc http://andromeda.shoutca.st:9254 &> /dev/null &'
    alias NRR='cvlc http://65.60.19.43:8600 &> /dev/null &'
    
    With these aliases in place, I can listen to my favourite stations by issuing the HRH or NRR command in the terminal.

    On execution of one of these aliases:
    cvlc is started in the background and starts streaming the station I want to listen to.

    All text output (error messages etc) is redirected to /dev/null using the &> redirection operator - so the running process never interrupts what I am doing.

    When I'm finished listening, I use the jobs command to get the PID for vlc:
    Code:
    jobs -l
    
    And then I use the kill command to kill it:
    Code:
    kill -15 {PID}
    
    Note: the use of signal 15 (SIGTERM), which politely asks the process to end, rather than signal 9 (SIGKILL) - which will forcefully kill the process. See here for more info...

    At some point, I'll probably get around to setting up an alias for that too! :)

    Anywhoo, those are a few of my favourite aliases. What are yours?
     
    #1 JasKinasis, Jun 5, 2017
    Last edited: Jun 5, 2017
    Rob likes this.
  2. RoseHosting

    RoseHosting New Member

    Joined:
    May 11, 2017
    Messages:
    3
    Likes Received:
    5
    This one is pretty useful, when you forget to 'sudo' your command:
    Code:
    alias oops='sudo $(history -p \!\!)'
    It will run your last command with sudo
     
    Lazydog, JasKinasis and brevis like this.
  3. Rob

    Rob Administrator
    Staff Member

    Joined:
    Oct 27, 2011
    Messages:
    166
    Likes Received:
    448
    Good one - though, you can currently do 'sudo !!' as well. oops is a little shorter, however :)
     
  4. RoseHosting

    RoseHosting New Member

    Joined:
    May 11, 2017
    Messages:
    3
    Likes Received:
    5
    JasKinasis and Rob like this.
  5. Rob

    Rob Administrator
    Staff Member

    Joined:
    Oct 27, 2011
    Messages:
    166
    Likes Received:
    448
    I don't use too many mainly because most of my day is in different servers.. Although, I could set some up on one of the jump hosts!
     
  6. ryanvade

    ryanvade Moderator
    Staff Member

    Joined:
    Apr 24, 2017
    Messages:
    28
    Likes Received:
    23
    As of late...
    Code:
    alias chrome.exe='sl'
     
    JasKinasis and Rob like this.
  7. JasKinasis

    JasKinasis Member

    Joined:
    Apr 25, 2017
    Messages:
    67
    Likes Received:
    80
    So every time you try to fire up chrome, you get a steam locomotive scrolling across the screen? XD

    [​IMG]
     
    brevis and Rob like this.
  8. ryanvade

    ryanvade Moderator
    Staff Member

    Joined:
    Apr 24, 2017
    Messages:
    28
    Likes Received:
    23
    Not exactly. Every time a friend of mine tries to start chrome HE gets the train.
     
    JasKinasis likes this.
  9. JasKinasis

    JasKinasis Member

    Joined:
    Apr 25, 2017
    Messages:
    67
    Likes Received:
    80
    Ha ha - brilliant!
     
  10. JasKinasis

    JasKinasis Member

    Joined:
    Apr 25, 2017
    Messages:
    67
    Likes Received:
    80
    Since making my original post about aliases, I've solved my command-line media player woes.

    Instead of firing off cvlc in my audio-related aliases (and then having to manually kill vlc in the terminal when finished listening) - I've started using cmus - which is actually a really good terminal based media player, which supports internet radio playback. Really lightweight and has a lot of functionality.

    Whenever I'm using my PC, I usually have a few tmux* sessions open in various terminals - one for general stuff and others set up for specific projects/tasks.

    In the script to set up my main, 'general' tmux session - I've added an extra pane running cmus. So whenever I start my primary tmux session, cmus is automatically ran in its own pane.

    To facilitate playback and playlist management from ANY running terminal, I've set up some handy aliases for several cmus-remote commands.
    e.g.
    Code:
    alias stop='cmus-remote -s'
    alias play='cmus-remote -p;cmstat'
    
    etc...
    There are several others, but you get the idea.
    cmstat is a script I wrote in my personal bin/ directory, which uses cmus-remote to get information about the currently playing track and then displays artist/track name information in the terminal.

    I've also added some keybinds to dwm** which bind the {fn} + {media keys} to various cmus-remote commands. Allowing me to control playback when I don't have a terminal in focus - e.g. when viewing a GUI application in another tag***!

    ========================================================================
    NOTES:
    * - tmux is a terminal multiplexer, similar to GNU Screen - allowing you to control multiple terminal sessions from within a single terminal.
    ** - dwm is a simple tiling window manager written in C.
    *** - Tags in dwm are a similar concept to workspaces/virtual desktops - but are not exactly the same.
    ========================================================================

    Another handy alias I've added since my initial post:
    Code:
    alias bashr='vim ~/.bashrc;. ~/.bashrc'
    
    Which allows me to quickly edit my .bashrc file and immediately apply the changes to the current terminal.

    I've set up similar aliases to allow quick edits of configs for other applications like vim, tmux, conky etc etc...

    Basically, I'm a lazy git. I'll do anything to make my life that little bit easier. Even if it only saves me from typing one or two extra characters in the terminal.... Scripts, cron-jobs, aliases, keybinds - whatever it takes - nothing is beneath me! XD
     

Share This Page