Unable to understand a line in .vimrc - vim

I do not understand what the following line does in .vimrc
nmap <silent> <leader>v :EditConfig<cr>
It seems that
nmap mean noremap
silent seems to mean apparently no beep in Vim
leader seems to mean the first character in the mode :
v seems to mean visual mode
EditConfig should be a command in vim in the mode : (However, it is not.)
What does the line mean in .vimrc?

nmap means "map a key sequence when in normal mode" (see vim's docs).
<silent> tells vim to show no message when this key sequence is used.
<leader> means the key sequence starts with the character assigned to variable mapleader -- a backslash, if no let mapleader = statement has executed yet at the point nmap executes.
And the v is the rest of the key sequence.
So overall this is mapping, in normal mode, a backslash-v key sequence to show no message and execute :EditConfig which is likely a function defined previously in the vimrc to edit configuration files (see for example this vimrc, search in browser for editconfig). :call EditConfig() at the end (as the vimrc file I gave the URL to uses) would be better, I believe.

It would appear that you are missing a function...
Try,
function! EditConfig()
for config in ['$MYGVIMRC', '$MYVIMRC']
if exists(config)
execute 'edit '.config
endif
endfor
endfunction
Check this example.

Related

Setting mouse=a and mapping <esc> to :noh breaks mouse working on vim? [duplicate]

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

Vim key map not registering

I am trying to map Alt-r in Vim to save then execute the current file, and Alt-R to save then execute, but allow for extra parameters to be passed to the script.
In my vimrc I have the following (key mappings at the end of the file):
" Basic setup
set modeline
syntax on
filetype plugin indent on
" Load pathoen
execute pathogen#infect()
" Solarized colourscheme
if has('gui_running')
set background=light
else
set background=dark
endif
colorscheme solarized
" Map Alt-r to run current file as if it were a script
nmap <M-r> :w<CR>:!./%<CR>
nmap <M-R> :w<CR>:!./%<Space>
" Allow Alt-<CursorKey> to move around windows
nmap <M-Up> :wincmd k<CR>
nmap <M-Down> :wincmd j<CR>
nmap <M-Left> :wincmd h<CR>
nmap <M-Right> :wincmd l<CR>
The key mappings for window movement work fine, but Alt-r doesn't seem to do anything while Alt-R puts Vim into replace mode. I checked with :help M^r and :help M^R but they didn't list any mappings, nor does :map list those keys as taken. I have also already disabled Alt key mappings in the Gnome terminal.
Any suggestions??
This could be a problem with your terminal emulator which may encode the alt key different from what vim expects.
Ctrl+v causes vim (and most shells) to output the next character or key combination verbatim (literally). This way, you can insert the keycode produced when you press Alt+r directly into your configuration file by adding
set <M-r>=^[r
set <M-R>=^[R
where the escaped characters like ^[x have to be entered by pressing Ctrl+v and then Alt+x and may produce a different result on different terminal emulators.
Notice that there are no spaces around the equal signs. Vim would interpret additional spaces as part of the assignment which would make the directive misbehave.

Mapping <esc> in vimrc causes bizarre arrow behaviour

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

Mapping <C-j> to something in Vim

I have mapped Ctrl-j to move to the buffer below. However it was hard mapped
to something else so I had to write the first row below to make it mappable at
all.
The script below works perfectly, except when I write .sh files. I have
bash-support plugin installed.
let g:C_Ctrl_j = 'off'
map <C-j> <C-w>j
EDIT: I have just found out that the problem is in bash-support. Bash-support
maps Ctrl-j to jump insert (or something like that) which moves cursor at a
predefined point and goes to insert mode. I guess changing that in bash-support
script will do the job though I don't like messing with those files.
EDIT2: let g:C_Ctrl_j = 'off' actually turns the csupport plugin jump insert feature.
According to the document of bash-support.vim:
The original meaning of Ctrl-j is 'move [n] lines downward' (see |CTRL-j|).
If you are accustomed to use the default and don't like these jump targets you
can switch them off. Put the following line in the file '.vimrc' :
let g:BASH_Ctrl_j = 'off'
So that you have to put let g:BASH_Ctrl_j = 'off' into your vimrc.
Additionally, use nnoremap <C-j> <C-w>j instead of map <C-j> <C-w>j in your vimrc.
The latter defines key mappings in Normal, Visual and Operator-pending modes.
The key mappings are also recursively expanded.
It might cause troubles when you define more key mappings.
Ctrl-j (0x0A) is a special character, Line Feed.
There's a good chance that this key-press is not is not arriving at Vim as Ctrl-j (0x0A).
In Vim in a Terminal.app window on my Mac, typing Ctrl-v Ctrl-j displays ^#, (0x00 or NULL). Whereas Ctrl-v Ctrl-g and Ctrl-v Ctrl-k display ^G and ^K, respectively.
I'd suggest using another mapping (or just training yourself to use Ctrl-w j).
I had the same issue, but for a different reason. VIM-LaTeX uses imaps.vim plugin, which also remaps some keys, including Ctrl-j. What worked for me is to set an autocmd in my .vimrc:
augroup vimrc
au!
au VimEnter * unmap <C-j>
au VimEnter * noremap <C-j> <C-w>j
augroup END
The VimEnter trigger makes sure that the remap will executed after vim starts up, and the plug-ins are loaded (if I understand it correctly), so it overwrites the imaps.vim plug-in's maps. The unmap isn't necessary, but it can't hurt.
Try non recursive mappings,
nnoremap <C-j> <C-w>j
ought to do it.

Custom keys with NERDComment plugin and remapped Leader?

I'm trying to set up the NERDComment plugin in vim, but I'm having some trouble with the keys. I'd like to set the basic toggle functionality (comment a line if it's uncommented, uncomment if it's commented) to be c. The problem is that I've remapped the Leader to be ,, which is the same key that NERD wants for all of it's hotkeys. Anyone have any idea as to how to set this up?
Just call NERDComment function in your mapping. For example, my mapping to comment the current line:
inoremap ,c <C-o>:call NERDComment(0,"toggle")<C-m>
Here's a breakdown of how this vim remap works.
The i in inoremap means that the remap only applies in insert mode.
The noremap means that the remap can't be overridden later in your .vimrc file by accident, or by a plugin.
The ,c is the key combination that triggers the key map.
The <C-o> temporarily takes you out of insert mode for one command, so the next section of the remap can call the NERDComment function.
The :call NERDComment(0,"toggle") is the NERDComment function being called.
Then <C-m> is another way of saying carriage return, which executes the command.
If you want the comment shortcut to work in normal mode and visual mode, but not in insert mode where it might do something weird when you try to type a comma, you can use the following remaps:
nnoremap ,c :call NERDComment(0,"toggle")<CR>
vnoremap ,c :call NERDComment(0,"toggle")<CR>
documented method of remapping key is located here:
remapping documentation
reference
map <leader>d <Plug>NERDCommenterToggle
"silently rejects remap will not work
nnoremap <leader>d <Plug>NERDCommenterToggle
I fell into the pitfall of attempting to use "nnoremap" to remap on my first attempt resulting in unresponsive mapping. You must use "map", "nmap", etc to properly remap the function
:map <C-z> <plug>NERDCommenterToggle
Maps 'toggle comments' to ctrl+z

Resources