How do you send part of a command to vim(in Ex mode) using if_py?
I'm trying to open a file on Ctrl-f. If the command is typed in the presence of an empty buffer 'None' then I want the file to be opened in that buffer, but if there is no empty buffer I'd like to open a new buffer using :tabnew and then open the file in that. For this purpose I have a function OpenFile which is invoked.
function! OpenFile() python << EOF import vim import re buffer = vim.current.buffer name = str(buffer.name) if re.match('None', name): vim.command(':e ') else: vim.command(':tabnew') vim.command(':e ') EOF endfunction "Open file :map <C-f> :call OpenFile()<CR> :imap <C-f> <Esc>:call OpenFile()<CR>
vim.command executes the command so this is equivalent to :w!ENTER What I want to do is setup part of the command.. :e FILENAME ENTER
So I want to send the :e part in Ex mode via the python-function and get the user to type the filename and hit ENTER
First of all, why do you write this in Python? Sure, Vimscript is a bit strange (but since Vim 7 is has become a lot like Python), and you need to learn about the integration points, anyway, and this task has very little real logic in it.
This is easiest solved via a map-expression (:help map-expression):
:noremap <expr> <C-f> empty(bufname('')) ? ':edit ' : ':tabnew '
If you must, extract the conditional into a function and code it in Python, but I would recommend sticking to Vimscript, unless the logic is really complex or you could benefit from certain libraries.