Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim mapping with user input

Tags:

vim

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 shortcut for my most used grep command. I want the command to allow to to enter what I'm searching for then execute on enter.

This is sort of what I want:

nmap F :grep! "*user input*"<CR>:cw<CR>

I can't figure out how to pause and take user input. Am I even thinking about this the right way? Are there better ways to do what I'm trying to?

like image 201
gylaz Avatar asked Jan 17 '13 21:01

gylaz


2 Answers

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.

like image 137
Anton Kovalenko Avatar answered Sep 28 '22 00:09

Anton Kovalenko


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
like image 43
Peter Rincker Avatar answered Sep 27 '22 23:09

Peter Rincker