From 1b82df1510ddffd515ce1cd6cdbd71c4a2df2f79 Mon Sep 17 00:00:00 2001 From: Wang Shidong Date: Thu, 13 Jan 2022 19:18:38 +0800 Subject: [PATCH] docs(guide): Add vim script guide --- docs/cn/vim-script.md | 103 +++++++++++++++++++++++++++++ docs/development/vim-script.md | 114 +++++++++++++++++++++++++++++++++ 2 files changed, 217 insertions(+) create mode 100644 docs/cn/vim-script.md create mode 100644 docs/development/vim-script.md diff --git a/docs/cn/vim-script.md b/docs/cn/vim-script.md new file mode 100644 index 000000000..1941a0640 --- /dev/null +++ b/docs/cn/vim-script.md @@ -0,0 +1,103 @@ +--- +title: "Vim 脚本指南" +description: "Vim 脚本指南" +lang: zh +--- + +# [主页](../) >> Vim 脚本指南 + +使用 Vim 难免会接触到 Vim 脚本语言,本文主要介绍的 Vim 脚本的基本语法和使用技巧。 + + + +- [1 基本语法](#1-基本语法) + - [1.1 变量的命名](#11-变量的命名) + - [1.2 变量的作用域](#12-变量的作用域) + - [1.3 变量的定义](#13-变量的定义) + - [1.4 变量的类型](#14-变量的类型) + - [1.4.1 数字(number)](#141-数字number) + - [1.4.2 字符串(string)](#142-字符串string) +- [2、循环](#2循环) +- [3、流程控制](#3流程控制) + - [3.1、if-else 语句](#31if-else-语句) +- [4、函数](#4函数) + - [4.1、函数的定义](#41函数的定义) + - [4.2、函数的作用域](#42函数的作用域) + + + +## 1 基本语法 + +### 1.1 变量的命名 + +Vim 的变量名称可以使用字母、下划线、数字组成,区分大小写,并且不可以以数字开头。 + +### 1.2 变量的作用域 + +Vim 脚本中,变量的作用域支持如下类型: + +- `g:`:全局作用域,定义后,在任何地方,脚本内、函数内,都可以调用 +- `w:`:某个窗口作用域,Vim 中每个窗口都有一个固定的ID, +- `b:`:某个缓冲区作用域 +- `s:`:某个脚本内的作用域 + +### 1.3 变量的定义 + + +### 1.4 变量的类型 + +同其他大部分语言一样,Vim 脚本支持多种变量类型, 使用函数 `type()` 可以获取变量类型的值,包括: + +- 数字(number):0 +- 字符串(string):1 +- 函数(Funcref):2 +- 列表(list):3 +- 字典(Dictionary):4 +- 浮点数(Float):5 +- 布尔值(Boolean):6 +- 空(Null):7 + + +#### 1.4.1 数字(number) + +#### 1.4.2 字符串(string) + + +## 2、循环 + +通常当我们需要做一些有规律性的重复操作时,就需要重复执行某一段脚本。 + +这些被重复执行的语句就称之为循环体,是否需要继续重复,取决于循环的终止条件。 + +循环语句是由循环体及循环的终止条件两部分组成的。 + +```plantuml +start +while (终止条件) + endwhile (否) +:结束; +end +``` + +## 3、流程控制 + +### 3.1、if-else 语句 + +```plantuml +!pragma useVerticalIf on +start +if (condition A) then (yes) + :Text 1; +else (nothing) + :Text else; +endif +``` +## 4、函数 + +在 Vim 脚本中,函数是对语句和表达式进行抽象的主要方法。既可以用来处理一些特殊的工作,也可以用来计算一些值。 + +### 4.1、函数的定义 + +Vim 中使用 `function` 地 + +### 4.2、函数的作用域 diff --git a/docs/development/vim-script.md b/docs/development/vim-script.md new file mode 100644 index 000000000..e740254da --- /dev/null +++ b/docs/development/vim-script.md @@ -0,0 +1,114 @@ +# Vim Script Guide + + + + +- [Introduction](#introduction) +- [Syntax](#syntax) + - [Basic syntax](#basic-syntax) + - [Comments](#comments) +- [Variables](#variables) + - [Variable Scope](#variable-scope) +- [Data Types](#data-types) +- [Operators](#operators) +- [Loop](#loop) + - [for loop](#for-loop) + - [while loop](#while-loop) +- [Functions](#functions) + - [return statement](#return-statement) + + + +## Introduction + +Vim script is the built-in language used in Vim/Neovim editors. + +## Syntax + +### Basic syntax + +### Comments + +## Variables + +Variables are "containers" for storing information. In vim script, a variable starts with the scoop, +followed by the name of this variable. + +``` +let g:foo = 'hello world' +``` + +### Variable Scope + +In vim script, there are 6 kinds of variable scopes: + +1. `g:` global variable scope +2. `s:` local to script +3. `l:` local to function, it can be prepended. +4. `w:` local to window +5. `t:` local to tab +6. `b:` local to buffer + +## Data Types + +## Operators + +## Loop + +Often when you write vim script, you want the same code block to run over and over again. +Instead of adding several almost equal lines in the script, we can use loops. + +In vim script there are two kinds of lools, `for loop` and `while loop`. + +### for loop + + +To execute a block of code a specified number of times, you need to use for loop. +here is an example of for loop in vim script: + +```vim +for n in range(10) + echo n +endfor +``` + +### while loop + +While loops execute a block of code while the specified condition is true. + + +## Functions + +Vim provides many built-in functions, besides the built-in functions, +we can also create our own functions. + +```vim +function! TestHello() abort + echo "hello world" +endfunction +``` + +use `:call TestHello()` to run a function. + +### return statement + +Within a function, we can use return statement to return a variable. +if the return statement is prepended. `0` is returned. + +```vim +function! Test() abort + return 'hello' +endfunction + +echo Test() + +" hello + +function! Test() abort + +endfunction + +echo Test() + +" 0 +```