It is possible to make autocompletion in Powershell work like in bash?

19

9

I love Powershell, but coming from a Linux background there is a few things that is really bugging me. Like for example how the default auto completion works.

For example: Get-PS expands to "Get-PSBreakpoint" which is the first matching command. In bash if there is only one command matching bash will expand to the matching command. If there is several hitting tab once does nothing, double tapping tab will show a list of matching commands but not expanding. Is there a way to make Powershell behave like this?

And one other thing, if i have written one line, and moves the marker back to an earlier command (marker is where the ^ is):

"Get-PSS^ | Remove-PSSession" auto completes to "Get-PSSession^", removing all trailing commands. In bash the trailing commands would still be there unaffected. Is there a way to make Powershell not remove the trailing commands when auto completing?

A bonus would be to get CTRL+L to clear the screen. CTRL+A to go to the beginning of the line, and CTRL+E to go to the end of the line.

Peter Moberg

Posted 2010-11-03T10:11:42.803

Reputation: 291

Luckily we have bash on windows 10 now. – byxor – 2018-02-12T11:39:32.993

I posted an answer to your question but the funny thing is that I arrived on this question because I'm looking for the exact opposite... I want PowerShell style completion in bash :) – Robert S Ciaccio – 2010-11-28T00:24:14.070

PowerShell style completion is available in bash - look for the function menu-complete – Jason Shirk – 2013-11-15T23:20:30.847

Answers

13

Check out PSReadline.

From the article:

This module replaces the command line editing experience in PowerShell.exe. It provides:

  • Syntax coloring
  • Simple syntax error notification
  • A good multi-line experience (both editing and history)
  • Customizable key bindings
  • Cmd and emacs modes (neither are fully implemented yet, but both are usable)
  • Many configuration options
  • Bash style completion (optional in Cmd mode, default in Emacs mode)
  • Bash/zsh style interactive history search (CTRL-R)
  • Emacs yank/kill ring
  • PowerShell token based "word" movement and kill
  • Undo/redo
  • Automatic saving of history, including sharing history across live sessions
  • "Menu" completion (somewhat like Intellisense, select completion with arrows) via Ctrl+Space

I use it daily!

David Mohundro

Posted 2010-11-03T10:11:42.803

Reputation: 1 668

4Once you install PSReadLine, you have to enable bash-style tab completion by adding this to your profile: Set-PSReadlineKeyHandler -Key Tab -Function Complete – brianpeiris – 2016-11-21T06:24:18.120

3PSReadLine is now installed by default on Windows 10. Just put the configuration line that @brianpeiris mentioned into your profile and you're good to go. – Don Cruickshank – 2017-02-05T19:47:18.883

Try using CompleteMenu instead of Complete for a more bash-like experience, it lets you use the arrow keys to navigate the choices – stib – 2017-08-17T03:01:28.223

3

Maybe you should have a look at Powertab, a customizable PowerShell Tab expansion.

harrymc

Posted 2010-11-03T10:11:42.803

Reputation: 306 093

2Thanks! Powertab really makes tab expansion way better. Still, the issue completing something not at the end of the command is there. – Peter Moberg – 2010-11-03T12:22:39.787

2

PowerShell uses the same console features as the venerable Command Prompt; so you have the usual Home/End, and:

  • F3 to bring up the previous command
  • F1 to copy a single character from the previous command
  • F7 popup history window

...but there's no facility to show all possible completions, as far as I know.

njd

Posted 2010-11-03T10:11:42.803

Reputation: 9 743

This is still very limiting compared to good old bash. For example it is really great being able to type ctrl+r (speedsearch) in bash and just start typing and a matching previous command will show up. – Peter Moberg – 2010-11-11T08:24:26.557

2

hi peter just press F8 for something like speedsearch. It completes with the commands that you run previously that start with what you have in the line. The problem with the deletion of the trailing is annoying, I write a suggestion in Connect regarding the same issue. In the main time you could use Powershell ISE, that behave a little better(don't delete the trailing characters).

mjsr

Posted 2010-11-03T10:11:42.803

Reputation: 5 577

2

The old command.exe subsystem is still involved in PowerShell, and forces a lot of unpleasant behaviors. But as of PowerShell v3, the GUI (PowerShell ISE) is now usable, and it includes a much more modern completion experience + colorization.

enter image description here

Jay Bazuzi

Posted 2010-11-03T10:11:42.803

Reputation: 3 780

1

Regular powershell uses cmd as command prompt window. That's why it have a huge legacy (programs relay on behavior of old cmd for ages) that can not be change because of backward comparability.

I recommend you use powershell_ise instead of powershell (it's includes in standard powershell bundle). Version 3.0 (don't sure about previous) support Intellisense from the box. This is very close to bash completion style. Just hit Ctrl+Space instead of Tab. I experience problems with custom completions on PowerTab, but Intellisense works just fine with it. Powershell_ise also preserves trailing commands, have normal select mode, support Ctrl+C, Ctrl+V, Ctrl+A for select entered command, so Ctrl+A, jump to begging of the line, Ctrl+A, to the end (or Home/End).

xvorsx

Posted 2010-11-03T10:11:42.803

Reputation: 111

0

PowerShell uses its own parser for commands, so in a sense it is a different shell than cmd.exe, even though they share the same terminal (gui window).

Because of that difference, PowerShell allows you to override the tab-completion functionality with your own function. I haven't had time to mess with this myself, but you basically override the function tabexpansion in your profile, the same way you can override the prompt function.

I found this article, by Lee Holmes (author of the awesome Windows PowerShell Cookbook), that describes a tabexpansion override that is very similar to what you're looking for. You could probably tweak it to give you exactly what you want.

Robert S Ciaccio

Posted 2010-11-03T10:11:42.803

Reputation: 1 470