map a command with vim

1

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?

bliof

Posted 2011-09-13T07:56:34.927

Reputation: 169

Answers

2

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>

garyjohn

Posted 2011-09-13T07:56:34.927

Reputation: 29 085