November 4, 2026

Learning vimscript

There is now a new dialect "vim9script", but you have to explicitly activate it. There is also Neovim and Lua, but we aren't going down that road. The universally recommended resource is: Typing ":help usr_41" in vim gives you vimscript explained by the author. Also recommended is :help eval, as well as :help vim9 if you are interested.

Some tips from a neophyte

Some tips to get started. You can just type vimscript on the command line. (The command line being that bottom line inside of vim). You can use "source file" on the command line to pull in and run scripts.

You can use "echo" to get information. There is also "echom" which also adds the output to a list you can display using ":messages" (or :mes)

Comments are anything after a double quote (")
The "pipe" symbol separates commands, allowing several on a line (|)
To make the current line "continue" the prior line, start it with backslash (\)

Vimscript has variables, strings, lists, dictionaries, regex, functions, for loops, while loops, exception handling

What I know so far

I have this line in my .vimrc
:let mapleader = "-"
This allows a variety of commands like the following:
" generate empty h3
:nnoremap <leader>h o<h3></h3><esc>hhhh
" capitalize the current line
nnoremap <leader>c :.!tr '[a-z]' '[A-Z]'<CR>
The following runs an external command on the current line.
" swap bytes
nnoremap <leader>t :.!./vim_swap<CR>
The comma is thus far unused in vim, and can be used as a single keystroke way to do something, such as this, which calls a vimscript function "CheckAB()".
noremap , :call CheckAB()<CR>

Have any comments? Questions? Drop me a line!

Tom's Electronics pages / tom@mmto.org