I'm trying to view the contents of files in /var/lib/php5/sessions, directory in which I have no permissions to do ls, so I have to use sudo ls to see all those files with long names, I would like to make tab completion work when I type sudo vi longfilename.

I have tried starting vi and execute :e or :edit but it doesn't work either, when I press tab all that it shows is ^I.

I don't want to use the mouse to copy and paste that long name to execute vi, that's a workaround I don't like.

2 Answers 2


As your sudo authentication take place after completing the command line, I see no way so have sudo rights during typing the command line other than starting the shell with sudo.

Workaround without mouse:

sudo vi `sudo find /var/lib/php5/sessions -name *patternOfLongFile*`

Maybe define an alias sudovi for that

  • it works but, is there any way to make linux autocomplete/suggest file name by pressing tab or whatever key?
    – user202754
    Mar 9, 2017 at 7:19
  • @Ivanzinho You can't make the shell autocomplete for you since it's running "as you" and therefore doesn't have access to the directory listing.
    – Kusalananda
    Mar 9, 2017 at 7:25
  • Hopefully not! The shell shouldn't be able to suggest filenames in unaccessible directories before you authenticate as sudoer. Doing sudo vi and then open the file from vi will work, if your vi supports tab completion (vim does. Please note the different behaviour for multiple matches).
    – Philippos
    Mar 9, 2017 at 7:27
  • Having vi running as sudo I definitely can not make it autocomplete using :e or :edit
    – user202754
    Mar 9, 2017 at 20:07
  • But vim does, believe me! What's wrong about vim?
    – Philippos
    Mar 9, 2017 at 20:10

vi doesn't do tab-completion. vim (and other vi-clones such as elvis, vile) do this. If you happen to be using a machine with vim installed (and if it's not a "minimal" configuration), then you could use vim in your command rather than "vi".

  • Some distributions (e.g., Debian, red hat) install vi as an alias (rh) or symlink (Debian) to vim; so it is equivalent in those cases... Mar 9, 2017 at 8:59
  • That still doesn't make vim "vi", and your comment is not constructive. Mar 9, 2017 at 9:02
  • 4
    Neither is your answer... the difference between vim and vi is small enough for a novice user that it really doesn't matter. Mar 9, 2017 at 9:06
  • 1
    Apparently you're incorrect as well. Otherwise, OP would not have asked the question. Mar 9, 2017 at 9:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy