9

I am using zsh and oh-my-zsh on Arch Linux. I am not able to make directory using mkdir

edward@ArchLinux  ~ $ sudo mkdir -p /samba/raspberry
  [sudo] password for edward: 
  sudo: nocorrect: command not found

I know it has to do something with auto-completion feature of zsh and alias defined but can't figure out.

Alex Jones
  • 6,223
  • 17
  • 51
  • 83

3 Answers3

14

I have this alias alias sudo='sudo 'defined in a file which I sourced at the end of ~/.zshrc file which overwrote alias sudo='nocorrect sudo' which is defined in .oh-my-zsh/lib/correction.zsh

alias sudo='nocorrect sudo' is required by zsh's auto-completion feature to work
More: How to disable autocorrection for sudo [command] in zsh?

But at same time I need alias sudo='sudo ' for aliases of commands following sudo to work
More: Load aliases from .bashrc file while using sudo
Please note alias sudo='sudo ' works for zsh too

So I can either have zsh's auto-completion feature or have aliases (of other commands) while using sudo so I have now disabled zsh's auto-completion feature.

(Hope I am clear and not confusing.)

Alex Jones
  • 6,223
  • 17
  • 51
  • 83
  • I am not familiar with `zsh` but there is probably a `'` missing. – Hauke Laging Feb 07 '16 at 13:53
  • 4
    You don't, just use `sudo \mkdir ...` to disable alias expansion for `mkdir`. – cuonglm Feb 07 '16 at 18:39
  • @cuonglm it turns out that the problem is due to `alias mkdir='nocorrect mkdir'`, why? – Alex Jones Feb 07 '16 at 18:49
  • 1
    `alias sudo='sudo '` make alias expansion for `cmd` when you use `sudo cmd`. `sudo mkdir` expand to `sudo nocorrect mkdir`, which cause the error, because `nocorrect` is a reserved work. `sudo \cmd` disable alias expansion for `cmd`. – cuonglm Feb 07 '16 at 18:57
  • @cuonglm yes, your idea is can be a second choice along with mine, either disable auto-correction or use \ with every command that have alias of the type `alias command='nocorrect command'`. You should add this in answers – Alex Jones Feb 07 '16 at 19:01
  • Another idea would be to wrap mkdir in a script, with something like follows: `if [ $(whoami) = root ]; then mkdir $@; else nocorrect mkdir $@; fi`. – Sparhawk Feb 15 '16 at 06:11
  • This is all very frustrating. Commands should not have completely different behavior because they are run with sudo first. Aliases should just work with sudo. Auto-correction should either be enabled or disabled, and should still work with sudo. Making the behavior change is just bringing in inconsistency and thus inviting trouble. To make things worse, they piled more bad stuff on top of these bad things. Even if auto-correction would not affect anything, sometimes it still needs to be disabled. Of course this has nothing to do with the answer posted here... I guess I should create issues. – still_dreaming_1 Sep 21 '16 at 15:42
  • I thought that maybe if I completely disable autocorrection using `unsetopt correct_all` and `unsetopt correct` that I could use the sudo alias `alias sudo='sudo '` that makes it work with aliases and have it still work with commands like mdkir, but that did not work. – still_dreaming_1 Sep 21 '16 at 16:26
3

Just adding to answer from @edward-torvalds,

In your .aliases file, the use of a Tab might not be visible enough for some. Your alias definition can be written as such for better reading:

alias sudo=$'nocorrect sudo\t'

However, I did have issues with a trailing space, but not a trailing tab.

alias sudo='sudo '
alias sudo='nocorrect sudo '

above aliases resulted in errors, as follows:

~$ which mkdir 
mkdir: aliased to nocorrect mkdir -p -pv

~$ which sudo
sudo: aliased to nocorrect sudo

~$ alias sudo                  
sudo='nocorrect sudo '

~$ sudo mkdir /tmp/foo
sudo: nocorrect: command not found

Therefore, this would work alias sudo='sudo '

but I prefer alias sudo=$'nocorrect sudo\t' in a 1K+ line zshrc, the latter is just too simple ;)

...if anyone can possibly say why, please comment!

mike
  • 31
  • 2
0

A trick to make alias sudo='sudo ' work with nocorrect aliases would be to create this small nocorrect script in your path:

#!/bin/sh
exec "$@"

This will make nocorrect a noop for sudo.