Canceling searches for an empty string in vim - search

In my vimrc (I am using Nvim), I have mapped
noremap <C-f> <Esc>
noremap! <C-f> <Esc>
noremap / /\v
noremap ? ?\v
to automatically get very magic regex search. However, I often press / by mistake (or change my mind about searching), and then press C-f (and sometimes Enter) to cancel it. However, this highlights the entire file (since everything matches "\v"). Sometimes I remember to delete twice before pressing C-f, but this jarringly moves me to the next instance of my previous search (an empty search "").
I could press C-c or Esc to cancel the search, but I am not used to it and I feel like I would have to move my fingers in uncomfortable ways to do so. Would it be possible make this simpler and more comfortable? Two solutions I can think of, but don't know how to implement, would be
Remap C-f to work as Esc and C-c while searching.
and
Make some sort of alias or change some setting, so that searching for "\v" (empty regex) or "" (nothing) simply cancels the search.

The answer is simple:
Change your habit of "press Enter to cancel" an operation. Also don't press ctrl-f for searching.
Why you have this strange habit?
pressing Enter key after a command 90% will mean "Launch".
in Vim, ESC would be much more often pressed than Enter also, general speaking, Esc key should be the one to "cancel" an opeartion
what you meant "search nothing" in your comment, actually is not "search nothing", instead, it is "search anything"!
Mapping ctrl-f in normal mode is a bad idea, it does "page down", and ctrl-b for "page up". They are pretty useful functions I think.
Mapping ctrl-f in command/search mode is a bad idea too. ctrl-f brings you to the command window, there you can use full vim normal/insert commands to edit your commands. Very useful.
If you activated 'hlsearch', and want to remove the match highlighting, I have this in vimrc, you can try it:
"clear hl search by pressing ,/
nnoremap <silent> <Leader>/ :noh<cr>

I managed to solve it myself! Apparently, changing
noremap <C-f> <Esc>
noremap! <C-f> <Esc>
to
noremap <C-f> <C-c>
noremap! <C-f> <C-c>
did the trick. I am not quite sure why that is, especially since pressing Esc seems to work equally well to C-c.

Related

How to prevent Vim from jumping to next line, when auto-completing words using `C-P`?

When auto-completing words in Vim using C-P and pressing Enter to select the first candidate, without first using the arrow-keys to select between candidates, Vim will jump the cursor to the next line.
Conversely, if I first use the arrow-keys to select between candidates, Vim won't jump to the next line.
How can I prevent Vim from jumping to the next line, when selecting first candidate using Vim's auto-completion feature C-P?
When popup (completion) menu is active, you're supposed to use <C-N> <C-P> <C-E> <C-Y> instead of <Down> <Up> <Esc> <CR> respectively (note that all these keys behave a little differently). But if you really like it you can use a mapping:
inoremap <expr><CR> pumvisible() ? "\<C-Y>" : "\<CR>"
See also Improve completion popup menu Vim tip.

What does TAB do in vim?

I normally don't write tabs, so I mapped <TAB> to <ESC> in insert mode so that it's faster to leave insert mode. However, when I accidentally press TAB in normal mode, it jumps to some location in the file, without saving the jump location. I first suspected that a plugin is doing that, but I find no mapping for TAB in normal mode. Even explicitly unmapping it doesn't help, so it must be a built-in command. What does it do, why isn't this jump saved to history, and how do I disable it?
You're seeing the built-in :help CTRL-I; <C-I> and <Tab> are the same in (G)Vim. That command is the opposite of <C-O>; it goes to a newer position in the jump list. You don't see the "saving to history", because it operates on existing jumps.
If you don't want any action on Tab in normal mode, just put
:nnoremap <Tab> <Nop>
into your ~/.vimrc; :help <Nop>.
What does it do, why isn't this jump saved to history, and how do I disable it?
"Tab" (or <C-I>, which is really the same thing) goes forward through the jumplist ("undo" for <C-O>), so it's either no-op, or still there's nothing to save ;-) To see the embedded help topic use :h <tab>
If you really want to disable it, just remap it:
nnoremap <tab> <nop>

Assigning save (:w<cr>) to <leader>w in vim

