How to change command line auto completion?

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Post Reply
LMuser+
Level 1
Level 1
Posts: 15
Joined: Sat Dec 09, 2023 9:33 am

How to change command line auto completion?

Post by LMuser+ »

Hi, is there a way to change what command line auto completes when I press tab? When I'm writing python scripts with Vim, I have to run the script with python3 script.py, and when I write pyt and press tab, the command line completes it to python. However, I want it to be completed to python3. How could I do this?

I have looked at the complete command but I don't quite understand how to use it. Could I do this with that command?
Shiva
Level 3
Level 3
Posts: 141
Joined: Thu Jul 07, 2022 11:25 am

Re: How to change command line auto completion?

Post by Shiva »

LMuser+ wrote: Thu Dec 28, 2023 1:28 am Hi, is there a way to change what command line auto completes when I press tab? When I'm writing python scripts with Vim, I have to run the script with python3 script.py, and when I write pyt and press tab, the command line completes it to python. However, I want it to be completed to python3. How could I do this?

I have looked at the complete command but I don't quite understand how to use it. Could I do this with that command?
Hi,
Most script editors (Geany, Kate, ...) have their own in-built "Terminal". Are you running your script from LM's gnome-terminal or from an in-built one ?
This question because when I'm typing pyt in the gnome-terminal and press tab completion, I get python3 which seems to be what you want.
BTW, what's your LM version and desktop ?
User avatar
xenopeek
Level 25
Level 25
Posts: 29615
Joined: Wed Jul 06, 2011 3:58 am

Re: How to change command line auto completion?

Post by xenopeek »

LMuser+ likely is either on an very old version of Linux Mint that still came with Python 2, or python-is-python2 or python-is-python3 is installed. On Linux Mint 21 there is no 'python' command by default, only a 'python3'.
Image
User avatar
axrusar
Level 7
Level 7
Posts: 1515
Joined: Sat Jan 30, 2021 5:30 pm

Re: How to change command line auto completion?

Post by axrusar »

on 20.3 if i tab phy in the terminal i get:

Code: Select all

$python
Python 3.8.10 (default, Nov 22 2023, 10:22:35) 
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 
So it uses phyton3
Linux Mint Una Cinnamon 20.3 Kernel: 5.15.x | Quad Core I7 4.2Ghz | 24GB Ram | 1TB NVMe | Intel Graphics
Image
User avatar
Coggy
Level 5
Level 5
Posts: 642
Joined: Thu Mar 31, 2022 10:34 am

Re: How to change command line auto completion?

Post by Coggy »

The description of package python-is-python3 says "symlinks /usr/bin/python to python3".
You can check this with ls -l /usr/bin/python. If the command python links python3 then it doesn't matter which command you use.
You could perhaps uninstall python-is-python3, but then any old scripts that start with #!/usr/bin/python will fail. I have no idea whether you would hit that problem or not. I'm happy to leave python-is-python3 installed and be aware that both python and python3 do the same thing.

The autocomplete is doing the right thing though - waiting to see which of the two you want. You will not be able to change that, other than removing the legacy python command.
LMuser+
Level 1
Level 1
Posts: 15
Joined: Sat Dec 09, 2023 9:33 am

Re: How to change command line auto completion?

Post by LMuser+ »

I am using LM Cinnamon 21.2 and I have new hardware. I am using LM's gnome-terminal.

I tried python in the terminal and it says:

Code: Select all

$ python
Python 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
ls -l /usr/bin/python gives: /usr/bin/python -> python3

So apparently I can run my python scripts by typing python instead of python3?
Shiva
Level 3
Level 3
Posts: 141
Joined: Thu Jul 07, 2022 11:25 am

Re: How to change command line auto completion?

Post by Shiva »

LMuser+ wrote: Fri Dec 29, 2023 1:08 am So apparently I can run my python scripts by typing python instead of python3?
About tab completion, a double tab (the pending of a double-click with the mouse) gives you all the possible options :
Typing ca tab-tab in the terminal
returns
cal cancel cat
calibrate_ppa capsh catdoc
caller captoinfo catman
cameratopam caribou-preferences catppt
canberra-gtk-play case


and retypes the line to let me continue with typo

Try the same with your pyt command. On my system (LMDE6-Cinnamon) it completes immediately to python3 because there's only one possibility.

To know if python is similar to python3 on your system, you may use the which command returning the full path of the binary involved. On my system :

Code: Select all

which python3
returns /usr/bin/python3

Code: Select all

which python
returns nothing (no binary association).

If the binary is the same for both which python and which python3 on your system, you may safely use one for another.
User avatar
Coggy
Level 5
Level 5
Posts: 642
Joined: Thu Mar 31, 2022 10:34 am

Re: How to change command line auto completion?

Post by Coggy »

LMuser+ wrote: Fri Dec 29, 2023 1:08 am ISo apparently I can run my python scripts by typing python instead of python3?
Yes you can. But if you are writing scripts, you should start them with #!/usr/bin/python3.
The link /usr/bin/python -> python3 is provided so that scripts written calling for #!/usr/bin/python at least get a python interpreter rather than a not found error message. They may not work though, depending on whether they rely on old python2 syntax or names.
It may be worth uninstalling package python-is-python3. If you do happen to find that some scripts still call for python, you could add the missing 3 to them, or reinstall python-is-python3. It really is just a transition convenience, and probably not needed any more.

EDIT:
Oh wow! I just checked on my PC, and I find that script /usr/lib/linuxmint/mintMenu/pointerMonitor.py still calls for /usr/lib/python. That's probably an oversight. Or maybe it's just a library imported by another python program that does call python3, so the #! line is never used.
Also, if you were to install ansible, there are 7444 scripts in /usr/lib/python3/dist-packages/ansible_collections/ that start with #!/usr/lib/python. But I suspect they are all libraries that get called by something else that has already chosen its correct python interpreter.
User avatar
spamegg
Level 14
Level 14
Posts: 5118
Joined: Mon Oct 28, 2019 2:34 am
Contact:

Re: How to change command line auto completion?

Post by spamegg »

Instead of fighting built-in features, just install powerful third-party autocomplete / shell history tools such as Blesh or McFly and be done with it: https://github.com/akinomyoga/ble.sh https://github.com/cantino/mcfly
mikeflan
Level 17
Level 17
Posts: 7162
Joined: Sun Apr 26, 2020 9:28 am
Location: Houston, TX

Re: How to change command line auto completion?

Post by mikeflan »

I would like to try mcfly. How should I install it? The install script (curl)?
User avatar
spamegg
Level 14
Level 14
Posts: 5118
Joined: Mon Oct 28, 2019 2:34 am
Contact:

Re: How to change command line auto completion?

Post by spamegg »

mikeflan wrote: Sun Dec 31, 2023 12:20 pm I would like to try mcfly. How should I install it? The install script (curl)?
Yes, I don't use Homebrew anymore, so I used that instead:
https://github.com/cantino/mcfly#instal ... all-script

Although, after using Ble.sh, I don't have much of a need / use for McFly anymore. I don't have a search-oriented usage habit, I usually start typing the first few letters and Ble.sh completes it for me. If you don't know what to type and feel the need to search then McFly can be very useful.

Also I've been using zsh recently, and they have even more powerful auto-complete plugins in that ecosystem.
mikeflan
Level 17
Level 17
Posts: 7162
Joined: Sun Apr 26, 2020 9:28 am
Location: Houston, TX

Re: How to change command line auto completion?

Post by mikeflan »

Thank you. I did try mcfly and did not find it very useful.
So far I am sticking with the default bash autocomplete.
Post Reply

Return to “Scripts & Bash”