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.

2
  • I don't know a thing about zsh, but it's possible that the contents of your .zshcompdump file would help diagnose the issue
    – Jeff Schaller
    Commented Feb 7, 2016 at 17:23
  • What is the output of alias | grep sudo=?
    – cuonglm
    Commented Feb 7, 2016 at 18:35

3 Answers 3

15

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

8
  • I am not familiar with zsh but there is probably a ' missing. Commented Feb 7, 2016 at 13:53
  • 4
    You don't, just use sudo \mkdir ... to disable alias expansion for mkdir.
    – cuonglm
    Commented Feb 7, 2016 at 18:39
  • @cuonglm it turns out that the problem is due to alias mkdir='nocorrect mkdir', why?
    – Alex Jones
    Commented Feb 7, 2016 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
    Commented Feb 7, 2016 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
    Commented Feb 7, 2016 at 19:01
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!

2
  • 3
    Why do you believe that there should be a tab in the alias at all? Commented Jun 21, 2019 at 17:53
  • The tab (or space) after the alias is explained well in this answer
    – wardw
    Commented May 7, 2020 at 17:37
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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .