Hot answers tagged

267

The other answers provide some soft general guidelines based on personal taste, but ignore many pertinent facts that one should consider when deciding between scripts, functions, or aliases. Aliases and Functions ¹ The entire contents of aliases and functions are stored in the shell's memory. A natural consequence of this is aliases and functions can ...


246

An alias should effectively not (in general) do more than change the default options of a command. It is nothing more than simple text replacement on the command name. It can't do anything with arguments but pass them to the command it actually runs. So if you simply need to add an argument at the front of a single command, an alias will work. Common ...


224

You can also prefix a back slash to disable the alias: \ls Edit: Other ways of doing the same include: Use "command": command ls as per Mikel. Use the full path: /bin/ls as per uther. Quote the command: "ls" or 'ls' as per Mikel comment. You can remove the alias temporarily for that terminal session with unalias command_name.


139

MacOS: alias ll='ls -lG' Linux: alias ll='ls -l --color=auto' Stick that in ~/.bashrc.


136

If you're just switching between two directories, you can use cd - to go back and forth.


81

That's what the command command is for. Try command ls This tells the shell to bypass aliases and functions. This way is supported by bash, zsh, and ash/dash.


64

There is a shell variable CDPATH in bash and ksh and cdpath in zsh: CDPATH The search path for the cd command. This is a colon-separated list of directories in which the shell looks for destination directories specified by the cd command. So you can set in your ~/.bashrc: export CDPATH=/Project/Warnest:~/Dropbox/Projects/ds Then cd ...


63

!! is expanded by bash when you type it. It's not expanded by alias substitution. You can use the history built-in to do the expansion: alias sbb='sudo $(history -p !!)' If the command is more than a simple command (e.g. it contains redirections or pipes), you need to invoke a shell under sudo: alias sbb='sudo "$BASH" -c "$(history -p !!)"'


52

The two, no, three, ... Amongst the main obstacles to that are: It's not a valid name for an alias. Bash's online manual: The characters ... and any of the shell metacharacters or quoting characters listed above may not appear in an alias name. (, ), &, | and whitespace are out in Bash 4.4. That particular string is not the only way to write a ...


51

Something else you might try is a tool called autojump. It keeps a database of calls to it's alias (j by default) and attempts to make intelligent decisions about where you want to go. For example if you frequently type: j ~/Pictures You can use the following to get there in a pinch: j Pic It is available of Debian and Ubuntu, and included on a per-...


45

In OS X 10.9.5 since Mavericks (and at least up to El Capitan) you have to add an alias command to your .bash_profile file in your home folder: ~/.bash_profile which is equivalent to your user path at /Users/YOUR_USER_NAME/.bash_profile To see that file in finder you have to activate the display of hidden files (e.g. using the app InVisible). Otherwise ...


41

Try: alias sbb='sudo $(fc -ln -1)' I like actually prefer to name it 'please': alias please='sudo $(fc -ln -1)' Info: fc is a in-built command in the bash shell. that lists, edits and reexecutes commands previously entered to an interactive shell.


40

If it's a small number of directories, you can use pushd to rotate between them: # starting point $ pwd /Project/Warnest/docs # add second dir and change to it $ pushd ~/Dropbox/Projects/ds/test ~/Dropbox/Projects/ds/test /Project/Warnest/docs # prove we're in the right place $ pwd ~/Dropbox/Projects/ds/test # swap directories $ pushd /Project/Warnest/docs ~...


38

I think it's up to each person's taste. For me the logic goes like this: First I try to make an alias, because it's the simplest. If the thing is too complicated to fit in one line, I try to make it a function. When the function starts to grow beyond a dozen of lines I put it in a script. There is really nothing to restrict you from doing something that ...


38

With the assumption that you call vi with the directory as the last argument: vi() { if [[ -d ${!#} ]]; then cd "$@" else command vi "$@" fi }


37

Have a look at lnav, the advanced log file viewer. It can also pretty print various formats. Before: After:


36

I see the below information from here. When using sudo, use alias expansion (otherwise sudo ignores your aliases) alias sudo='sudo ' The reason why it doesn't work is explained here. Bash only checks the first word of a command for an alias, any words after that are not checked. That means in a command like sudo ll, only the first word (sudo) is ...


35

Try complete-alias, which solves this problem exactly. (Disclaimer: I am the author of complete_alias) After install it you can use one generic function to complete many aliases like this: complete -F _complete_alias <myalias1> complete -F _complete_alias <myalias2> complete -F _complete_alias <myalias3> You may want to source the ...


35

This is, how I do it in my .zshrc: if [ -f ~/.zsh/zshalias ]; then source ~/.zsh/zshalias else print "404: ~/.zsh/zshalias not found." fi


34

ls () { case "$PWD" in /) command ls -I test "$@" ;; *) command ls "$@" ;; esac } The above shell function will test the current directory against / and executes the GNU ls command differently according to the outcome of the test. "$@" will be replaced by the command line options and operands on the original command line. We need to use ...


31

I would look in /etc/profile.d/ for the offending alias. You could also do the following to find it: grep -r '^alias COMMAND' /etc This will recursively grep through files looking for a line beginning with alias COMMAND. If all else fails, put this at the end of your ~/.bashrc unalias COMMAND


31

Not a direct answer to your question (since aliases can only be one word), but you should be using git-config instead: git config --global alias.civ commit -v This creates a git alias so that git civ runs git commit -v. Unfortunately, AFAIK there is no way to override existing git commands with aliases. However, you can always pick a suitable alias name ...


30

One difference between the two is that aliases are only a shell feature. Environment variables are inherited by all subprocesses (unless deliberately cleared). The environment variable would be more likely to work even if less is launched indirectly, such as via another shell (e.g. tcsh), man, vim, psql, etc.


29

Aliases are expanded when a function definition is read, not when the function is executed … $ echo "The quick brown fox jumps over the lazy dog." > myfile   $ alias myalias=cat   $ myfunc() { > myalias myfile > }   $ myfunc The quick brown fox jumps over the lazy dog.   $ alias myalias="ls -l"   $ myalias myfile -rw-r--r-- 1 myusername mygroup ...


28

zsh -x 2>zsh.trace exit grep 'alias.*subl' zsh.trace The -x option causes zsh to print out every command that it executes on stderr. Any command that was executed from reading a file has a prefix with the file name and line. So look for the alias definition in the trace file and you'll know where it was defined.


27

Alias solution If you're really against using a function per se, you can use: $ alias wrap_args='f(){ echo before "$@" after; unset -f f; }; f' $ wrap_args x y z before x y z after You can replace $@ with $1 if you only want the first argument. Explanation This creates a temporary function f, which is passed the arguments. Alias arguments are only ...


27

There's a few things you can try: use bash -v to see what lines are being read during shell startup use bash -x to see what commands are being run during shell startup run with only one startup file bash -v The -v option makes bash print each line from every script file it reads as it reads it. Start by running bash -i -v >bash-i.out 2>&1 ...


26

Aliases ls is a command, l and la are most likely aliases which make use of the command ls. If you run the command alias you can find all the aliases on your system. $ alias | grep -E ' l=| la=' This will return all the aliases that match the pattern l=... or la=.... Debugging it further You can also use the command type to see how a particular command ...


26

To execute a command with a specific working directory, one usually does ( cd directory && utility ) The parentheses around the cd ... means that the command(s) therein runs in a subshell. Changing the working directory in a subshell makes it so that the current working directory of the calling shell is not changed, i.e., after having called this ...


Only top voted, non community-wiki answers of a minimum length are eligible