Interacting with Vim

The execute command lets you parse and execute a string as a Vim command. For example, the two following statements will produce equivalent results:

echo animal . ' says hello'
execute 'echo ' . animal . ' says hello'

You can use normal to execute keys just like the user would in normal mode. For instance, if you wanted to search for a word cat and delete it, you could do this:

normal /cat<cr>dw
<cr> here needs to be typed with Ctrl + v, followed by the Enter key. However, execute "normal /cat<cr>dw" would use the literal string <cr> to represent Enter key press. Just a quirk to be aware of.

Running normal like this will respect the user's mappings, so if you want to ignore custom mappings, you could use normal!:

normal! /cat<cr>dw

Another command can suppress output from other commands (such as execute): silent. Neither of these will produce any output:

silent echo animal . ' says hello'
silent execute 'echo ' . animal . ' says hello'

Furthermore, silent can suppress the output from external commands and ignore prompts:

silent !echo 'this is running in a shell'

You can also check if the Vim you're running in has a particular feature enabled:

if has('python3')
echom 'Your Vim was compiled with Python 3 support!'
endif

You can view the full list of features via :help feature-list, but something worth noting is OS indicators: win32/win64, darwin (macOS), or unix. These are extremely helpful if you're planning to build a cross-platform script.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.53.209