vimscript: update layout

This commit is contained in:
Rico Sta. Cruz 2017-08-30 18:43:36 +08:00
parent 240bb3a408
commit 1f0d4eca51
No known key found for this signature in database
GPG Key ID: CAAD38AE2962619A
3 changed files with 137 additions and 60 deletions

View File

@ -40,10 +40,11 @@ category: React
layout: 2017/sheet # 'default' | '2017/sheet' layout: 2017/sheet # 'default' | '2017/sheet'
# Optional: # Optional:
updated: 201708 # To show in the updated list (update _config.yml) updated: 201708 # To show in the updated list (update _config.yml)
ads: false # Add this to disable ads ads: false # Add this to disable ads
weight: -5 # lower number = higher in related posts list weight: -5 # lower number = higher in related posts list
deprecated: true # Don't show in related posts deprecated: true # Don't show in related posts
prism_languages: [vim] # Extra syntax highlighting
--- ---
``` ```

View File

@ -12,6 +12,10 @@
<script src='https://unpkg.com/prismjs@1.6.0/components/prism-bash.min.js'></script> <script src='https://unpkg.com/prismjs@1.6.0/components/prism-bash.min.js'></script>
<script src='https://unpkg.com/prismjs@1.6.0/components/prism-scss.min.js'></script> <script src='https://unpkg.com/prismjs@1.6.0/components/prism-scss.min.js'></script>
<script src='https://unpkg.com/prismjs@1.6.0/components/prism-elixir.min.js'></script> <script src='https://unpkg.com/prismjs@1.6.0/components/prism-elixir.min.js'></script>
<script src='https://unpkg.com/prismjs@1.6.0/components/prism-elixir.min.js'></script>
{% for lang in page.prism_languages %}
<script src='https://unpkg.com/prismjs@1.6.0/components/prism-{{lang}}.min.js'></script>
{% endfor %}
<script src='https://unpkg.com/prismjs@1.6.0/plugins/line-highlight/prism-line-highlight.min.js'></script> <script src='https://unpkg.com/prismjs@1.6.0/plugins/line-highlight/prism-line-highlight.min.js'></script>
<link rel='stylesheet' href='https://unpkg.com/sanitize.css@5.0.0/sanitize.css'> <link rel='stylesheet' href='https://unpkg.com/sanitize.css@5.0.0/sanitize.css'>
<!-- <link rel='stylesheet' href='https://unpkg.com/prismjs@1.6.0/themes/prism.css'> --> <!-- <link rel='stylesheet' href='https://unpkg.com/prismjs@1.6.0/themes/prism.css'> -->

View File

