Tamás Szelei
Tamás Szelei

Reputation: 23921

How can I call an editor command from a vimscript?

I want to remove an unwanted scrollbar from taglist. I created a function and a command like this:

function s:TlistWaToggle()
    normal :TlistToggle<cr> " <- this does not work
    set guioptions-=r
endfunction

command! -nargs=0 -bar TlistWaToggle call s:TlistWaToggle()

I want to wrap the call to :TlistToggle together with the command to remove the right scrollbar (I have that setting of course, but it always reappears, so this is a workaround). Currently my :TlistWaToggle doesn't do anything. How can I make it work?

Upvotes: 6

Views: 1990

Answers (2)

ZyX
ZyX

Reputation: 53604

In addition to @sidyll's answer: :normal is not a :*map, it accepts only raw character strings. Correct command will be execute "normal! :TlistToggle\<CR>" (or execute "normal! :TlistToggle\n"). Note that you should not use non-banged version in your scripts.

I don't think you will ever use :normal! to execute an ex command, but my answer would be useful when you want to pass any other special character. It also applies to feedkeys() call.

By the way, comments and other commands will be considered part of string passed to :normal command.

Upvotes: 3

sidyll
sidyll

Reputation: 59277

Vim script uses ex commands, and apparently :TlistToggle is an ex command…

function! s:TlistWaToggle()
    TlistToggle
    set guioptions-=r
endfunction

Upvotes: 5

Related Questions