Use Vim to write Shell scripts and run them on Windows systems

1. PowerShell under Windows

Shell is both a command language and a programming language. It is currently mainly used in Linux systems.
PowerShell is a cross-platform task automation solution consisting of a command line shell, a scripting language, and a configuration management framework that can run on Windows, Linux, and macOS.
In the Windows environment, PowerShell commands are more similar to Linux shell commands than cmd, so if you want to learn shell scripts under Windows, using PowerShell is a good choice.

2. Vim download and installation (Windows environment)

Download address portal: https://www.vim.org/download.php#pc
Click the blue link to download
After downloading, you can choose the classic version to install. It is recommended to install it in the D drive directory with a pure English path.

3. Configure PowerShell policy, use Vim

To modify the PowerShell execution policy, you need to open PowerShell with administrator rights. Just open a directory and follow the following three steps.
Insert image description here

# 在 PowerShell 输入下面的命令,执行 Y (RemoteSigned 模式也是可以的)
Set-ExecutionPolicy Unrestricted

# 继续输入 
new-item -path $profile -itemtype file -force

Insert image description here
Find the folder in the red box in the picture above, open the script file in it with Notepad Microsoft.PowerShell_profile.ps1, and enter the following content.

set-alias vim "D:\Program Files (x86)\Vim\vim82\vim.exe" # 此处为 Vim 的安装路径
 
Function Edit-Profile
{
    
    
    vim $profile
}
 
Function Edit-Vimrc
{
    
    
    vim $HOME\_vimrc
}

4. Use Vim to write Shell script + run in PowerShell

After completing the above steps, restart PowerShell.
Regarding Vim, iit is to insert, escexit the insert mode, and then use to :wqsave and exit.

# 生成并编辑文件 test.ps1
vim test.ps1
# i 插入模式后,输入 shell 脚本代码,完成后 esc 退出插入模式,:wq 保存退出编辑
echo "Hello World !"
# 运行 shell 脚本
./test.ps1

Note that the PowerShell script extension is .ps1, not the one in Linux .sh.

References

Guess you like

Origin blog.csdn.net/weixin_44778151/article/details/124573537