I like to use hlsearch but I hate to keep everything highlighted after searching, to solve this problem I could simply use :nohlsearch or an abbreviation of it but that is still to much effort so I decided to try to do that on pressing escape. What I came up with is:
nnoremap <ESC> :nohlsearch<CR>
This works exactly as I want it to in GVim which I usually use for development but it does not work in vim.
If I search something in vim, press escape to deactivate the highlighting and use one of the arrow keys to navigate vim goes directly into insert mode and inserts a character on a new line.
As I never really came around to using h, j, k and l for navigation this is really annoying and I would like to know how I can make vim behave like gvim.
If you need more information you can find my entire vim configuration here.
Your problem is that when you press <Up> terminal sends something like <Esc>OA (you will see it if you type <C-v><Up> in insert mode) which is remapped to :nohlsearch<CR>OA. I do not know any solution except not mapping a single <Esc>, try either mapping to double <Esc>.
I created this map to disable search when press double <Esc>
nnoremap <silent> <Esc><Esc> :let #/ = ""<CR>
is :noh still too much work?
EDIT: I don't know about you, but I personally think :noh is easier than pressing Esc key, since I can press all the buttons without stretching my pinky too far (which is why I think the default mapping of Esc for going back to Command Mode from Insert Mode is a bit unfortunate). If you really use the :nohlsearch that much, you probably should remap it to something you can reach from the Home Area (i.e. regular letters, or numbers, or perhaps Ctrl-letters).
Anyway, typing the exact command you give works in my vim (on gnome-terminal). Are you sure you put the rule in .vimrc file, instead of .gvimrc? No luck after restarting vim? Try :source /path/to/config/file and see if that makes it to work.
Related
Currently, when I press cursor-left in the command line in neovim (:whatever foo bar), the cursor will move over a whole word. Most of the time, I just want it to move one character.
This shouldn't be the default behaviour. It's probably a plugin or something you once added to your vimrc at some point. Try using :verbose cmap to see what it's set to (also see How do I debug my vimrc file?).
You should also be able to use :cnoremap <Left> <Left> to restore the default behaviour.
This has to do with putty and how it sends cursor keys. The trick is to find out what nvim actually sees, and then :cnoremap... accordingly.
The way to figure out what your nvim receives, is this:
enter input mode
hit ctrl-v
hit the key you want to see
Be aware that nvim behaves slightly differently than vim here: vim shows you the actual escape sequence, while nvim shows you the translated keys. The latter is a tad unfortunate.
In my case, for reasons I haven't figured out, nvim saw Ctrl-Left as , but Left as . Since I never need S-Left in the command line, :cnoremap <S-Left> <Left> did the trick.
I wanted to add a way to run :noh that made the most sense to me, so I added the following
nnoremap / :noh<CR>/
nnoremap ? :noh<CR>?
So far it's working exactly how I expected (and want): hilights are cleared as another search is started and typing /<backspace> makes more sense for me than <leader><space> or similar.
My concern is that this will somehow break other useful commands or a plugin.
Anyone know if I'm safe doing this? Thanks.
What you can break in third party plugins
Every mapping that you define can break plugins, if they are badly written, or if they rely on mappings that you have redefined.
Consider a plugin that uses map instead of noremap or normal instead of normal!: if the right-hand side of that mapping or the normal mode commands include /, then your mapping will be triggered.
Still, it wo'nt break much, it's just a matter of display.
What you can break in a normal use of vim
But there is still a case where you break something: Try this
iI am typing in insert modeCTRL-O/helloEnter
Normally, CTRL-O in insert mode temporarily switches to normal mode for one command. You've just broken that, because the call to :noh consumes that command.
You still can do it this way:
function ResetPattern(forward)
noh
redraw
return a:forward ? '/' : '?'
endfunction
nnoremap <expr> / ResetPattern(1)
nnoremap <expr> ? ResetPattern(0)
I think it's safe. But, it will lose some functionality.
For example:
Type /helloEnter will highlight all hello
Type /world to inc-search, then type ESC to return where you are. But the original highlight is lost.
I just have this mapping in my .vimrc:
nmap <BS> :noh<CR>
Normal mode mapping <C-l> usually refreshes the screen. I have modified it to clear highlightings. Easily available and comes from a reflex nowadays. Doesn't destroy any functionality and clearing the highlights can be thought as a part of the screen refreshing.
nnoremap <C-l> :nohl<CR><C-l>
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
When I'm escaping from insert mode with either <esc> or jj the cursor moves one character backwards, which, I guess, is the typical behavior for Vim or MacVim GUI.
I tried solving the problem by using inoremap jj <esc>l but the problem there is that when I'm at the end of the line the cursor jumps to the next line, which is even stranger.
I know I can go around this issue by getting used to a instead of i to jump back to insert mode, but first I want to be sure there is no other workaround.
Do you have any suggestions?
Sorry because this is not what you would be expecting, but you probably should get used to it instead of remapping it. In Vim, in normal mode, your cursor is not between characters but on characters. Traditional editors do not have a normal mode, you are always inserting and thus you need to see a cursor between characters.
If you still really want to do that, set virtualedit to onemore.
just simply inoremap jj <esc>
I'm not a big fan of Ctrl-n, I'd like to be able to use Ctrl-Space. Any ideas how I can do that?
if has("gui_running")
" C-Space seems to work under gVim on both Linux and win32
inoremap <C-Space> <C-n>
else " no gui
if has("unix")
inoremap <Nul> <C-n>
else
" I have no idea of the name of Ctrl-Space elsewhere
endif
endif
I like the Ctrl+Space mapping as well.
" Remap code completion to Ctrl+Space {{{2
inoremap <Nul> <C-x><C-o>
In your case you want:
inoremap <Nul> <C-n>
If you wanted to map it to, say, Ctrl-E, it'd be something like:
inoremap <C-E> <C-N>
The problem with Ctrl-Space is that most terminals will just see it as a space. I'll assume you're using some terminal program inside X; if you're using something different, you'll have to supply the relevant substitutions yourself.
Bash's readline normally has Ctrl-V mapped to "treat the next key as literal." So pressing Ctrl-V then Home at a bash command line will insert ^[[H or something like that on the command line, rather than going to the start of the line. Try pressing Ctrl-V then Ctrl-Space. Probably you'll just see a space.
In that case, you'll have to fool with xmodmap or write your own /usr/share/X11/xkb/* files to tell X to output something different when you press Ctrl-Space. Programs like Firefox don't care; they detect which base key is being pressed and figure out for themselves what modifiers are being pressed. But most terminal-based programs will just see Ctrl-Space as a Space unless you tell X to treat Space and Ctrl-Space differently.
I doubt you can make this change with xmodmap alone; you'll probably need to do the lower-level /usr/share/X11/xkb/* hacking. This is complicated, and I don't even know if you're using X in the first place, so I'll just leave it there.
Some terminals like urxvt let you specify your own keybindings. Like Firefox, they can tell when it's a Space and when it's a Control-Space even without you doing anything special to configure X. So you could tell urxvt to output "\033I_TYPED_CONTROL_SPACE_DAMMIT" when you press Ctrl-Space. And then you could tell vim to map that to <C-N>.
EDIT: I forgot that Ctrl-Space used to output \0 (I remapped that somewhere else on my keyboard). In that case, all the complexity I described above is unnecessary. What I said would apply to someone who wanted to use a more exotic mapping, like Ctrl-colon or Alt-Space.