@ -1,21 +1,21 @@
--- ---
title: Vim script title: Vim scripting
category: Vim category: Vim
hljs_languages: [vim] prism_languages: [vim]
layout: default-ad layout: 2017/sheet
weight: -10
--- ---
### Start hacking. ### Start hacking
You can either put this in a script (`script.vim`) and run it (`:source script.vim`), or you can type the commands individually in normal mode as `:let` and `:echo`.
```vim ```vim
let name = "John" let name = "John"
echo "Hello, " . name echo "Hello, " . name
``` ```
{:.light}
You can either put this in a script (`script.vim`) and run it (`:source script.vim`), or you can type the commands individually in normal mode as `:let` and `:echo`.
### Learn by example ### Learn by example
[Here](http://www.vimbits.com/bits/46)'s another example with [functions](#functions), [variables](#variables) and [mapping](#mapping).
```vim ```vim
function! SuperTab() function! SuperTab()
@ -30,16 +30,19 @@ endfunction
imap <Tab> <C-R>=SuperTab()<CR> imap <Tab> <C-R>=SuperTab()<CR>
``` ```
[Here](http://www.vimbits.com/bits/46)'s another example with [functions](#functions), [variables](#variables) and [mapping](#mapping).
Variables Variables
--------- ---------
### Defining
{: .-prime}
```vim ```vim
let var = "hello" let var = "hello"
``` ```
{:.light}
### Variable prefixes ### Variable prefixes
The `s:` prefix is also available in function names. See `:help local-variables`
```vim ```vim
let g:ack_options = '-s -H' " g: global let g:ack_options = '-s -H' " g: global
@ -47,20 +50,23 @@ let s:ack_program = 'ack' " s: local (to script)
let l:foo = 'bar' " l: local (to function) let l:foo = 'bar' " l: local (to function)
``` ```
The `s:` prefix is also available in function names. See `:help local-variables`
### Other prefixes ### Other prefixes
```vim ```vim
let w:foo = 'bar' " w: window let w:foo = 'bar' " w: window
let b:state = 'on' " b: buffer let b:state = 'on' " b: buffer
let t:state = 'off' " t: tab let t:state = 'off' " t: tab
echo v:var " v: vim special echo v:var " v: vim special
```
let @/ = '' " @ register (this clears last search patterN) ```vim
echo $PATH " $ env let @/ = '' " @ register (this clears last search pattern)
echo $PATH " $ env
``` ```
### Vim options ### Vim options
Prefix with `&`
```vim ```vim
echo 'tabstop is ' . &tabstop echo 'tabstop is ' . &tabstop
@ -69,6 +75,8 @@ echo &g:option
echo &l:option echo &l:option
``` ```
Prefix Vim options with `&`
### Operators ### Operators
```vim ```vim
@ -82,9 +90,9 @@ let var += 5
let var .= 'string' " concat let var .= 'string' " concat
``` ```
## [Strings](http://learnvimscriptthehardway.stevelosh.com/chapters/26.html) ## Strings
Also see `:help literal-string` and `:help expr-quote` ### Strings
```vim ```vim
let str = "String" let str = "String"
@ -96,8 +104,10 @@ let literal = 'that''s enough' " double '' => '
echo "result = " . re " concatenation echo "result = " . re " concatenation
``` ```
### [String functions](learnvimscriptthehardway.stevelosh.com/chapters/27.html) Also see `:help literal-string` and `:help expr-quote`.
Also see `:help functions` See: [Strings](http://learnvimscriptthehardway.stevelosh.com/chapters/26.html)
### String functions
```vim ```vim
strlen(str) " length strlen(str) " length
@ -113,9 +123,15 @@ tolower('Hello')
toupper('Hello') toupper('Hello')
``` ```
[Functions](http://learnvimscriptthehardway.stevelosh.com/chapters/23.html) Also see `:help functions`
See: [String functions](learnvimscriptthehardway.stevelosh.com/chapters/27.html)
Functions
--------- ---------
### Functions
{: .-prime}
```vim ```vim
" prefix with s: for local script-only functions " prefix with s: for local script-only functions
function! s:Initialize(cmd, args) function! s:Initialize(cmd, args)
@ -125,7 +141,8 @@ function! s:Initialize(cmd, args)
return true return true
endfunction endfunction
``` ```
{:.light}
See: [Functions](http://learnvimscriptthehardway.stevelosh.com/chapters/23.html)
### Namespacing ### Namespacing
@ -154,8 +171,7 @@ function! myfunction() abort
endfunction endfunction
``` ```
### [Var arguments](http://learnvimscriptthehardway.stevelosh.com/chapters/24.html) ### Var arguments
See `:help function-argument`
```vim ```vim
function! infect(...) function! infect(...)
@ -171,6 +187,8 @@ endfunction
infect('jake', 'bella') infect('jake', 'bella')
``` ```
See `:help function-argument`. See: [Var arguments](http://learnvimscriptthehardway.stevelosh.com/chapters/24.html)
Loops Loops
----- -----
@ -190,12 +208,14 @@ endwhile
Custom commands Custom commands
--------------- ---------------
Custom commands start with uppercase letters. The `!` redefines a command if it already exists. ### Custom commands
{: .-prime}
```vim ```vim
command! Save :set fo=want tw=80 nowrap command! Save :set fo=want tw=80 nowrap
``` ```
{:.light}
Custom commands start with uppercase letters. The `!` redefines a command if it already exists.
### Commands calling functions ### Commands calling functions
@ -236,8 +256,7 @@ else
endif endif
``` ```
### [Truthiness](http://learnvimscriptthehardway.stevelosh.com/chapters/21.html) ### Truthiness
No booleans. `0` is false, `1` is true.
```vim ```vim
if 1 | echo "true" | endif if 1 | echo "true" | endif
@ -252,8 +271,10 @@ if "456" "=> 1 (true)
if "xfz" "=> 0 (false) if "xfz" "=> 0 (false)
``` ```
### [Operators](http://learnvimscriptthehardway.stevelosh.com/chapters/22.html) No booleans. `0` is false, `1` is true.
See `:help expression-syntax` See: [Truthiness](http://learnvimscriptthehardway.stevelosh.com/chapters/21.html)
### Operators
```vim ```vim
if 3 > 2 if 3 > 2
@ -262,6 +283,9 @@ if (a && b) || (c && d)
if !c if !c
``` ```
See `:help expression-syntax`.
See: [Operators](http://learnvimscriptthehardway.stevelosh.com/chapters/22.html)
### Strings ### Strings
```vim ```vim
@ -273,13 +297,14 @@ if name == 'John' " depends on :set ignorecase
``` ```
### Identity operators ### Identity operators
Checks if it's the same instance object
``` ```vim
a is b a is b
a isnot b a isnot b
``` ```
Checks if it's the same instance object.
### Regexp matches ### Regexp matches
```vim ```vim
@ -294,16 +319,21 @@ if empty(a:path) | return [] | endif
a ? b : c a ? b : c
``` ```
Use `|` to join lines together.
### Boolean logic ### Boolean logic
```vim ```vim
if g:use_dispatch && s:has_dispatch if g:use_dispatch && s:has_dispatch
···
endif endif
``` ```
Lists Lists
----- -----
### Lists
```vim ```vim
let mylist = [1, two, 3, "four"] let mylist = [1, two, 3, "four"]
@ -360,7 +390,7 @@ call filter(files, 'v:val != ""')
Dictionaries Dictionaries
------------ ------------
See `:help dict` ### Dictionaries
```vim ```vim
let colors = { let colors = {
@ -370,21 +400,35 @@ let colors = {
echo colors["a"] echo colors["a"]
echo get(colors, "apple") " supress error echo get(colors, "apple") " supress error
```
See `:help dict`
### Using dictionaries
```vim
remove(colors, "apple") remove(colors, "apple")
```
```vim
" :help E715 " :help E715
if has_key(dict, 'foo') if has_key(dict, 'foo')
if empty(dict) if empty(dict)
keys(dict) keys(dict)
len(dict) len(dict)
```
```vim
max(dict) max(dict)
min(dict) min(dict)
```
```vim
count(dict, 'x') count(dict, 'x')
string(dict) string(dict)
```
```vim
map(dict, '<>> " . v:val') map(dict, '<>> " . v:val')
``` ```
@ -397,12 +441,13 @@ endfor
``` ```
### Prefixes ### Prefixes
Prefixes (`s:`, `g:`, `l:`, etc) are actually dictionaries
```vim ```vim
keys(s:) keys(s:)
``` ```
Prefixes (`s:`, `g:`, `l:`, etc) are actually dictionaries.
### Extending ### Extending
```vim ```vim
@ -419,10 +464,11 @@ str2nr("3")
float2nr("3.14") float2nr("3.14")
``` ```
[Numbers](http://learnvimscriptthehardway.stevelosh.com/chapters/25.html) Numbers
------- -------
See `:help Number` ### Numbers
{: .-prime}
```vim ```vim
let int = 1000 let int = 1000
@ -430,14 +476,18 @@ let int = 0xff
let int = 0755 " octal let int = 0755 " octal
``` ```
See `:help Number`.
See: [Numbers](http://learnvimscriptthehardway.stevelosh.com/chapters/25.html)
### Floats ### Floats
See `:help Float`
```vim ```vim
let fl = 100.1 let fl = 100.1
let fl = 5.4e4 let fl = 5.4e4
``` ```
See `:help Float`
### Arithmetic ### Arithmetic
```vim ```vim
@ -462,16 +512,17 @@ asin() acos() atan()
Vim-isms Vim-isms
-------- --------
### [Execute a command](http://learnvimscriptthehardway.stevelosh.com/chapters/28.html) ### Execute a command
Runs an ex command you typically run with `:`. Also see `:help execute`
```vim ```vim
execute "vsplit" execute "vsplit"
execute "e " . fnameescape(filename) execute "e " . fnameescape(filename)
``` ```
### [Running keystrokes](http://learnvimscriptthehardway.stevelosh.com/chapters/29.html) Runs an ex command you typically run with `:`. Also see `:help execute`.
Use `:normal` to execute keystrokes as if you're typing them in normal mode. Combine with `:execute` for special keystrokes. See: [Execute a command](http://learnvimscriptthehardway.stevelosh.com/chapters/28.html)
### Running keystrokes
```vim ```vim
normal G normal G
@ -480,6 +531,9 @@ normal! G " skips key mappings
execute "normal! gg/foo\<cr>dd" execute "normal! gg/foo\<cr>dd"
``` ```
Use `:normal` to execute keystrokes as if you're typing them in normal mode. Combine with `:execute` for special keystrokes.
See: [Running keystrokes](http://learnvimscriptthehardway.stevelosh.com/chapters/29.html)
### Getting filenames ### Getting filenames
```vim ```vim
@ -490,13 +544,16 @@ echo expand("%:r") " path/file
echo expand("%:e") " txt echo expand("%:e") " txt
``` ```
See `:help expand`
### Silencing ### Silencing
See `:help silent`
```vim ```vim
silent g/Aap/p silent g/Aap/p
``` ```
Supresses output. See `:help silent`
### Echo ### Echo
```vim ```vim
@ -541,33 +598,48 @@ exists("g:...")
Mapping Mapping
------- -------
{: .-three-column}
### Mapping commands
```vim ```vim
nnoremap nmap
vmap vmap
imap
xmap
nnoremap
vnoremap
inoremap
xnoremap
... ...
``` ```
### Explanation
```vim ```vim
[nvixso](nore)map [nvixso](nore)map
^ ^
| don't recurse
|
normal, visual, insert, eX mode, select, operator-pending
``` ```
Arguments: ```
│ └ don't recurse
└ normal, visual, insert,
eX mode, select, operator-pending
```
{: .-setup}
- `<buffer>` - only in current buffer ### Arguments
- `<silent>` - no echo
- `<nowait>` | `<buffer>` | only in current buffer |
| `<silent>` | no echo |
| `<nowait>` | |
Syntax Syntax
------ ------
### Highlights ### Highlights
``` ```vim
hi Comment hi Comment
term=bold,underline term=bold,underline
gui=bold gui=bold
@ -577,7 +649,7 @@ hi Comment
### Filetype detection ### Filetype detection
``` ```vim
augroup filetypedetect augroup filetypedetect
au! BufNewFile,BufRead *.json setf javascript au! BufNewFile,BufRead *.json setf javascript
augroup END augroup END
@ -587,7 +659,7 @@ au Filetype markdown setlocal spell
### Conceal ### Conceal
``` ```vim
set conceallevel=2 set conceallevel=2
syn match newLine "<br>" conceal cchar=} syn match newLine "<br>" conceal cchar=}
hi newLine guifg=green hi newLine guifg=green
@ -595,7 +667,7 @@ hi newLine guifg=green
### Region conceal ### Region conceal
``` ```vim
syn region inBold concealends matchgroup=bTag start="<b>" end="</b>" syn region inBold concealends matchgroup=bTag start="<b>" end="</b>"
hi inBold gui=bold hi inBold gui=bold
hi bTag guifg=blue hi bTag guifg=blue
@ -603,7 +675,7 @@ hi bTag guifg=blue
### Syntax ### Syntax
``` ```vim
syn match :name ":regex" :flags syn match :name ":regex" :flags
syn region Comment start="/\*" end="\*/" syn region Comment start="/\*" end="\*/"