"execute" command will give an error. The "!"
command will send everything after it to the shell, causing an error for an
unmatched '"' character.
There can be no comment after ":map", ":abbreviate", ":execute" and "!"
commands (there are a few more commands with this restriction). For the
":map", ":abbreviate" and ":execute" commands there is a trick: >
:abbrev dev development|" shorthand
:map <F3> o#include|" insert include
:execute cmd |" do it
With the '|' character the command is separated from the next one. And that
next command is only a comment. For the last command you need to do two
things: |:execute| and use '|': >
:exe '!ls *.c' |" list C files
Notice that there is no white space before the '|' in the abbreviation and
mapping. For these commands, any character until the end-of-line or '|' is
included. As a consequence of this behavior, you don't always see that
trailing whitespace is included: >
:map <F4> o#include
To spot these problems, you can set the 'list' option when editing vimrc
files.
For Unix there is one special way to comment a line, that allows making a Vim
script executable: >
#!/usr/bin/env vim -S
echo "this is a Vim script"
quit
The "#" command by itself lists a line with the line number. Adding an
exclamation mark changes it into doing nothing, so that you can add the shell
command to execute the rest of the file. |:#!| |-S|
PITFALLS
Even bigger problem arises in the following example: >
:map ,ab o#include
:unmap ,ab
Here the unmap command will not work, because it tries to unmap ",ab ". This
does not exist as a mapped sequence. An error will be issued, which is very
hard to identify, because the ending whitespace character in ":unmap ,ab " is
not visible.
And this is the same as what happens when one uses a comment after an "unmap"
command: >
:unmap ,ab " comment
Here the comment part will be ignored. However, Vim will try to unmap
',ab ', which does not exist. Rewrite it as: >
:unmap ,ab| " comment
RESTORING THE VIEW
Sometimes you want to make a change and go back to where the cursor was.
Restoring the relative position would also be nice, so that the same line
appears at the top of the window.
This example yanks the current line, puts it above the first line in the
file and then restores the view: >
map ,p ma"aYHmbgg"aP`bzt`a
What this does: >
ma"aYHmbgg"aP`bzt`a
< ma set mark a at cursor position
"aY yank current line into register a
Hmb go to top line in window and set mark b there
gg go to first line in file
"aP put the yanked line above it
`b go back to top line in display
zt position the text in the window as before
`a go back to saved cursor position
PACKAGING
To avoid your function names to interfere with functions that you get from
others, use this scheme:
- Prepend a unique string before each function name. I often use an
abbreviation. For example, "OW_" is used for the option window functions.
- Put the definition of your functions together in a file. Set a global
variable to indicate that the functions have been loaded. When sourcing the
file again, first unload the functions.
Example: >
" This is the XXX package
if exists("XXX_loaded")
delfun XXX_one
delfun XXX_two
endif
function XXX_one(a)
... body of function ...
endfun
function XXX_two(b)
... body of function ...
endfun
let XXX_loaded = 1
==============================================================================
*41.11* Writing a plugin *write-plugin*
You can write a Vim script in such a way that many people can use it. This is
called a plugin. Vim users can drop your script in their plugin directory and
use its features right away |add-plugin|.
There are actually two types of plugins:
global plugins: For all types of files.
filetype plugins: Only for files of a specific type.
In this section the first type is explained. Most items are also relevant for
writing filetype