How to map :Ag command to :ag - vim

I am using the plugin ag.vim in Vim, and I want to do a simple re-mapping.
When I run ag with :Ag I want remap it to :ag so I don't have to use shift.
example: :ag string_to_find instead of :Ag string_to_find
How can I do this?

You shouldn't. Lowercase commands are reserved for built-in commands, and the norm/rules are to start custom commands with an uppercase character (just like with functions).
See Is it possible to remap an Ex command in Vim (remap :Ack to :ack)?

This isn't exactly what you asked for, but dding the following will allow you to just type 'ag' to search:
map ag :Ag

EDIT: The downvoters are right and this is a bad answer. I'll leave it here so future searchers know it's wrong.
Try:
command: Ag ag
See :help command for more.

Related

Use capital Q also to quit Vim

Tried using the following in my .vimrc, but somehow it isn't working. Also, is there any down sides to using "Q" also along side the usual q to quit the editor. Apparently, I find stumbling on this issue a lot, but don't see this tweak is most .vimrcs.
noremap Q :quit<CR>
If you want to quit Vim with :Q, a simple mapping won't do, because you then won't be able to type Q in the command-line (which is the mode you would have to map, using :cnoremap).
You can either just define an uppercase command:
:command! -bar -bang Q quit<bang>
or use the technique described in aliasing a command in vim, which also works for lower-case. Or have a look at cmdalias.vim - Create aliases for Vim commands.
If you want to quit vim with :Q just add this to your vimrc
:command Q q

Where to find a list of special character representations?

For example backspace is <BS> and tab is <Tab>, but where can I look them up if I don't know or remember the sequence for, say, the up or down arrow?
Two :helpful tricks:
In insert mode and in the command line, hitting <C-v> followed by some key inserts that key's internal notation.
For example,
:helpCtrl+v↑
produces
:help <Up>
The :help command supports completion so you can type a keyword related to what you want and hit <Tab> or <C-d>:
:help key<Tab>
I was going to ask this, but then I found the answer by random luck in the help files. So here it is for those who are looking for the same:
:help keycodes

Searching vim history for matching first characters

In bash, adding the lines
"\e[B": history-search-forward
"\e[A": history-search-backward
to my .inputrc, allows me to search the history for expressions that begin with the characters in front of my cursor by using the <page-up>/<page-down> keys.
Can I achieve something similar in vim?
I already know about the possibility of opening a history window with q: and performing even complex searches there, but I am looking for a simple solution for the simplest case of history search.
Thank you!
This is built-in as <Up> and <Down>.
Of course, you can customize this, e.g.:
:cnoremap <PageUp> <Up>

Vim mapping with user input

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?
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.
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
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>"

Omit 'Pattern not found' error message in Vim script

I added a function in my .vimrc that's just few search and replace commands. However if one of the patterns is not found I get an error message that I need to enter through. How can I suppress this?
You can either use :silent or :silent! as a prefix to any command or you can add the 'e' option to the substitute, which is often easier.
:%s/x/y/ge
:silent! %s/x/y/g
:silent %s/x/y/g
For more information, see
:help :silent
:help :s_flags
The information on the e flag is a few paragraphs down from the :s_flags help.
You might use silent:
:silent %s/x/y/g
or, if you need to do string manipulation to determine the strings to search and replace:
exec ":silent %s/x/" . varName . "/g"
I'm not 100% sure, but I think that silent only works in scripts.
Probably a little bit off topic but i found this thread when searching for solutions for "search pattern not found" errors in vim, when i wanted to search for the next occurence in vim. The following map omits the error when pressing 'n'
map n :silent! /
Maybe the :he :silent thing can be used to suppress those errors?
this also works for global operation
:silent g/^$/d'

Resources