Skip to content

Commit

Permalink
Inital commit if PSVim
Browse files Browse the repository at this point in the history
Start-VimErrorFile
  • Loading branch information
Staffan Gustafsson committed May 3, 2014
1 parent 97b1869 commit cdd7b0f
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 0 deletions.
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,39 @@ PSVim
=====

Gems for using PowerShell with vim.

This module is intented for the gigantic intersection of PowerShell and Vim users.

Usage
-----
To start using, just import the module

```powershell
Import-Module vim
```

Start-VimErrorFile
------------------

Using the --errorfile option of gvim to quickly jump between files or matches from Select-String.

In the same way as you can jump between compilation errors in vim, you can now jump between files or matches piped into
Start-VimErrorFile

```powershell
Get-ChildItem -Recurse -Filter *.cs | Select-String throw | Start-VimErrorFile
ls *.txt | stve
# open a new gvim window
ls *.ps1 | sls function | stve -newinstance
```
The above examples open gvim with all locations where an exception in the error list

By adding the following to _vimrc it is quick and easy to jump between the hits with F2 or F3
```
nnoremap <silent> <F2> :bn<CR>
nnoremap <silent> <S-F2> :bp<CR>
nnoremap <silent> <F3> :cn<CR>
nnoremap <silent> <S-F3> :cp<CR>
```
118 changes: 118 additions & 0 deletions vim/errorfile.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
function LaunchVim
{
param
(
[System.String]
$errorFile,

[System.Management.Automation.SwitchParameter]
$NewInstance
)

if ($NewInstance)
{
gvim -q $errorfile
}
else
{
# Make sure we have a running instance to send commands to
if(!(vim --serverlist | Select-String "$reuseInstanceName" -q))
{
gvim '--servername' $reuseInstanceName
Start-Sleep -Milliseconds 1000
}
gvim --servername $reuseInstanceName --remote-send "<ESC><ESC><ESC>:cf $errorFile<CR>"
}
}


function Start-VimErrorFile
{
<#
.Synopsis
Starts the Vim editor with an error file.
.Description
This command starts creates an errorfile from the pipeline input and starts gvim with that file as an argument.
This enables easy navigations
.Example
PS> Get-ChildItem *.txt | Start-VimWithErrorInfo
PS> Select-String 'Foo' *.txt | Start-VimWithErrorInfo
.Link
.Notes
NAME: Start-VimErrorInfo
#Requires -Version 2.0
#>
[CmdletBinding(
DefaultParameterSetName='input')]
param(
[Parameter(ParameterSetName='ErrorFile',Position=0, Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
[string] $errfile,
[Parameter(ParameterSetName='input',Position=0, Mandatory=$true, ValueFromPipeline=$true)]
[ValidateNotNull()]
[PSObject[]] $data,
[switch] $NewInstance
)

begin{
$lines = New-Object System.Collections.Generic.list[string] 100
}
process {
if ($PsCmdLet.ParameterSetName -eq 'ErrorFile')
{
LaunchVim (Get-Item $errFile).FullName -forceNewInstance:$NewInstance
return
}
foreach($d in $data)
{
if($d -is [Microsoft.PowerShell.Commands.MatchInfo])
{
$lines += '{0}({1}) : {2}' -f $d.path, $d.LineNumber, $d.Line
continue
}
if ($d -is [System.IO.FileInfo])
{
$lines += '{0}(1) : File: {1}' -f $d.FullName, $d.Name
continue
}
if ($d -is [System.IO.DirectoryInfo])
{
continue
}
if ($d.PSPath)
{
$lines += (Resolve-Path $d.PSPath).ProviderPath | ForEach-Object {'{0}(1) : File: {1}' -f $_, (Split-Path -Leaf $_)}
continue
}
if ($d -is [string]){
$lines += $d
continue
}

Write-Error -targetobject $data -message 'Invalid type of input'

}
}
end
{
if($lines)
{
$outfile = [io.path]::GetTempFileName() + '.vimerror'
$output = [string]::join("`n", $lines)
Set-Content -encoding Ascii -Path $outfile -Value $output
LaunchVim (Get-Item $outfile).fullname -NewInstance:$NewInstance
}
}
}

function Set-VimReuseInstanceName{
param
(
[string] $name = 'PsVim'
)

$script:reuseInstanceName = $name
}
Binary file added vim/vim.psd1
Binary file not shown.
15 changes: 15 additions & 0 deletions vim/vim.psm1
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
$reuseInstanceName='PsVim'

$gvimPath = (Get-ItemProperty HKLM:\SOFTWARE\Vim\Gvim -Name Path).Path

$vimDir = Split-Path $gvimPath

Set-Alias gvim $vimDir\gvim.exe
Set-Alias vim $vimDir\vim.exe

. $PSScriptRoot\errorfile.ps1

Set-Alias stve Start-VimErrorFile

Export-ModuleMember Start-VimErrorFile,Set-VimReuseInstanceName
Export-ModuleMember -alias gvim, stve

0 comments on commit cdd7b0f

Please sign in to comment.