How can I map a command in vim which does something like:

:command (I write here and press enter) (Executes another command with the things that I wrote)

or

:badd my_buffer_name
:b my_buffer_name

I want to map this with "\ff" and it should work like this: \ff my_buffer_name. How can I map such commands?

link|improve this question
feedback

1 Answer

up vote 0 down vote accepted

Assuming that you want your mapping to execute one of those buffer commands, use this:

:map \ff :badd 

Make sure that you include a space character after ":badd". See

:help 05.3
:help map.txt

If you meant that your mapping should execute both of those commands, then use this:

:command -nargs=1 BuffAdd badd <args> <bar> b <args>
:map \ff :BufFAdd 

Again, include a space after ":BufAdd" in the mapping. See

:help 40.2
:help user-commands

For more complicated tasks or argument-handling, you could write a function. See

:help 41.7
:help user-functions

A function that included the use of the input() function could allow you to type \ff followed by the buffer name without seeing :BufAdd on the command line, like this:

function MyFunc()
    let my_buffer_name = input("Buffer name: ")
    exe 'badd' my_buffer_name
    exe 'b' my_buffer_name
endfunction
map \ff :call MyFunc()<CR>
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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