Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

What I normally type to edit a file is:

sudo vim Example.php

I'd like to type

sim Example.php

as a shortcut. I know I can setup an alias in the bash config file, but how do I go about specifying the argument in the alias? In this case the argument is the filename.

share|improve this question

migrated from Feb 9 '10 at 22:00

This question came from our site for professional and enthusiast programmers.

Let me answer your question in several parts.

Setting up an alias in Bash

In your .bashrc, put a line that reads as follows:

alias sim="sudo vim"

On some systems (Ubuntu, for example), this line should instead go in your .bash_aliases file, which will automatically be loaded by your default .bashrc.

Note that it is not necessary to deal with the command arguments; these will be tacked onto the end of the sudo vim command automatically.

Editing as superuser

Take everything I just said and throw it out.

It is generally a better idea to use sudoedit instead. sudoedit will automatically make a temporary copy of the file that you can edit without superuser privileges. This means that vim will run as you (instead of as root), and only a brief moment of superuserity is needed after you're done editing the file.

Making sure sudoedit runs vim

On Debianish systems, use sudo update-alternatives --config editor to choose your preferred editor. Otherwise, make sure $EDITOR is set to vim in your .bashrc.

share|improve this answer
export EDITOR=vim is the line in ~/.bashrc for Arch. – Ben Sep 16 '13 at 10:56

You use sudoedit Example.php or sudo -e Example.php instead. If that's invoking an editor other than Vim, then just add an export EDITOR=vim to your bash startup files.

As far as the alias is concerned, jschmier is correct. Aliases are simply expanded and then any arguments are handled normally. If you actually need to process arguments (instead of leaving them in place after the alias is expanded), you'll want to use a function. See the bash documentation for aliases and functions.

share|improve this answer
You should explain why this is better... that it doesn't run vi as root and let the user do lots of other bad-ness. I know this isn't what the user asked, but it is a much better answer to the apparent problem. – Mark Santesson Feb 9 '10 at 17:55
export EDITOR=vim is the line in ~/.bashrc for Arch. – Ben Sep 16 '13 at 10:55

alias sim='sudo vim $1'

share|improve this answer
Arguments aren't handled in aliases. A function should be used if you need to handle arguments specially (i.e., not just use them exactly as they're given to the alias). – jamessan Feb 9 '10 at 17:54
alias test='cat $1|grep config' works fine for me. – bhups Feb 9 '10 at 18:07
@bhups: $1 is null and the filename is being passed as an command-line argument to grep (rather than through the pipe). So then cat is waiting for input from the terminal and you have to press Ctrl-D or Ctrl-C. Try something that does something to the file in the first step, like: grep config $1 | tr c C – Dennis Williamson Feb 9 '10 at 18:58

alias sim='sudo vim' and call as sim Example.php

Bash always reads at least one complete line of input before executing any of the commands on that line. Aliases are expanded when a command is read, not when it is executed.

See the Aliases entry in the Bash Reference Manual for more details.

share|improve this answer

Use a bash function instead. I have a function I use to show the last modification date of a file/folder:

function lm()
    ls -l "$*" | awk '{print $6, $7, $8}'

Try this:

function sim()
    sudo vim "$*"

The $* in bash represents all the command line arguments, i.e. everything after the name of the function.

Then use it like so: $ sim -p Example.php Example2.php

share|improve this answer

not exactly answering the question but there is a nice trick to use when you started to edit the file and only then remembered that you forgot the sudo. you can use :w !sudo tee % to save the file with all your edits.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.