Vim mapping with user input

后端 未结 3 492
傲寒
傲寒 2021-02-05 12:55

I\'m wondering if it\'s possible in Vim to create a mapping (for normal mode) that allows user input before the mapping executes.

I want to create a mapping for a shortc

相关标签:
3条回答
  • 2021-02-05 12:59

    An alternative would be create a custom command and use a mapping to call the new command.

    command! -nargs=+ -complete=file -bar Grep grep! <args>|cw
    

    Now you can create your mapping:

    nnoremap <f2> :Grep<space>
    

    You probably also want to stay away from mapping the F key as it a pretty handy mapping.

    For more help see:

    :h :command
    
    0 讨论(0)
  • 2021-02-05 13:05

    see also this answer on vi.stackexchange by @EvergreenTree

    in short: map an expression to make use of input():

    nnoremap <expr> ;n "<ESC>Go<CR>" . input("Input your text here: ", "Prepend this string to my input by default -") . " <<- that was your input.<ESC>"

    0 讨论(0)
  • 2021-02-05 13:07

    There is a function input(prompt [, text [, completion]]):

    The result is a String, which is whatever the user typed on the command-line. The {prompt} argument is either a prompt string, or a blank string (for no prompt). A '\n' can be used in the prompt to start a new line.

    (from :help input()).

    For things like :grep!, you'll probably have to build the command using a string expression and then :execute it.

    0 讨论(0)
提交回复
热议问题