1

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.

Zumo de Vidrio
  • 1,703
  • 1
  • 13
  • 28

2 Answers2

-1

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

Philippos
  • 13,237
  • 2
  • 37
  • 76
  • it works but, is there any way to make linux autocomplete/suggest file name by pressing tab or whatever key? –  Mar 09 '17 at 07: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 09 '17 at 07: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 09 '17 at 07:27
  • Having vi running as sudo I definitely can not make it autocomplete using :e or :edit –  Mar 09 '17 at 20:07
  • But vim does, believe me! What's wrong about vim? – Philippos Mar 09 '17 at 20:10
-3

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

Thomas Dickey
  • 75,040
  • 9
  • 171
  • 268