1

I have started learning Ubuntu since 1 year..
During this 1 year period I typed a lot of these below texts in gnome-terminal

sudo -H nautilus /foo1/foo2/foo3/foo4/
sudo -H gedit /foo1/foo2/foo3/foo4/

what i am trying to ask is,

Is there a way I can configure like if I
type sn and then press TAB, Autocompletion to sudo -H nautilus
type sg TAB, Autocompletion to sudo -H gedit

I am Using Ubuntu 18.04 & 18.10 with gnome-terminal

Edit: I am not looking to type sn /foo1/foo2/foo3/foo4 or sg /foo1/foo2/foo3/foo4.
looking for sn TAB or sn TAB TAB to see the text on terminal as sudo -H nautilus

pomsky
  • 68,507
PRATAP
  • 22,460
  • 3
    Autocompletion is not done by the terminal emulator (e.g. gnome-terminal) but the shell (e.g. bash, zsh). Please modify the question accordingly, stating which shell you use. – egmont Mar 24 '19 at 09:20
  • Autocompletion is done on a character or partial string of a command. What you're asking is full conversion from 1 to a completely different string of text, so I'm 90% sure this is not possible. What I'd suggest is creating a function or alias. Function and alias names don't allow for numeric names, so use sn and sg for sudo -H nautilus and sudo -H gedit instead. – Sergiy Kolodyazhnyy Mar 24 '19 at 09:33
  • @PRATAP Please see the linked duplicate. The form would be either sn(){ sudo -H nautilus "$@" ; } or function sn(){ sudo -H nautilus "$@" ;} Of course the usage is sn /path/to/directory. Where to put those functions is explained in linked duplicate. I'll provide couple extra links in a minute – Sergiy Kolodyazhnyy Mar 24 '19 at 09:49
  • 1
    To me it sounds like you want text expansion. There is a zsh plugin that supposedly works as you described it, albeit not using TAB but any other keyboard shortcut. – danzel Mar 24 '19 at 10:03
  • @PRATAP I've posted an answer on the linked duplicate that outlines the essential steps. Let me know if you have further questions – Sergiy Kolodyazhnyy Mar 24 '19 at 10:06
  • @PRATAP Alright, I've reopened the post. Like I've mentioned it's 90% likely this is not possible ( or at least not in a simple way). One could use a small hack: use xdotool to basically erase what user typed into the terminal, and overwrite that with the desired command, but it's only going to be available in GUI, and not always in terminal. – Sergiy Kolodyazhnyy Mar 24 '19 at 10:14
  • @PRATAP You're welcome. I try to help however I can. Right now I have to leave the site, so I'll come back to this question later. – Sergiy Kolodyazhnyy Mar 24 '19 at 10:17

1 Answers1

2

In bash, you can simply use aliases to achieve what you want. Just type the alias, then continue providing the arguments with tab completion.

If you need to effectively expand your alias before executing the command, then press the hotkey Ctrl+Alt+e. Thus, after you have typed the name of your abbreviation (= an alias), press that key combination to see the alias expanded.

See here on how to create a permanent alias.

vanadium
  • 88,010
  • I have added a link to an askubuntu question about creating a permanent alias. In particular, you see a section # some more ls aliasses. You could add your aliasses there. – vanadium Mar 24 '19 at 11:38
  • Glad it worked! Indeed, it takes some studying before you grasp it, but once you got it, it is very easy. – vanadium Mar 24 '19 at 12:33