Hot answers tagged

65

!! 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 ...


49

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 ...


48

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.


46

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 ...


44

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 ...


43

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


43

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 ...


39

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


38

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


34

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 ...


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 ...


32

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

.zshrc and .bashrc are script files, not config files, so you "source" the alias file. In Zsh (.zshrc) and Bash (.bashrc) alike: . my_alias will run my_alias and leave its effects in the same environment with the RC files, effectively giving you the aliases in the shell. Of course, your are not limited to aliases either. I use a .shrc that is sourced by ...


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.


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

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 ...


26

If you're on Bash (or another Bourne-like shell), you can use type. type command will tell you whether command is a shell built-in, alias (and if so, aliased to what), function (and if so it will list the function body) or stored in a file (and if so, the path to the file). For more information on a "binary" file, you can do file "$(type -P command)" 2&...


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 ...


23

Use pkill instead: alias killguake='pkill guake' This is a whole lot safer than trying to parse the process table outputted by ps. Having said that, I will now explain why your alias doesn't do what you want, but really, use pkill. Your alias alias killguake="kill -9 $(ps aux | grep guake | head -n -1 | awk '{print $2}')" is double quoted. This means ...


23

Your system (like many Unix systems) does not have an external cd command (at least not at that path). Even if it had one, the ls would give you the directory listing of the original directory. An external command can never change directory for the calling process (your shell)1. Remove the alias from the environment with unalias cd (and also remove its ...


23

An alias can not take arguments and use $@ to access them like that. Alias expansion in bash is a simple text replacement. If you have alias rm ='something something', then using rm file1 file2 would execute something something file1 file2 and if the alias included $@, this would be expanded with the command line arguments of the shell, not of the alias. ...


22

Use a function that tests if you're in / for ls: ls () { if [[ "$PWD" == / ]] then command ls -I test "$@" else command ls "$@" fi } This way, any arguments you pass to ls will still be used. Or: ls () { if [ "$PWD" == / ] then set -- -I test "$@" fi command ls "$@" }


21

A fish alias is actually implemented as a function. To save a function, you need funcsave. So this is the sequence alias foo=bar funcsave foo That creates ~/.config/fish/functions/foo.fish which will then be available in any fish session.


20

strace doesn't run rm -i for the same reason as: echo rm doesn't output rm -i. Aliases are a feature of a few shells to allow some strings to automatically be replaced by another when found in command position. In: alias foo='whatever' foo xxx The shells expands that to: whatever xxx and that undergoes another round of interpretation, in that case ...


20

Aliases are a feature of the shell. Defining an alias creates a new shell command name. It's recognized only by the shell, and only when it appears as a command name. For example, if you type > ff at a shell prompt, it will invoke your alias, but if you type > echo ff the ff is just an argument, not a command. (At least in bash, you can play some ...


19

First find out which complete-function is used for the systemctl command: complete | grep " systemctl$" The output looks like this: complete -F _functionname systemctl Then use: complete -F _functionname sctl To register the function for the completion of your alias. Now, when you type sctl <tab><tab>, the same suggestions as when you type ...


18

To list active aliases, run: alias To see names of all active functions, run: declare -F To see the names and definitions of all active functions, run: declare -f More The information on aliases is also available is a script-friendly format with: declare -p BASH_ALIASES man bash provides more info on the alias builtin: alias [-p] [name[=value] .....


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