![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | TabExpansion for... command history. And issues... TabExpansion for... command history. And issues... I use very long command history, my profile imports previously exported commands and I finish interactive PowerShell sessions by a wrapper of 'exit' which exports commands. As a result I keep more than a thousand commands and quite often I effectively reuse very old of them. Right, I do need a solution for easy and convenient command search/extraction. Windows console command history tools (F7, F8) are very primitive and, worse, they work only on commands of the current session. Sure, I composed a function which searches the history for a given pattern and displays matched commands. But after that I have to take a mouse, select, copy and paste a command, leave a mouse and press enter... The solution is still not good, especially I hate a mouse in this operation (there is also a keyboard only way but it is even more ugly). Finally I have got an idea that it would be quite natural and convenient to use TabExpansion for that. OK, it is easy: I created my own function TabExpansion which is a copy of built-in one (Get-Content function:TabExpansion), slightly extended it and added to my profile. The idea: PS> '<pattern>&[tab] PS> <pattern without spaces>&[tab] where <pattern> is a standard wildcard pattern (* is always appended to the end internally). I use the ampersand indicator to avoid conflicts with expansion of file system paths (they don't contain &) and most of the other tab expansion cases (correct me if I miss something serious). Examples: PS> &[tab] - just all commands PS> get&[tab] - commands starting with 'get' PS> '*| group&[tab] - commands containing '| group' The code below should be inserted next line after "switch -regex ($lastWord)": ----- 8< ----- # Handle command history # E.g. '<pattern>&[tab] or <pattern>&[tab] '(.*)&$' { $pattern = $matches[1] + '*' $history = Get-History -Count $MaximumHistoryCount | % { if ($_.CommandLine -like $pattern) { $_.CommandLine } } for($i = $history.Count; $i-- {$history[$i] } break } ----- 8< ----- This works *almost* perfectly. This is *almost* what I need so much. Why almost? Commands usually contain spaces. It turned out that tab expansion guts treat such cases like this (note extra ' and &): Case 1: PS> '<pattern>&[tab] Expanded: PS> '<command>' Case 2: PS> <pattern>&[tab] Expanded: PS> & '<command>' I do understand that I am trying to use a tool for what it is not designed. As for the case 1 I even understand what PowerShell does. To be honest I have no idea what PowerShell does in the case 2. Could anybody tell or better give an example when it is used effectively? This solution is still attractive to me even with all these oddities. Yes, I remove unwanted ' and & manually every time and still think this way is quite effective. The question is: do I have a chance to change this unwanted results of tab expansion? Or at least to fool tab expansion somehow? P.S. In fact I use not a built-in TabExpansion but a function from TabExpansion.ps1 http://www.codeplex.com/SourceContro...tName=PsObject. Take a look, it's cool. P.S. Quite dirty trick how to heal the mentioned problems is below. I do not like this, but one can find this technique useful for something else. The trick also uses TabExpansion but actually does nothing for it; instead it sends few keystrokes to the current console window... 'Input': PS> '<command>'^[tab] PS> & '<command>'^[tab] 'Result': PS> <command> The code below should be inserted as one more switch case in TabExpansion switch body: ----- 8< ----- # 'Heal' command lines: '<command>' or & '<command>' '(.*)\^$' { if ($line -like "& '*^" -or $line -like "'*^") { & { trap { [void][Reflection.Assembly]::LoadWithPartialName( 'System.Windows.Forms') continue } [void][System.Windows.Forms.SendKeys] } if ($line -like "& '*^") { [System.Windows.Forms.SendKeys]::SendWait( '{BS}{BS}{HOME}{DEL}{DEL}{DEL}{END}') } else { [System.Windows.Forms.SendKeys]::SendWait( '{BS}{BS}{HOME}{DEL}{END}') } } break } ----- 8< ----- -- Thanks, Roman |
My System Specs![]() |
| | #2 (permalink) |
| | RE: TabExpansion for... command history. And issues... One more tab expansion issue (not related to my approach for command history). As I mentioned, in some cases when the function TabExpansion returns a string with spaces it is expanded as: PS> 'string with spaces' or PS> & 'string with spaces' In both cases PowerShell just adds single quotes around a string and *does not* care of single quotes inside a string at all. This behaviour looks questionable in any case. -- Thanks, Roman |
My System Specs![]() |
| | #3 (permalink) |
| | Re: TabExpansion for... command history. And issues... Roman Kuzmin wrote: > TabExpansion for... command history. And issues... > > I use very long command history, my profile imports previously exported > commands and I finish interactive PowerShell sessions by a wrapper of 'exit' > which exports commands. As a result I keep more than a thousand commands and > quite often I effectively reuse very old of them. Right, I do need a solution > for easy and convenient command search/extraction. I know what you mean... Sorry I don't have an answer for the tab expansion questions. What I did, though, was create a "last" command that searches the command history backwards for a given regular expression. It works something like: % last pack Run 'zipper pack c:\foo\bar2.zpr' (Y/N)? n Run 'zipper pack c:\foo\bar.zpr' (Y/N)? y .... It's still not nearly as good as bash's Ctrl-R history searching, but with a good regular expression it finds the one I want pretty quickly. Then you don't need to copy and paste with the mouse. |
My System Specs![]() |
| | #4 (permalink) |
| | Re: TabExpansion for... command history. And issues... "Adam Milazzo" wrote: > % last pack > Run 'zipper pack c:\foo\bar2.zpr' (Y/N)? n > Run 'zipper pack c:\foo\bar.zpr' (Y/N)? y > .... I was thinking of a similar solution. But approximately in 50% cases first I have to modify a command a bit. That is why I am looking for a solution giving me a command text just in the command line ready for both execution or modification. TabExpansion of a pattern is exactly what I need. Unfortunately it is not perfect due to added extra symbols & and '. It would be great to know how to control results of tab expansion more precisely if it is possible. -- Thanks, Roman |
My System Specs![]() |
| | #5 (permalink) |
| | RE: TabExpansion for... command history. And issues... At least it would be interesting to know, is it OK that tab expansion changes my data? Perhaps it is not at all. If I can customize data for tab expansion, then I guess, PowerShell must just display them as they are, it is my duty to take care of spaces or whatever else in what I return back. TabExpansion is a good ready to use tool to extend currently quite limited UI according to a user needs. That's a pity that it behaves like it does now. And I still do not see reasons for that. -- Thanks, Roman |
My System Specs![]() |
| | #6 (permalink) |
| | Re: TabExpansion for... command history. And issues... In RC2, the strings returned by the TabExpansion function are no longer automatically quoted so you may be able to do what you want. -bruce -- Bruce Payette [MSFT] Windows PowerShell Technical Lead Microsoft Corporation This posting is provided "AS IS" with no warranties, and confers no rights. "Roman Kuzmin" <RomanKuzmin@discussions.microsoft.com> wrote in message news:776FCBFD-A950-4BCE-9478-202CEE7DA3CE@microsoft.com... > "Adam Milazzo" wrote: >> % last pack >> Run 'zipper pack c:\foo\bar2.zpr' (Y/N)? n >> Run 'zipper pack c:\foo\bar.zpr' (Y/N)? y >> .... > > I was thinking of a similar solution. But approximately in 50% cases first > I > have to modify a command a bit. That is why I am looking for a solution > giving me a command text just in the command line ready for both execution > or > modification. TabExpansion of a pattern is exactly what I need. > Unfortunately > it is not perfect due to added extra symbols & and '. It would be great to > know how to control results of tab expansion more precisely if it is > possible. > > -- > Thanks, > Roman > |
My System Specs![]() |
| | #7 (permalink) |
| | Re: TabExpansion for... command history. And issues... "Bruce Payette [MSFT]" wrote: > In RC2, the strings returned by the TabExpansion function are no longer > automatically quoted so you may be able to do what you want. What can I say? It's just great!! I do have plans for using TabExpansion very intensively for my needs (actually I use it already but with weird workaround). -- Thanks, Roman |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| best way to determine if a command history exists? | PowerShell | |||
| Command window - history? | Vista General | |||
| Start->Run Command History annoyance. | Vista General | |||
| Bash style command history | PowerShell | |||
| command history | PowerShell | |||