I want to be able to save a file in vim while in the insert mode. I thought of using the following shortcut:
inoremap <leader>w <Esc>:w<cr>
While the shortcut saves the file in the insert mode, it leaves the cursor one spot ahead of where the cursor would be if I physically typed out the keys
Esc :w followed by Enter. This is a problem because when I use the shortcut whenever I am at the end of a line, it takes me to the next line, and I have to then come back to the spot where I initiated the save.
Any help would be appreciated on how I can map <leader>w to the exact actions that occur in Vim when I physically type out the Esc :w followed by Enter key sequence.
I should add that if I instead use the following key-mapping, things work exactly as I want:
inoremap <C-s> <esc>:w<CR>
However, I would like to avoid pressing CTRL and s at the same time. It is possible there is some problem with the <leader>, but I cannot figure out what it is (I am using , as my leader key).
Though one could discuss the suitability of your insert-mode mapping, the root cause of your problem is a trailing space in the mapping definition; i.e. Vim reads this as follows:
inoremap <leader>w <Esc>:w<cr><Space>
You'll even see this in the :imap <Leader>w output! <Space> in normal mode advances the cursor one to the right (like l); that explains the unexpected move.
Try this instead:
inoremap <silent> <leader>w <C-o>:w<CR>
The idea is Ctrl-o can be used to run commands directly from insert mode. See :help i_CTRL-O for details.
Why not simply doing
inoremap <leader>w <Esc>h:w<cr>
(not the additional h for going back one character)?

VIM navigate on insert mode

One of the things that bugs me the most about VIM is having to move while in insert mode. With any other programs I can use the arrow keys to move around but with VIM I have learned to use h/j/k/l and in order to enter that mode I have to press escape then I again, Is there a quicker way to do that?
I have my escape button mapped to jj.
imap jj <ESC>
That way when I want to enter normal mode fast I double tap jj and my fingers are in a good position to start navigating.
It may seem awkward to begin with but once you get muscle memory it will be like lightening.
You can temporarily drop out of insert mode by typing ^O to navigate. Useful to get past auto inserted closing brackets.
Most of the time vim understands the page movement buttons if your terminal is known to it. The original vi did not, and this command has always been there.
Useful muscle memory:
^Oo - stop and open line below, capital O is above
^OA - go to the end of the line and carry on inserting
^OI - Start inserting at beginning of line
As an aside:
I used to use vi on system V way back, at least 23 years ago. Personally find the idea of vi having a philosophy very funny. It was a pragmatic replacement for ex written by a lone coder in a hurry. Its pragmatism is why it survived because it was easy to port to any Unix. To get the best you should learn how to use f, t, comma and semicolon - they can save you a lot of effort when you use them with c.
use CTRL-[
here's a suggestion (probably from Bram) in the insert.txt helpfile for :h i_CTRL-[
"Note: If your key is hard to hit on your keyboard, train yourself to use CTRL-[."
You can use Ctrl+C to exit from the insert mode. Adding things to your vimrc file has a caveat: they don't work if you're on a remote server.
If you have a line in your vimrc file that looks like set term=ansi, comment it out. Once I did that I was able to navigate insert mode with arrow keys no problem.
If you want to move more than ONE or TWO positions...
...the best choice is to hit ESC, move around, and get back to insert mode again.
There's no point on create mappings like <C-h> to move left and then start hitting it too many times... as a vim user you're not supposed to hit the same key multiple times to achieve a smart movement.
(If the ESC key isn't close to your fingers, it would be a good option to create a mapping for it.)
If you want to move ONE or TWO posistions on insert mode...
...a good choice would be to define some movements using your <Leader> key:
(I use , as <Leader> key since it's feels close and confortable to my fingers)
noremap! <Leader>h <left> "move cursor left
noremap! <Leader>j <down> "move cursor down
noremap! <Leader>k <up> "move cursor up
noremap! <Leader>l <right> "move cursor right
noremap! <Leader>w <esc>wi "move one word forward
noremap! <Leader>e <esc>ei "move forward to the end of word
noremap! <Leader>b <esc>bi "move one word backward

Vim: command-line grep-like search with Ctrl-P and Ctrl-N

Basically, I don't want to use my arrow keys completely.
But for that to happen the Ctrl-P needs
to have the same functionality in
the command-line as my Up arrow key.
For Example, I type
:so
then I press Up, it turns like so
:source $MYVIMRC
but when I press Ctrl-P, it only goes to the last executed
command
:h camelcasemotion
Can anyone help me with this? It would be much appreciated.
In command mode, <Up> does “move to the previous command in the history that matches the string entered so far”.
In command mode,
CTRL-P has two functions.
After a Tab completion that has multiple possibilities, it does “show the previous matching value”.
Think of it as backward Tab. Most terminals do not distinguish Tab from Shift-Tab, but it and CTRL-P are the same mapping in GUI versions of Vim.
Without a preceding Tab completion, it does “move to the previous command in the history” (without trying to match the text entered so far).
You can remap CTRL-P to be the same as <Up> if you really want to:
:cmap <C-P> <Up>
You might also want to remap CTRL-N, too:
:cmap <C-N> <Down>

Resources