Vim Skills Training Course (12) - Vim's scripting language support

Vim scripting language support

At the beginning of this section, we are officially exposed to vimscript, an ancient scripting language.
First of all, there are many extensions supported by vim, such as python, Python 3, ruby, lua, TCL and other common scripting languages. It can support scripts embedded in. vimrc or execute files in scripting languages such as python.
Run the: version command to see which extension languages the current vim release holds:

VIM - Vi IMproved 8.0 (2016 Sep 12, compiled Jun 22 2017 03:02:45)
MacOS X (unix) version
Included patches: 1-648
Compiled by travis@Traviss-Mac-712.local
Huge version with MacVim GUI.  Features included (+) or not (-):
+acl             +cmdline_compl   +digraphs        +folding         +lambda          +mouse           +multi_lang      +profile         +statusline      +timers          +wildignore
+arabic          +cmdline_hist    +dnd             -footer          +langmap         +mouseshape      -mzscheme        +python/dyn      -sun_workshop    +title           +wildmenu
+autocmd         +cmdline_info    -ebcdic          +fork()          +libcall         +mouse_dec       +netbeans_intg   +python3/dyn     +syntax          +toolbar         +windows
+balloon_eval    +comments        +emacs_tags      +fullscreen      +linebreak       -mouse_gpm       +num64           +quickfix        +tag_binary      +transparency    +writebackup
+browse          +conceal         +eval            -gettext         +lispindent      -mouse_jsbterm   +odbeditor       +reltime         +tag_old_static  +user_commands   -X11
++builtin_terms  +cryptv          +ex_extra        -hangul_input    +listcmds        +mouse_netterm   +packages        +rightleft       -tag_any_white   +vertsplit       -xfontset
+byte_offset     +cscope          +extra_search    +iconv           +localmap        +mouse_sgr       +path_extra      +ruby/dyn        -tcl             +virtualedit     +xim
+channel         +cursorbind      +farsi           +insert_expand   +lua/dyn         -mouse_sysmouse  +perl/dyn        +scrollbind      +termguicolors   +visual          -xpm
+cindent         +cursorshape     +file_in_path    +job             +menu            +mouse_urxvt     +persistent_undo +signs           +terminfo        +visualextra     -xsmp
+clientserver    +dialog_con_gui  +find_in_path    +jumplist        +mksession       +mouse_xterm     +postscript      +smartindent     +termresponse    +viminfo         -xterm_clipboard
+clipboard       +diff            +float           +keymap          +modify_fname    +multi_byte      +printer         +startuptime     +textobjects     +vreplace        -xterm_save

For example, the macVim I use supports python, Python 3, ruby, lua, Perl and so on, but does not support tcl.

However, no matter what version of vim is, vimscript must be supported. Let's first look at the usage of various languages, and finally we have to learn vimscript.

Writing vim plug-in in Python language

Use of the python command

One-line statements can use the format python {one-line statements}
Multi-line statement usage

:python << EOF
 Multiline python statement
EOF

You can also write python code to a file and load it with the: pyfile command.

First, we need to introduce vim packages:

        :python import vim

Execute the ex command

Execute the ex command through the vim.command function:

        :py vim.command(cmd)            # execute an Ex command

Example:

:py vim.command(":normal! 2j")

This is a familiar one, simulating two j in normal mode.

Window correlation

  • vim.current.window: Get the current window
  • vim.windows[n]: Get the nth window
        :py w = vim.windows[n]          # gets window "n"
        :py cw = vim.current.window     # gets the current window

After obtaining the window object, you can operate on the window:

  • The height attribute is the window height
  • width:Width
  • Cursor property sets the cursor position in the window
  • buffer: buffer in the window
        :py w.height = lines            # sets the window height
        :py w.cursor = (row, col)       # sets the window cursor position
        :py pos = w.cursor              # gets a tuple (row, col)

Buffer correlation

All text is buffer-related.

  • vim.current.buffer to get the current buffer
  • vim.buffers[n]: Get buffer number n
        :py b = vim.buffers[n]          # gets buffer "n"
        :py cb = vim.current.buffer     # gets the current buffer

Once we get the buffer, we can manipulate the text.

  • Name: Buffer name, usually the same as file name
  • append: Add rows
  • mark: mark a piece
  • Range: Get a range

Once the buffer is acquired, it can be accessed as an array to process the text.
The following examples are given:

        :py name = b.name               # gets the buffer file name
        :py line = b[n]                 # gets a line from the buffer
        :py lines = b[n:m]              # gets a list of lines
        :py num = len(b)                # gets the number of lines
        :py b[n] = str                  # sets a line in the buffer
        :py b[n:m] = [str1, str2, str3] # sets a number of lines at once
        :py del b[n]                    # deletes a line
        :py del b[n:m]                  # deletes a number of lines
        :py b.append("bottom")          # add a line at the bottom
        :py n = len(b)                  # number of lines
        :py (row,col) = b.mark('a')     # named mark
        :py r = b.range(1,5)            # a sub-range of the buffer

current object

For the most part, we operate on the current object:

  • vim.current.line: Current line
  • vim.current.buffer current buffer
  • vim.current.window current window
  • Current range selected by vim.currrent.range

python3

python 3.x support is another different module. Its usage is basically the same as python.

Writing vim plug-ins in ruby language

Let me give you an example.

  :ruby print VIM::Window.count 

Execute the ex command

        VIM.command(cmd)                      # execute an Ex command

Window correlation

        num = VIM::Window.count               # gets the number of windows
        w = VIM::Window[n]                    # gets window "n"
        cw = VIM::Window.current              # gets the current window
        w.height = lines                      # sets the window height
        w.cursor = [row, col]                 # sets the window cursor position
        pos = w.cursor                        # gets an array [row, col]

Buffer correlation

        num = VIM::Buffer.count               # gets the number of buffers
        b = VIM::Buffer[n]                    # gets buffer "n"
        cb = VIM::Buffer.current              # gets the current buffer
        name = b.name                         # gets the buffer file name
        line = b[n]                           # gets a line from the buffer
        num = b.count                         # gets the number of lines
        b[n] = str                            # sets a line in the buffer
        b.delete(n)                           # deletes a line
        b.append(n, str)                      # appends a line after n
        line = VIM::Buffer.current.line       # gets the current line
        num = VIM::Buffer.current.line_number # gets the current line number
        VIM::Buffer.current.line = "test"     # sets the current line number

Keywords: vim Python Ruby Windows

Added by objNoob on Thu, 13 Jun 2019 23:08:10 +0300