Skip to content

Commit cdd7b0f

Browse files
author
Staffan Gustafsson
committed
Inital commit if PSVim
Start-VimErrorFile
1 parent 97b1869 commit cdd7b0f

File tree

4 files changed

+169
-0
lines changed

4 files changed

+169
-0
lines changed

README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,39 @@ PSVim
22
=====
33

44
Gems for using PowerShell with vim.
5+
6+
This module is intented for the gigantic intersection of PowerShell and Vim users.
7+
8+
Usage
9+
-----
10+
To start using, just import the module
11+
12+
```powershell
13+
Import-Module vim
14+
```
15+
16+
Start-VimErrorFile
17+
------------------
18+
19+
Using the --errorfile option of gvim to quickly jump between files or matches from Select-String.
20+
21+
In the same way as you can jump between compilation errors in vim, you can now jump between files or matches piped into
22+
Start-VimErrorFile
23+
24+
```powershell
25+
Get-ChildItem -Recurse -Filter *.cs | Select-String throw | Start-VimErrorFile
26+
27+
ls *.txt | stve
28+
29+
# open a new gvim window
30+
ls *.ps1 | sls function | stve -newinstance
31+
```
32+
The above examples open gvim with all locations where an exception in the error list
33+
34+
By adding the following to _vimrc it is quick and easy to jump between the hits with F2 or F3
35+
```
36+
nnoremap <silent> <F2> :bn<CR>
37+
nnoremap <silent> <S-F2> :bp<CR>
38+
nnoremap <silent> <F3> :cn<CR>
39+
nnoremap <silent> <S-F3> :cp<CR>
40+
```

vim/errorfile.ps1

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
function LaunchVim
2+
{
3+
param
4+
(
5+
[System.String]
6+
$errorFile,
7+
8+
[System.Management.Automation.SwitchParameter]
9+
$NewInstance
10+
)
11+
12+
if ($NewInstance)
13+
{
14+
gvim -q $errorfile
15+
}
16+
else
17+
{
18+
# Make sure we have a running instance to send commands to
19+
if(!(vim --serverlist | Select-String "$reuseInstanceName" -q))
20+
{
21+
gvim '--servername' $reuseInstanceName
22+
Start-Sleep -Milliseconds 1000
23+
}
24+
gvim --servername $reuseInstanceName --remote-send "<ESC><ESC><ESC>:cf $errorFile<CR>"
25+
}
26+
}
27+
28+
29+
function Start-VimErrorFile
30+
{
31+
<#
32+
.Synopsis
33+
Starts the Vim editor with an error file.
34+
.Description
35+
This command starts creates an errorfile from the pipeline input and starts gvim with that file as an argument.
36+
This enables easy navigations
37+
38+
.Example
39+
PS> Get-ChildItem *.txt | Start-VimWithErrorInfo
40+
41+
PS> Select-String 'Foo' *.txt | Start-VimWithErrorInfo
42+
43+
.Link
44+
45+
.Notes
46+
NAME: Start-VimErrorInfo
47+
#Requires -Version 2.0
48+
#>
49+
[CmdletBinding(
50+
DefaultParameterSetName='input')]
51+
param(
52+
[Parameter(ParameterSetName='ErrorFile',Position=0, Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
53+
[string] $errfile,
54+
[Parameter(ParameterSetName='input',Position=0, Mandatory=$true, ValueFromPipeline=$true)]
55+
[ValidateNotNull()]
56+
[PSObject[]] $data,
57+
[switch] $NewInstance
58+
)
59+
60+
begin{
61+
$lines = New-Object System.Collections.Generic.list[string] 100
62+
}
63+
process {
64+
if ($PsCmdLet.ParameterSetName -eq 'ErrorFile')
65+
{
66+
LaunchVim (Get-Item $errFile).FullName -forceNewInstance:$NewInstance
67+
return
68+
}
69+
foreach($d in $data)
70+
{
71+
if($d -is [Microsoft.PowerShell.Commands.MatchInfo])
72+
{
73+
$lines += '{0}({1}) : {2}' -f $d.path, $d.LineNumber, $d.Line
74+
continue
75+
}
76+
if ($d -is [System.IO.FileInfo])
77+
{
78+
$lines += '{0}(1) : File: {1}' -f $d.FullName, $d.Name
79+
continue
80+
}
81+
if ($d -is [System.IO.DirectoryInfo])
82+
{
83+
continue
84+
}
85+
if ($d.PSPath)
86+
{
87+
$lines += (Resolve-Path $d.PSPath).ProviderPath | ForEach-Object {'{0}(1) : File: {1}' -f $_, (Split-Path -Leaf $_)}
88+
continue
89+
}
90+
if ($d -is [string]){
91+
$lines += $d
92+
continue
93+
}
94+
95+
Write-Error -targetobject $data -message 'Invalid type of input'
96+
97+
}
98+
}
99+
end
100+
{
101+
if($lines)
102+
{
103+
$outfile = [io.path]::GetTempFileName() + '.vimerror'
104+
$output = [string]::join("`n", $lines)
105+
Set-Content -encoding Ascii -Path $outfile -Value $output
106+
LaunchVim (Get-Item $outfile).fullname -NewInstance:$NewInstance
107+
}
108+
}
109+
}
110+
111+
function Set-VimReuseInstanceName{
112+
param
113+
(
114+
[string] $name = 'PsVim'
115+
)
116+
117+
$script:reuseInstanceName = $name
118+
}

vim/vim.psd1

5.09 KB
Binary file not shown.

vim/vim.psm1

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
$reuseInstanceName='PsVim'
2+
3+
$gvimPath = (Get-ItemProperty HKLM:\SOFTWARE\Vim\Gvim -Name Path).Path
4+
5+
$vimDir = Split-Path $gvimPath
6+
7+
Set-Alias gvim $vimDir\gvim.exe
8+
Set-Alias vim $vimDir\vim.exe
9+
10+
. $PSScriptRoot\errorfile.ps1
11+
12+
Set-Alias stve Start-VimErrorFile
13+
14+
Export-ModuleMember Start-VimErrorFile,Set-VimReuseInstanceName
15+
Export-ModuleMember -alias gvim, stve

0 commit comments

Comments
 (0)