8
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Vim のインストール(PowerShell スクリプト)

Last updated at Posted at 2016-09-07

こんにちは。
Vim - KaoriYa (Windwos用) を自動でインストールする PowerShell スクリプトを作ってみました1。内部で netupvim に頼っています。

合わせて、Chocolatey および less もインストールし、less の中からは Vim へ遷移できるようにしました(キー入力v)。

PS> .\install_vim.ps1
(installing vim.exe into C:\commands\vim\)
  :
  :

インストール後、使ってみると、

PS> vi install_vim.ps1
PS> less install_vim.ps1
install_vim.ps1
# Encoding::Shift-JIS

function Check-Privilege {
  if (([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole(`
        [Security.Principal.WindowsBuiltInRole] "Administrator")) {
    if ((Get-ExecutionPolicy) -eq "Restricted") {
      Write-Warning "Set-ExecutionPolicy RemoteSigned  を PowerShell 上で事前に実行してください" -ForegroundColor "Yellow"
      exit 1
    }
  }
  else {
      Write-Warning "PowerShell を管理者権限で実行してください" -ForegroundColor "Yellow"
    exit 1
  }
}

function Check-Profile {
  if (!(Test-Path "$profile")) { 
    new-item -type file -force $profile | out-null
  }
}

function Get-UniqueDir($parentPath) {
  $path = $null
  Do {
    $path = Join-Path $parentPath ([IO.Path]::GetRandomFileName())
  } while (Test-Path $path)
  return $path
}

function Make-Dir($dir) {
  $dirinfo = $null
  if (!(Test-Path "$dir")) { 
    mkdir "$dir" | out-null
  }
  return $dir
}

function Create-Shortcut($TargetFile, $dir) {
  $command = (Get-Item $TargetFile).BaseName
  $ShortcutFile = "$($dir)\$($command).lnk"
  $WScriptShell = New-Object -ComObject WScript.Shell
  $Shortcut = $WScriptShell.CreateShortcut($ShortcutFile)
  $Shortcut.TargetPath = $TargetFile
  $Shortcut.Save()
}

function Set-ProfileforVim($vim) {
  $lines = @"
set shell=powershell.exe
`$env:VISUAL = `"```"$vim```"`"
set-alias vi `"$vim`"
"@
  echo $lines | Out-File $profile -Append -Encoding ASCII
  . $profile
}

# Vim - KaoriYa
# https://www.kaoriya.net/software/vim/
function Install-Vim {
  $ver="1.2"
  $vimDir="C:\Program Files\vim"
  $installer = "UPDATE.bat"
  $executable = "vim.exe"
  $executable_gvim = "gvim.exe"
  $startmenu = "C:\ProgramData\Microsoft\Windows\Start Menu\Programs"
  $url = "https://github.com/koron/netupvim/releases/download/v$($ver)/netupvim-v$($ver).zip" 
  Write-host "(installing $($executable) into $($vimDir)\)"
  $wc = new-object System.Net.WebClient
  $uri = New-Object System.Uri($url)
  $tempDir = (Make-Dir (Get-UniqueDir ([IO.Path]::GetTempPath())))  
  $downloaded = (Join-Path $tempDir (Split-Path $uri.AbsolutePath -Leaf))
  $wc.DownloadFile($uri, $downloaded)
  $shell_app = new-object -com shell.application
  Make-Dir "$vimDir"
  $tgtDir = $shell_app.namespace($vimDir)
  $tgtDir.Copyhere($shell_app.namespace("$downloaded").items())
  remove-item -path $tempDir -recurse -force
  cmd /c (Join-Path $vimDir $installer)
  Set-ProfileforVim (Join-Path $vimDir $executable)
  Create-Shortcut (Join-Path $vimDir $executable_gvim) $startmenu
}

# Chocolatey
# https://chocolatey.org/packages
function Install-Chocolatey {
  set ChocolateyInstall=C:\ProgramData\chocolatey
  $url = 'https://chocolatey.org/install.ps1'
  $wc = new-object System.Net.WebClient
  Invoke-Expression ($wc.DownloadString($url))
  choco list -lo
}

Check-Privilege
Check-Profile
Install-Vim
Install-Chocolatey
choco install -y less
exit
  1. 同梱の gvim (GUI 版) も合わせて自動でインストールされます。

8
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?