1

I have written the following in vimrc.vim

:filetype plugin on
:let s:save_cpo = &cpo
:set cpo&vim
:let &cpo = s:save_cpo
:set runtimepath = ~/home/nikcha/.vim/plugin
:set fileformat = unix
:function! print_hi()
:echo "Hi..its done"
:endfunction
:map <F7> : call print_hi()<CR>

On opening a file and typing :F7 it gives the error E488:Trailing Characters What could have gone wrong?

2

By using :map, you make the F7 mapping available to the Normal, Visual, Select, Operator-pending modes. By pressing :, you're entering command-line mode, which is not covered.

See :help map-modes for an overview.

As operator-pending mode is quite special and your example function also doesn't make sense on a selection, it's best to restrict it to (the default) normal mode via :nmap.

Additionally, you should use :noremap; it makes the mapping immune to remapping and recursion: :nnoremap.


If you want a shortcut for command-line mode, define a custom command instead:

:command! Hi call print_hi()
0
2

Colons

The colons at the beginning of your lines are optional. For scripts they are usually omitted as it makes the code cleaner/easier to read. The colon is however needed inside the mapping as explained later.

Setting values to options

When you set option values like fileformat and runtimepath you can not have a space between = and the value. It should be:

set fileformat =unix
" or
set fileformat=unix

note that this is not the case for let, which you use to assign values to variables, or by referring to an option. These are OK:

let s:foo = "bar"
let &fileformat = "unix"

Naming functions

When you write your own functions the name have to start with a capital letter. In your case something like:

function! Print_hi()
"         |
"         +----------- Capital

Compatible mode

Your save and restore cpo routine does not do much. The “normal” thing to do is to save it, then have your script, and at the end restore it.

let s:save_cpo = &cpo
set cpo&vim

" Rest of script

let &cpo = s:save_cpo

As your script is now, it only saves it, set it, then restores it in an adjacent sequence. In other words it can be omitted as a whole or you use the pattern as shown above.

Reason for this routine is that, in your script, you do not know if the user is in compatible mode, (More like vi, less like vim), or not. Therefore it is normal to save the mode, set mode to vim, then restore it at the end. This way you can use Vim-specific functionality without problems.

Rewritten script

As a whole, this should be more in the direction you want it:

let s:save_cpo = &cpo
set cpo&vim

function! Print_hi()
    echo "Hi..its done"
endfunction

map <F7> :call Print_hi()<CR>
"        |
"        +-------------------- Note; here we use colon.

let &cpo = s:save_cpo

The mapping, E488

The other answers have given a good enough explanation on why you get the

E488:Trailing Characters

error. In short: when mapped like this, only press F7. Do not press colon first. Ingo Karkat has some good advice on the map function to use.

The colon : in your mapping tells Vim to enter command-line mode. Then the command is printed call Print_hi(). Finally <CR> is added to execute the command line. Same as Enter if you wrote the command manually. In effect it becomes an automated-keyboard sequence.

More effective writing

Generally it is best to add scrips to their own files, then load the scripts from .vimrc. Not write lot of scripts / functions in .vimrc itself. When you write on a script and want to test it you can simply say:

  • :so %Enter

I have these mappings in my rc file for quick loading:

"^O is CTRL-O (i: Ctrl-V Ctrl-S-O; to prevent cursor movement
" Save + Source
inoremap    <silent>    <C-S-F12>  ^O:w<CR>^O:so %<CR>
nnoremap    <silent>    <C-S-F12>    :w<CR>:so %<CR>
" Source
inoremap    <silent>    <C-F12>  ^O:so %<CR>
nnoremap    <silent>    <C-F12>    :so %<CR>

Also have a function for only sourcing selected code, but as this post is becoming somewhat long ...

0

You are not supposed to type :<F7> but to press the F7 key instead. This is what the :map <F7> : call print_hi()<CR> means:

:map        # Map the following printable characters:
<F7>        # # How F7 is seen by vim
:           # to
call        # call a function:
print_hi()  # # previously defined function print_hi
<CR>        # and print a new line.

If you want to call the function in visual mode (typing :bla) you shouldn't use keys but type :call print_hi().

2
  • thanks for the answer....my bad ..I wrote type F7 , but actually in the command line mode in a file I am pressing :< F7 >
    – Nikita
    Jan 23 '15 at 11:05
  • You're still adding an extraneous colon :
    – wurtel
    Jan 23 '15 at 11:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.