Call a function directly from the command line [duplicate] - vim

How do I define a function so that I can call it from command-line mode without :call in front of it?
Right now, I have to do this: :call TrimWhitespace()
I want to define it so that I can do this: :TrimWhitespace

This won't be a function, you should create a command instead. Check the documentation for commands (:help user-commands in Vim).
The simplest case is of a command to call a function:
command! TrimWhitespace call TrimWhitespace()

Related

.vimrc function - How to call "normal" mode keypress of equals (=) key

I am trying to write a function that I can call from a command, that will allow me to re-set all open panes to equal size. I started with this:
command Equal execute "normal! <C-w>="
Which allows me to call
:Equal
Which works the way I would expect it to. However, I need to add a little more functionality to this (an if statement, etc), so I need to declare it as a function. However, I cannot figure out how to call the "equals sign" key from within my function. Here is what I have:
command Equal call EqualizePanes()
function! EqualizePanes()
execute "normal! <C-w>="
endfunction
I know that my function declaration and method of calling it is correct, because I have replaced the contents of my function with something simple, like "echo foobar", which works as expected. However, as it stands, when I call :Equal, nothing happens. I have tried other things like:
command Equal call EqualizePanes()
function! EqualizePanes()
normal <C-w>=
endfunction
as well, without any luck. Any thoughts on what I am doing wrong here? Thanks in advance for your help.
Note that there's a special :wincmd Ex command that you can use instead of :normal; this avoids the :execute that is normally necessary to process the \<C-w> notation:
command Equal wincmd =
The problem is that normal doesn't parse special character sequences like <C-w>. So escape
command Equal call EqualizePanes()
function! EqualizePanes()
execute "normal! \<C-w>="
endfunction
:help expr-quote
:h execute
:h normal

vim function only works properly the first time

I wrote a function in vim to insert text in "paste"-mode. If I leave the insert mode, the script also leaves the paste mode (set nopaste). Therefore I used the autocommand InsertLeave.
The Problem is, that the "set paste" command works only the first time I call the function. If I want to call it once more I have to restart vim.
This is the vim function:
function Paste_from_clipboard()
execute "normal! :set paste\<CR>\<Esc>o"
execute "startinsert"
autocmd InsertLeave * execute "normal! :set nopaste\<CR>"
endfunction
map <Leader>p :call Paste_from_clipboard()<CR>
What did I do wrong?
I think you are misunderstanding how VimScript works. Every line (be it
on .vimrc, a plugin, a syntax file) is just an ex command, where the
starting : is not needed. So when you write this:
execute "normal! :set paste\<CR>\<Esc>o"
You're basically calling a ex command (:exec) which calls another ex
command (:normal) which then simulates normal mode to what? To call
yet another ex command (:set) and using key codes to execute it. Why?
You can just use the final ex command directly:
set paste
This is also happening in your auto command. Also, it is important to
notice that you're recreating an auto command every time you call your
function. A simple fix is then to remove your extra commands and move
the auto command outside the function, so it is created only once. The
execution will then happen every time the event is triggered (without
another event listener being created over and over.
function Paste_from_clipboard()
set paste
startinsert
endfunction
autocmd InsertLeave * set nopaste
map <Leader>p :call Paste_from_clipboard()<CR>
Check the :h pt for the pastetoggle option. It might be an
alternative to what you are doing.

automate and attach a VIM script to an event

I have a small vim function say myFunc() defined in my .vimrc and have this function mapped to to keyword in normal mode say cl and i am successfully able to call this function whenever i type cl in normal mode, myFunc() is invoked.
Now I want to go one step further, I want this function myFunc automatically called whenever press i to go from normal mode to insert mode in vim
Please suggest how can I achieve that.
I think you want to use an auto command on InsertEnter.
autocmd InsertEnter * call MyFunc()
Noter user defined functions must start with a capital letter.
Take a look at :help autocmd and :help InsertEnter.

Is it possible to use <C-R>= with a VIM command?

I have the following:
map <F6> :SCCompile<cr>
map! <F6> <c-r>=SingleCompile#Compile()<cr>
I'd like to use :SCCompile in the second mapping too, is that possible?
Currently I have another command which I really don't want to wrap in a function call. I use <c-r>= in insert mode because it doesn't create undo points and it aways works good except for this limitation.
I've tried execute but it's not working.
You can do something like this:
map <F6> :SCCompile<cr>
map! <F6> <c-r>=feedkeys("<c-o>:SCCompile\<lt>cr>")?'':''<cr>
The command isn't the simplest:
here we ask Vim to execute some keys after leaving <c-r>=
use <lt> to allow expanding of <cr> in command-line mode instead of in <c-r>=
use ?: operator to ignore value returned by feedkeys() function
See :help feedkeys().
You can built on my answer from your recent similar question. It's exactly the same issue: Like :call, a custom Vim command doesn't return anything, but <C-R> requires an expression that returns something:
function! SingleCompileWrapper()
SCCompile
return ''
endfunction
map! <F6> <C-R>=SingleCompileWrapper()<CR>

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?
Vim script uses ex commands, and apparently :TlistToggle is an ex command…
function! s:TlistWaToggle()
TlistToggle
set guioptions-=r
endfunction
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.

Resources