Vimscript Reference: Comprehensive Developer's Guide

" ---------------------------------------------------------------------------------
" 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('
            
Licensed under
CC BY-SA 4.0
) 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('
Licensed under
CC BY-SA 4.0
, "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('
Licensed under
CC BY-SA 4.0
, "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!
Licensed under
CC BY-SA 4.0