-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvimscript_reference.vim
More file actions
873 lines (721 loc) · 29.2 KB
/
vimscript_reference.vim
File metadata and controls
873 lines (721 loc) · 29.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
" ---------------------------------------------------------------------------------
" Vimscript Reference and Guide
"
" ReferenceCollection.com
" Licensed under CC BY-SA
" ---------------------------------------------------------------------------------
" TABLE OF CONTENTS
" -----------------
" 1. Introduction to Vim Script
" 2. Basic Syntax and Structure
" 3. Variables and Data Types
" 4. Operators and Expressions
" 5. Control Flow Statements
" 6. Functions (Built-in and User-defined)
" 7. Working with Buffers and Windows
" 8. Working with Text
" 9. User Interaction
" 10. Autocommands
" 11. External Commands and System Interaction
" 12. Error Handling
" 13. Debugging Vim Script
" 14. Advanced Topics
" 15. Common Vim Script Patterns
" ---------------------------------------------------------------------------------
" 1. Introduction to Vim Script
" ---------------------------------------------------------------------------------
" Vim script (also known as VimL or simply script) is the built-in scripting
" language of the Vim text editor. It allows users to automate tasks, extend
" Vim's functionality, and customize the editor to their specific needs.
" Why Learn Vim Script?
" - Automation: Automate repetitive editing tasks, saving time and effort.
" - Customization: Tailor Vim's behavior, appearance, and functionality.
" - Plugins: Develop your own Vim plugins or contribute to existing ones.
" - Deeper Understanding: Gain a more profound understanding of how Vim works.
" Where to Use Vim Script:
" - `.vimrc` or `_vimrc`: Your main Vim configuration file.
" - Plugin files: To implement the logic of your plugins.
" - Command-line execution: Execute Vim scripts from the terminal.
" - Dynamically during a Vim session: Enter and execute script commands directly.
" Basic Execution:
" - Sourcing a file: `:source <filename>` or `:so <filename>`
" - Executing a string: `:execute "echom 'Hello, Vim Script!'"`
" - Evaluating an expression: `:echo 1 + 1`
" ---------------------------------------------------------------------------------
" 2. Basic Syntax and Structure
" ---------------------------------------------------------------------------------
" Comments:
" --------
" Comments start with a double quote (").
" Commands:
" ---------
" Vim script consists of a series of commands, typically one per line.
" Commands are often prefixed with a colon `:`
" Case Sensitivity:
" ---------------
" Vim script commands are case-sensitive. `echom` is correct, `Echom` is not.
" Whitespace:
" -----------
" Generally, whitespace is ignored, but it improves readability.
" Line Continuations:
" -----------------
" To split a long command over multiple lines, use a backslash (`\`):
let very_long_variable_name = "This is a very long string " \
. "that spans multiple lines " \
. "using a backslash."
" Command Ranges:
" --------------
" Some commands can operate on a range of lines.
" `:10,20 delete` " Deletes lines 10 through 20.
" `:%s/old/new/g` " Substitute 'old' with 'new' globally in the buffer.
" `:'a,'b delete` " Delete lines marked with 'a' and 'b'.
" Command Modifiers:
" -----------------
" Commands can be modified using flags or options.
" `:silent !date` " Executes the external command 'date' silently.
" ---------------------------------------------------------------------------------
" 3. Variables and Data Types
" ---------------------------------------------------------------------------------
" Variables in Vim script are dynamically typed, meaning you don't need to
" declare their type explicitly.
" Declaration and Assignment:
" --------------------------
" Use the `let` command to declare and assign values to variables.
let my_string = "Hello"
let my_number = 10
let my_list = [1, 2, 3]
let my_dict = {'name': 'Vim', 'version': '9.0'}
" Variable Scope:
" --------------
" Vim script has several scopes for variables:
" - Global (`g:`): Accessible from anywhere in Vim.
" - Buffer-local (`b:`): Specific to the current buffer.
" - Window-local (`w:`): Specific to the current window.
" - Tabpage-local (`t:`): Specific to the current tabpage.
" - Function-local (`l:`): Only within the function where it's defined.
" - Script-local (`s:`): Only within the script file where it's defined.
" - Environment (`v:env`): Access environment variables.
" - Option (`&`): Access Vim options.
" - Register (`@`): Access register contents.
" - Mapping (`maplocalleader`, etc.): Access mapping settings.
" Accessing Variables:
" --------------------
" Use the variable name (prefixed with its scope identifier if needed).
echom g:my_string
let l:local_var = "Local"
echom l:local_var
" Data Types:
" ----------
" - Number: Integers and floating-point numbers.
let num1 = 10
let num2 = 3.14
" - String: Sequences of characters.
let str1 = "Vim script"
let str2 = 'Single quoted string' " No special interpretation.
" - List: Ordered collections of items.
let list1 = [1, "two", 3.0]
" - Dictionary: Unordered collections of key-value pairs.
let dict1 = {'name': 'Example', 'value': 100}
" - Funcref: References to functions.
function MyFunc()
echom "Function called"
endfunction
let my_func_ref = function('MyFunc')
call my_func_ref()
" - Job: Represents a running external command.
" - Channel: Represents a connection for asynchronous communication.
" Type Conversion:
" ---------------
" Vim script automatically performs type conversions where necessary.
let result = "10" + 5 " Result will be 15 (string "10" converted to number)
let str_num = 123 .. "" " Convert number to string (concatenation with empty string)
let num_str = str2nr("456") " Convert string to number. Returns 0 if not a valid number.
" ---------------------------------------------------------------------------------
" 4. Operators and Expressions
" ---------------------------------------------------------------------------------
" Operators in Vim script are similar to those found in other programming languages.
" Arithmetic Operators:
" --------------------
" `+` (addition), `-` (subtraction), `*` (multiplication), `/` (division),
" `%` (modulo), `^` or `**` (exponentiation).
let sum = 10 + 5
let product = 4 * 6
" String Operators:
" ----------------
" `.` (concatenation).
let greeting = "Hello, " . "Vim!"
" Comparison Operators:
" ---------------------
" `==` (equal), `!=` (not equal), `>` (greater than), `<` (less than),
" `>=` (greater than or equal), `<=` (less than or equal).
" Case sensitivity: `==?`, `!=?`, `>?`, `<?, `>=?`, `<=?` for case-insensitive.
if 10 > 5
echom "10 is greater than 5"
endif
" Logical Operators:
" -----------------
" `&&` or `and` (logical AND), `||` or `or` (logical OR), `!` or `not` (logical NOT).
if (10 > 5) && ("a" != "b")
echom "Both conditions are true"
endif
" List Operators:
" ---------------
" `+` (list concatenation), `[]` (indexing), `[start:end]` (slicing), `in` (membership).
let my_list = [1, 2, 3] + [4, 5] " Result: [1, 2, 3, 4, 5]
echom my_list[0] " Access the first element (index 0)
let sub_list = my_list[1:3] " Result: [2, 3, 4]
if 2 in my_list
echom "2 is in the list"
endif
" Dictionary Operators:
" --------------------
" `.` (access by key), `['key']` (access by key).
let my_dict = {'name': 'Vim', 'version': '9.0'}
echom my_dict.name
echom my_dict['version']
" Ternary Operator:
" ----------------
" `condition ? expr_if_true : expr_if_false`.
let result = (10 > 5) ? "Yes" : "No"
" ---------------------------------------------------------------------------------
" 5. Control Flow Statements
" ---------------------------------------------------------------------------------
" Control flow statements allow you to control the execution of your Vim script
" based on conditions or to repeat actions.
" `if` Statements:
" ---------------
" Executes a block of code if a condition is true.
if condition
" Code to execute if the condition is true
endif
" `if-else` Statements:
" --------------------
" Executes one block of code if the condition is true, and another if it's false.
if condition
" Code if true
else
" Code if false
endif
" `if-elseif-else` Statements:
" ----------------------------
" Allows for multiple conditions to be checked.
if condition1
" Code if condition1 is true
elseif condition2
" Code if condition2 is true
else
" Code if none of the conditions are true
endif
" `while` Loops:
" --------------
" Executes a block of code repeatedly as long as a condition is true.
let counter = 0
while counter < 5
echom "Counter: " . counter
let counter += 1
endwhile
" `for` Loops:
" ------------
" Iterates over items in a list or dictionary.
" Iterating over a list:
let my_list = ['a', 'b', 'c']
for item in my_list
echom "Item: " . item
endfor
" Iterating over a dictionary (items):
let my_dict = {'one': 1, 'two': 2, 'three': 3}
for [key, value] in items(my_dict)
echom "Key: " . key . ", Value: " . value
endfor
" Iterating over a dictionary (keys):
for key in keys(my_dict)
echom "Key: " . key
endfor
" `break` and `continue`:
" ----------------------
" - `break`: Exits the current loop.
" - `continue`: Skips the current iteration and continues to the next.
let counter = 0
while counter < 10
let counter += 1
if counter == 5
continue " Skip printing when counter is 5
endif
if counter > 7
break " Exit the loop when counter exceeds 7
endif
echom "Counter: " . counter
endwhile
" ---------------------------------------------------------------------------------
" 6. Functions (Built-in and User-defined)
" ---------------------------------------------------------------------------------
" Functions are reusable blocks of code that perform specific tasks. Vim script
" provides a rich set of built-in functions and allows you to define your own.
" Built-in Functions:
" -------------------
" Vim offers a vast library of built-in functions for various purposes. Refer to
" `:help functions` for a comprehensive list.
" Common built-in functions:
" - String manipulation: `strlen()`, `substring()`, `match()`, `substitute()`.
" - List manipulation: `len()`, `add()`, `remove()`, `sort()`.
" - Dictionary manipulation: `has_key()`, `get()`, `extend()`.
" - Buffer and window management: `bufnr()`, `win_getid()`, `wincmd()`.
" - User interface: `echom()`, `input()`, `confirm()`.
" - File system interaction: `readfile()`, `writefile()`, `filereadable()`.
" Example of using built-in functions:
let my_string = "Hello, Vim!"
echom strlen(my_string) " Output: 11
echom substring(my_string, 0, 5) " Output: Hello
" User-defined Functions:
" -----------------------
" Use the `function` keyword to define your own functions.
function! MyGreeting(name)
echom "Hello, " . a:name . "!"
endfunction
" Calling User-defined Functions:
" ------------------------------
" Use the `call` command followed by the function name and arguments.
call MyGreeting("User")
" Function Arguments:
" ------------------
" Arguments passed to a function are accessed using `a:`. The first argument
" is `a:1`, the second is `a:2`, and so on. `a:0` contains the number of arguments.
" `a:000` is a list of all arguments. Named arguments are also supported (see below).
" Function Return Values:
" ----------------------
" Use the `return` keyword to return a value from a function.
function! Add(num1, num2)
return a:num1 + a:num2
endfunction
let result = call Add(5, 3)
echom result " Output: 8
" Function Scope:
" ---------------
" By default, user-defined functions have global scope. To define a script-local
" function, prefix the function name with `s:`.
function! s:MyScriptLocalFunction()
echom "This is a script-local function"
endfunction
" Named Function Arguments:
" ------------------------
" You can define functions with named arguments, improving readability.
function! CalculateArea(width, height)
return a:width * a:height
endfunction
let area = CalculateArea(width=10, height=5)
echom area " Output: 50
" Variable Number of Arguments (`...`):
" ------------------------------------
" Functions can accept a variable number of arguments using `...`. These arguments
" are available in the `a:000` list.
function! Sum(...)
let total = 0
for num in a:000
let total += num
endfor
return total
endfunction
echom Sum(1, 2, 3, 4) " Output: 10
" ---------------------------------------------------------------------------------
" 7. Working with Buffers and Windows
" ---------------------------------------------------------------------------------
" Vim's interface revolves around buffers (in-memory representations of files) and
" windows (viewports into those buffers). Vim script provides commands and
" functions to interact with them.
" Buffers:
" --------
" - `bufnr()`: Get the buffer number of a buffer (current, by name, etc.).
" - `bufname()`: Get the name of a buffer given its number.
" - `buflisted()`: Check if a buffer is listed.
" - `setbufvar()`: Set a buffer-local variable.
" - `getbufvar()`: Get a buffer-local variable.
" - `bufexists()`: Check if a buffer exists.
" - `buffer`: Switch to a buffer by number or name.
" Examples:
let current_bufnr = bufnr('%') " Get the buffer number of the current buffer
echom bufname(current_bufnr) " Get the name of the current buffer
let file_bufnr = bufnr('myfile.txt')
if bufexists(file_bufnr)
buffer myfile.txt
endif
" Windows:
" --------
" - `win_getid()`: Get the ID of a window.
" - `winbufnr()`: Get the buffer number displayed in a window.
" - `winnr()`: Get the number of the current window or a window by ID.
" - `winrestview()`: Restore the view of a window (cursor position, folds, etc.).
" - `winsaveview()`: Save the current view of a window.
" - `wincmd`: Execute a window command (e.g., `wincmd w` to switch windows).
" Examples:
let current_winid = win_getid()
let current_winnr = winnr()
let buf_in_current_win = winbufnr(current_winid)
echom "Buffer in current window: " . bufname(buf_in_current_win)
" Tab Pages:
" ----------
" - `tabpagenr()`: Get the number of the current tab page or the total number of tab pages.
" - `tabpagebuflist()`: Get a list of buffer numbers in a tab page.
" - `tabedit`: Open a new tab page with a file.
" - `tabclose`: Close the current tab page.
" Examples:
let current_tabnr = tabpagenr()
let total_tabs = tabpagenr('$')
let bufs_in_tab = tabpagebuflist(current_tabnr)
" Modifying Buffer Content:
" ------------------------
" Use commands like `execute`, `normal`, and `call` with functions that modify
" buffer content (e.g., `append()`, `deletebufline()`, `setline()`).
" Example: Append a line to the current buffer
call append('$', "New line appended by script")
" ---------------------------------------------------------------------------------
" 8. Working with Text
" ---------------------------------------------------------------------------------
" Vim script provides powerful ways to manipulate text within buffers.
" String Manipulation:
" -------------------
" - `strlen()`: Get the length of a string.
" - `substring()`: Extract a substring.
" - `match()`: Find the position of a pattern in a string.
" - `substitute()`: Replace occurrences of a pattern in a string.
" - `tolower()`, `toupper()`: Convert case.
" - `split()`: Split a string into a list.
" - `join()`: Join a list into a string.
" Examples:
let my_string = " Vim Script "
echom strlen(my_string) " Output: 14
echom substring(my_string, 2, 5) " Output: Vim
let pos = match(my_string, 'Script')
echom pos " Output: 4
let replaced = substitute(my_string, ' ', '_', 'g')
echom replaced " Output: __Vim_Script__
let words = split(my_string)
echom words " Output: ['Vim', 'Script']
" Regular Expressions:
" --------------------
" Vim uses its own powerful regular expression engine. Many commands and
" functions support regex for pattern matching and substitution. Refer to
" `:help pattern` for details on Vim's regex syntax.
" Using Regular Expressions:
" - `:s/pattern/replacement/flags`: Substitute command.
" - `match()`: Find a match in a string.
" - `filter()`: Filter a list based on a regex.
" Example: Substitute using regex
:%s/\s\+//g " Remove all spaces in the current buffer
" Working with Lines:
" ------------------
" - `getline()`: Get a line from a buffer.
" - `setline()`: Set a line in a buffer.
" - `append()`: Append a line after a specified line.
" - `deletebufline()`: Delete a line from a buffer.
" - `line()`: Get the line number of a position (e.g., cursor, last line).
" Examples:
let current_line = getline('.')
call setline(1, "This is the first line")
call append('$', "This is the last line")
call deletebufline('%', 2) " Delete the second line of the current buffer
" ---------------------------------------------------------------------------------
" 9. User Interaction
" ---------------------------------------------------------------------------------
" Vim script allows you to interact with the user through messages, prompts,
" and confirmations.
" Displaying Messages:
" -------------------
" - `echom`: Display a message without affecting the command-line history.
" - `echo`: Display a message (may affect command-line history).
" - `echon`: Display a message without a newline.
" - `redraw`: Force a screen redraw to show messages.
" Examples:
echom "This is a message using echom"
echo "This is a message using echo"
echon "This is part 1"
echon " and part 2"
redraw
" Getting User Input:
" -------------------
" - `input()`: Prompt the user for text input.
" - `inputsecret()`: Prompt for input without displaying it (for passwords).
" - `inputlist()`: Display a list and let the user choose an item.
" Examples:
let name = input("Enter your name: ")
echom "Hello, " . name
let password = inputsecret("Enter your password: ")
let choice = inputlist(["Option 1", "Option 2", "Option 3"], "Choose an option: ")
echom "You chose: " . choice
" Showing Confirmations:
" ---------------------
" - `confirm()`: Display a message with choices and return the user's selection.
" Example:
let choice = confirm("Are you sure?", "&Yes\n&No\n&Cancel")
if choice == 1
echom "You chose Yes"
elseif choice == 2
echom "You chose No"
else
echom "You chose Cancel"
endif
" ---------------------------------------------------------------------------------
" 10. Autocommands
" ---------------------------------------------------------------------------------
" Autocommands are events that trigger the execution of Vim script code
" automatically when specific events occur in Vim.
" Defining Autocommands:
" ----------------------
" Use the `autocmd` command to define autocommands. The syntax is:
" `autocmd [group] event pattern command`
" - `group`: An optional name for a group of autocommands.
" - `event`: The event that triggers the autocommand (e.g., `BufRead`, `BufWritePre`).
" - `pattern`: A file pattern (e.g., `*.txt`, `*.py`). Use `*` for all files.
" - `command`: The Vim script command(s) to execute.
" Common Autocommand Events:
" -------------------------
" - `BufNewFile`: When a new, not-yet-saved buffer is opened.
" - `BufRead`: Before reading a file into a buffer.
" - `BufReadPost`: After reading a file into a buffer.
" - `BufWritePre`: Before writing a buffer to a file.
" - `BufWritePost`: After writing a buffer to a file.
" - `BufDelete`: Before a buffer is deleted.
" - `VimEnter`: When Vim starts up.
" - `VimLeave`: When Vim exits.
" - `WinEnter`: When entering a window.
" - `WinLeave`: When leaving a window.
" Examples:
" Set the 'textwidth' option for text files
autocmd FileType text setlocal textwidth=78
" Trim trailing whitespace before saving
autocmd BufWritePre * :%s/\s\+$//e
" Create an autocommand group
augroup my_autocommands
autocmd! " Clear existing autocommands in the group
autocmd BufReadPost *.md setlocal spell
augroup END
" Deleting Autocommands:
" ----------------------
" - `autocmd!`: Delete all autocommands.
" - `autocmd! group`: Delete all autocommands in a specific group.
" - `autocmd! event pattern`: Delete specific autocommands.
" ---------------------------------------------------------------------------------
" 11. External Commands and System Interaction
" ---------------------------------------------------------------------------------
" Vim script allows you to execute external commands and interact with the
" operating system.
" Executing External Commands:
" --------------------------
" - `:!` or `system()`: Execute a shell command. `system()` returns the output.
" - `:silent !`: Execute silently (no output displayed).
" Examples:
!date " Execute the 'date' command and display the output
let output = system('ls -l')
echom output
" Interacting with the File System:
" ---------------------------------
" - `readfile()`: Read the content of a file into a list.
" - `writefile()`: Write a list of lines to a file.
" - `filereadable()`: Check if a file exists and is readable.
" - `getfsize()`: Get the size of a file.
" - `delete()`: Delete a file.
" Examples:
let lines = readfile('myfile.txt')
call writefile(["Line 1", "Line 2"], 'output.txt')
if filereadable('config.ini')
echom "config.ini is readable"
endif
" Working with Directories:
" -----------------------
" - `mkdir()`: Create a directory.
" - `exists()`: Check if a file or directory exists.
" Example:
if !exists('mydir')
call mkdir('mydir', 'p') " Create directory and parent directories
endif
" ---------------------------------------------------------------------------------
" 12. Error Handling
" ---------------------------------------------------------------------------------
" Handling errors gracefully is crucial for robust Vim scripts.
" `try-catch-finally` Blocks:
" -------------------------
" Used to catch and handle exceptions.
try
" Code that might raise an error
let result = 10 / 0
catch /E133:/ " Catch division by zero error (E133)
echom "Error: Division by zero!"
catch /.*/ " Catch any other error
echom "An unexpected error occurred"
finally
" Code that always executes, regardless of errors
echom "Cleanup done"
endtry
" Error Numbers and Messages:
" ---------------------------
" Vim errors have unique numbers (e.g., E133). You can catch specific errors
" by their error number or use a more general pattern (`/.*/`).
" Throwing Errors:
" ---------------
" You can use the `throw` command to raise your own errors.
if some_condition
throw "MyCustomError: Something went wrong"
endif
" ---------------------------------------------------------------------------------
" 13. Debugging Vim Script
" ---------------------------------------------------------------------------------
" Vim provides several tools for debugging your Vim script.
" `:echom` for Basic Output:
" -------------------------
" Use `echom` to print the values of variables or intermediate results.
" `:debug` Mode:
" -------------
" Start Vim in debug mode with `vim -D`. You can then step through your script.
" `:debug` Command within Vim:
" ---------------------------
" Use `:debug` followed by a command to execute that command in debug mode.
:debug source myscript.vim
" Debugger Commands:
" -----------------
" When in debug mode, you can use these commands:
" - `n` (next): Execute the next line.
" - `s` (step): Step into a function call.
" - `c` (continue): Continue execution until the next breakpoint or end of script.
" - `q` (quit): Quit debugging.
" - `i var`: Inspect the value of a variable.
" Breakpoints:
" -----------
" Set breakpoints in your script using `:breakadd func <function_name>` or
" `:breakadd file <filename>:<line_number>`. Clear breakpoints with `:breakdel`.
" List breakpoints with `:breaklist`.
" -------------------
" 14. Advanced Topics
" -------------------
" This section covers more advanced features of Vim script.
" Autoload Functions:
" ------------------
" Functions that are only loaded into memory when they are first called, improving
" startup time. Stored in specifically named files within the `autoload` directory.
" Assume you have a file `~/.vim/autoload/myutils.vim` with this content:
" autoload/myutils.vim
function! myutils#ToUpper(text)
return toupper(a:text)
endfunction
" Example usage in your script or `.vimrc`:
echom myutils#ToUpper("lowercase") " Output: LOWERCASE
" Dictionaries and Lists in Detail:
" ---------------------------------
" Advanced manipulation techniques, nesting, and using functions like `map()`,
" `filter()`, `sort()`.
let my_list = [1, 2, 3, 4, 5]
let doubled_list = map(my_list, 'v:val * 2') " [2, 4, 6, 8, 10]
let even_list = filter(my_list, 'v:val % 2 == 0') " [2, 4]
echom doubled_list
echom even_list
" Closures and `function()`:
" --------------------------
" Creating anonymous functions or functions that capture variables from their
" surrounding scope.
function! CreateMultiplier(factor)
return function('s:MultiplyBy', [a:factor])
endfunction
function! s:MultiplyBy(factor, num)
return a:factor * a:num
endfunction
let double = CreateMultiplier(2)
echom double(5) " Output: 10
" Timers and Asynchronous Operations:
" ---------------------------------
" Using timers (`timer_start()`) for delayed execution and channels/jobs for
" asynchronous tasks.
function! s:TimerCallback(timer_id)
echom "Timer fired!"
endfunction
call timer_start(1000, 's:TimerCallback') " Execute after 1000ms
" Partials (Using `bind()`):
" ------------------------
" Creating callable objects with pre-set arguments, useful for callbacks.
function! AddNumbers(a, b)
return a:a + a:b
endfunction
let add_five = function('AddNumbers')->bind(5)
echom add_five(3) " Output: 8
" Namespaces:
" -----------
" Organizing your functions and variables within modules to avoid naming conflicts.
" Public function accessible outside the namespace
function! myplugin#say_hello(name)
return "Hello, " . a:name . "!"
endfunction
" Private (script-local) function within the namespace
function! s:capitalize(word)
return toupper(a:word)
endfunction
" Remote Plugins (Using Channels and Jobs):
" ---------------------------------------
" Communicating with external programs to extend Vim's functionality.
" Start an external program (e.g., a simple Python script)
let job = job_start(['python3', '-c', 'import sys; print("Hello from Python", flush=True)'], {
\ 'on_stdout': { channel, msg -> echom "Received from Python: " . msg }
\ })
" Open a channel to the job
let chan = job_getchannel(job)
" Send a message to the job
call ch_sendexpr(chan, 'Vim message')
" Note: This is a simplified example.
" Real-world remote plugins involve more complex communication protocol
" Testing Vim Script:
" ------------------
" Techniques for writing unit tests for your Vim script code.
function! s:Double(n)
return a:n * 2
endfunction
function! TestDouble()
AssertEquals(4, s:Double(2))
echom "Test passed"
endfunction
" call TestDouble()
" ---------------------------------------------------------------------------------
" 15. Common Vim Script Patterns
" ---------------------------------------------------------------------------------
" This section highlights common patterns and idioms used in Vim script.
" Getting the Current File's Directory:
" ------------------------------------
function! s:GetFileDir()
return fnamemodify(expand('%:p'), ':h')
endfunction
" Checking if a Variable is Defined:
" ---------------------------------
if exists('my_variable')
echom "my_variable is defined"
endif
" Preventing Multiple Execution of a Script:
" ----------------------------------------
if exists('g:script_loaded')
finish
endif
let g:script_loaded = 1
" Saving and Restoring Cursor Position:
" ------------------------------------
let save_cursor = getpos('.')
" Perform some actions that might move the cursor
call setpos('.', save_cursor)
" Working with Command-Line Arguments:
" -----------------------------------
if argc() > 0
echom "Arguments passed: " . join(argv(), ' ')
endif
" Defining Mappings Programmatically:
" ---------------------------------
nnoremap <leader>f :call MyFunction()<CR>
" Creating Custom Commands:
" ------------------------
command! MyCommand :echom "Custom command executed"
" Defining User-Defined File Types:
" --------------------------------
autocmd BufNewFile,BufRead *.mytype setfiletype mytype
autocmd FileType mytype setlocal keywordprg=myhelp
" Handling Missing Features:
" ------------------------
if has('python3')
" Use Python 3 functionality
elseif has('python')
" Use Python 2 functionality
else
echom "Python support not available"
endif
" Happy scripting!