Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer

up vote 1 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>
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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