On MacVim, I'm trying to remap leader to ' (single quote), and change ' to \. However
let mapleader="'"
nnoremap \ '
is having no effect after :w and :so %. Nor does let mapleader="," seem to remap it to ,.
Am I doing it correctly? Any help? Thanks!
Changing mapleader only affects new mappings that use the <Leader> special key. So, you need to set this in your ~/.vimrc and restart Vim so that all plugins correctly pick this up.
If I understand you right, you then want to map \ to ', in order to use the jump mark command without the delay introduced by using that key for the Leader now, right? That should be fine.
Related
I have added set term=xterm to my vimrc to be able to use 256-color vim schemes in terminal, but it comes at a price (at least for me). It also replaces (sort of) the BackSpace with Delete (i.e. BackSpace starts to delete forward just like Delete does) in insert mode and makes it "BackSpace" instead of moving left (or h) in normal mode. I have nothing against Ctrl-H as a way "to Backspace", but I simply don't want to have two delete buttons and ability "to BackSpace" (delete backward) in normal mode.
How can I reverse that change while retaining the setting I need?
PS I've read :h CTRL-h and a bit of :h xterm, but I couldn't find the solution.
Vim's inoremap and nnoremap commands can be used to adjust how keys are interpreted in Vim.
A solution is documented here for your specific context: https://conemu.github.io/en/VimXterm.html
The relevant quote:
"If you have problems with BS in Vim (BS acts like Delete key) under ConEmu when term=xterm, you may try to remap BS key:
inoremap <Char-0x07F> <BS>
nnoremap <Char-0x07F> <BS>
"
In general, when a key does not do what you want, the trick is to find out what it actually sends to Vim. Sometimes hitting Ctrl-V followed by that key in insert mode might help figure it out. Then inoremap and nnoremap as shown above can be used to reassign it to the behaviour you want in insert and normal modes, respectively.
I am a happy VIM user, although I admit I'm quite far from being fluent. I found this nice post:
Vim clear last search highlighting and I thought I'd become a better person if I didn't have to hammer away a random character sequence every time I did a search. Now, I'm also using the vimrc config from here:
http://amix.dk/vim/vimrc.html
and the problem I have is that when I add the line nnoremap <esc> :noh<return><esc> to it (it doesn't seem to make a difference where I put it) I get awkward behaviour when I use arrows in command mode, namely letters from A to D appear in a newline and I get switched to insert mode.
There has to be some mapping conflict but for the life of me I can't figure out where it is.
EDIT: As it follows from the answers it turns out the Ultimate vimrc part is not relevant, the mentioned nnoremap command will cause altered arrow behaviour even if it's the only vimrc entry. Changing title to a more informative one.
PS. I know I shouldn't use arrows, hopefully I'll get there one day.
The mapping
nnoremap <esc> :noh<return><esc>
will conflict with so called "grey keys" and I believe that it should be used either in GVim only or in terminal Vim by someone who does not use special keys like arrows.
From what I know (and guess) how Vim processes keys, I would say that it's impossible to do anything with this. For Vim to recognize special key all its components should go in a row, so when you press Arrow Left Vim gets the following sequence of codes:
<esc> [ D
But after your mapping Arrow Left becomes:
: n o h l <cr> <esc>
[ D
Vim sees two separate sequences and treats <esc> as a single press of Escape key, thus next two codes of Left Arrow key lose their special meaning.
So I suggest you to map :noh to some other key sequence (e.g. to one starting with <leader>, see :help mapleader; I don't recommend you to use F-keys, using them is as bad as using of arrow keys).
The cause had been explained well, but solution was not mentioned. However there is a straight one.
If you’ll tell to Vim explicitly that there are key sequences starting from <esc>[
:nnoremap <silent><esc> :noh<CR>
:nnoremap <esc>[ <esc>[
than when single <esc> will be pressed Vim will wait for a second (or different time, see :h 'timeoutlen') or for a next key (second <esc> for example) and only then replace it with :noh<CR>.
This solution preserves the ESC mapping to :nohlsearch.
The comment on this answer explaining why this is happening tells us that the root cause is the TermResponse behavior of vim. This can be compensated for by wrapping the mapping in an autocommand for the TermResponse event.
This ensures that the binding doesn't happen until after the term response is set, which prevents Esc from also sending a string like ]>1;3201;0c to vim.
Change your line in vimrc to this:
augroup no_highlight
autocmd TermResponse * nnoremap <esc> :noh<return><esc>
augroup END
The augroup commands are not strictly necessary, but they prevent multiple mappings when you reload your vimrc without quitting vim.
EDIT: If you also use a graphical vim like Gvim or Macvim, the TermResponse event will not fire. Assuming you use a single vimrc, you'll need some additional code like
if has('gui_running')
nnoremap <silent> <esc> :nohlsearch<return><esc>
else
" code from above
augroup no_highlight
autocmd TermResponse * nnoremap <esc> :noh<return><esc>
augroup END
end
Problem is that when you press an arrow terminal emits something like <Esc>OA. Vim part that supports terminal seems to use the same mapping mechanism to do the job as you are using: while nmap <Esc>OA will tell you nothing, call feedkeys("\eOA") will move one line up and call feedkeys("\eOA", 'n') will add letter A beyond current line. With your mapping being noremappable you forbid vim to use <Esc> as a part of the key. The problem is that you need remappable mapping here, but can have remappable mapping without it being recursive as well only if it starts with {lhs}, but <Esc>:noh<CR>OA is not going to work. I thought the following code will (it uses <expr> and function with side effect to make <Esc> be the first character of the actual {rhs} and still launch :noh), but in fact it does not:
function s:NoHlSearch()
nohlsearch
return "\e"
endfunction
nmap <expr> <Esc> <SID>NoHlSearch()
. I have no other idea how to solve the problem of having non-recursive remappable mapping which includes {lhs} but not at the start.
I have had good luck with this
if $TERM =~ 'xterm'
set noek
endif
nnoremap <silent> <esc> <esc>:noh<cr>
The disadvantage is that function keys can not be used in insert mode.
:h ek
It took me almost two years of programming till I decided to switch Textmate for Vim and I love it so far. However, after playing with it for a few days I hit a first issue.
As a beginner I reached for Janus as many people do but in the end I decided to create my own configuration from scratch to get to know the stuff better. I backed my configs up and started writing my new .vimrc file. But later on (pretty early) I noticed that leader key isn't working, it does nothing when I press it, well it just beeps. I didn't change the key for a leader nor did any key mapping so I was kinda surprised.
So once again I removed my .vimrc file and .vim directory to start with a clean state. It didn't help. So I opened Vim and tried to reconfigure a leader to a different key to see if it helps.
:let mapleader
> E121: Undefined variable: mapleader
:let mapleader = ','
:let mapleader
> mapleader ,
Looks fine but nothing really happened. Even when I put it under a different key my Mac just beeps and thats it. There's no vim configuration in my home directory, no plugins, nothing. Setting leader in '.vimrc' instead of vim console doesn't help either.
I saw some discussions here on timeouts for key pressing but it have not got me anywhere.
I'm kinda stuck here and not able to use Vim for my day to day job even if I'd love to. Any help would be highly appreciated.
Follow these steps carefully…
Create a blank .vimrc file in your $HOME directory:
$ cd
$ touch .vimrc
Vim should now run in "nocompatible" mode which is what we all want.
Open your blank ~/.vimrc and add these lines:
let mapleader=","
nnoremap <leader>a :echo("\<leader\> works! It is set to <leader>")<CR>
Hit ,a, you should obtain the following message in the command line.
<leader> works! It is set to ,
<leader> may not be useful right from the start, though, there are other things to worry about.
Anyway, from there, I'd suggest you to add these few non-opinionated settings that will make your life considerably easier:
filetype plugin indent on
syntax on
set autoindent
set hidden
set incsearch
filetype plugin indent on allows Vim to recognize the filetype of the files you open and to apply all sorts of built-in filetype-specific settings (indent rules, completion…).
syntax on turns syntax highlighting on.
set autoindent copies the indent of the current line when you do <CR> life sucks so much if this is off.
set hidden allows you to open a new file or switch to another buffer without saving the changes to the current one.
set incsearch makes search-based navigation (/foo, ?bar) instantly awesome by turning incremental search on.
After that, it's up to you to add settings and mappings as you need them.
It's working, but the mappings preceding the change won't understand the changed leader character. For example, if I have a .vimrc that says
nnoremap <Leader>a :echo "test"<cr>
Then I can start up vim and hit \a to get test in the bottom left. However, if you type :let mapleader = ',' then do ,a it doesn't work because you changed leader after the mapping. If you then type :nnoremap <Leader>a :echo "test"<cr> and try ,a again it will work. Thus, :let mapleader = must come before mappings that use <Leader>.
I'd like to have a left and right hand leader key. If I want both the default \ and , to be my leaders I thought it would be as simple as adding nnoremap , \ or nnoremap , <leader> to my .vimrc. But apparently not. How do I do this?
My <leader> is bound to , and this works for me:
:nmap \ ,
All of my leader mappings are now available using either \ or , as the leader. I think it's the nnoremap that's tripping you up.
You can map one leader key to the other, as in the accepted answer, but if you're going to use <leader> in the first place, you should make the binding to <leader> itself. That way the binding will still work if you change (or remove) the first leader key.
map , <leader>
Note that this still doesn't quite work like a second leader. If the first leader is unset, the binding will still work, but Vim will also revert to using \ as a leader, since there is no longer an "official" leader (ie. valid value for the mapleader variable). (This wouldn't be a problem for the OP, but may be for others.)
<leader> is convenient but you can create mappings like ,mm or \mm without using it. Just duplicate all your <leader>something and remap them with ' and \ directly:
nnoremap <leader>d "_d
would become
nnoremap ,d "_d
nnoremap \d "_d
How exactly would that work? Vim, when it encounters <leader> it replaces it with the mapped key. Were you to use two (keys for <leader>), how would it know which one to replace <leader> with?
What you can do however, is use <leader> and <localleader> but that's just two separate leaders, not mapping two keys to one of them.
So, no ... you can't.
You can map your shortcuts explicitly though.
I see <leader> in many .vimrc files, and I am wondering what does it mean?
What is it used for?
Just a general overview of the purpose and usage would be great.
The <Leader> key is mapped to \ by default. So if you have a map of <Leader>t, you can execute it by default with \+t. For more detail or re-assigning it using the mapleader variable, see
:help leader
To define a mapping which uses the "mapleader" variable, the special string
"<Leader>" can be used. It is replaced with the string value of "mapleader".
If "mapleader" is not set or empty, a backslash is used instead.
Example:
:map <Leader>A oanother line <Esc>
Works like:
:map \A oanother line <Esc>
But after:
:let mapleader = ","
It works like:
:map ,A oanother line <Esc>
Note that the value of "mapleader" is used at the moment the mapping is
defined. Changing "mapleader" after that has no effect for already defined
mappings.
Be aware that when you do press your <leader> key you have only 1000ms or 1 second (by default) to enter the command following it.
This is exacerbated because there is no visual feedback (by default) that you have pressed your <leader> key and vim is awaiting the command; and so there is also no visual way to know when this time out has happened.
If you add set showcmd to your vimrc then you will see your <leader> key appear in the bottom right hand corner of vim (to the left of the cursor location) and perhaps more importantly you will see it disappear when the time out happens.
The length of the timeout can also be set in your vimrc, see :help timeoutlen for more information.
The "Leader key" is a way of extending the power of VIM's shortcuts by using sequences of keys to perform a command. The default leader key is backslash. Therefore, if you have a map of <Leader>Q, you can perform that action by typing \Q.
Vim's <leader> key is a way of creating a namespace for commands you want to define. Vim already maps most keys and combinations of Ctrl + (some key), so <leader>(some key) is where you (or plugins) can add custom behavior.
For example, if you find yourself frequently deleting exactly 3 words and 7 characters, you might find it convenient to map a command via nmap <leader>d 3dw7x so that pressing the leader key followed by d will delete 3 words and 7 characters. Because it uses the leader key as a prefix, you can be (relatively) assured that you're not stomping on any pre-existing behavior.
The default key for <leader> is \, but you can use the command :let mapleader = "," to remap it to another key (, in this case).
Usevim's page on the leader key has more information.
The default leader key is backslash(\). It's used in bindings as a chord with other keystrokes.