I started using Vim recently, just installed NERDTree (a plugin to navigate files).
The command to access that plugin is :NERDTree so I though it's a good idea to start learning mappings by assigning one to that command.
So I added to my .vimrc file the following line: map :nt :NERDTree - but when I type :nt in a vim file (even after restarting) I receive the following error message: not an editor command: nt
I also tried to add the mapping directly while editing a file by typing :map :nt :NERDTree but it returned the same error when I tried to use the command.
I checked that answer:What is the difference between the remap, noremap, nnoremap and vnoremap mapping commands in vim?, so it seems to me that :map (opposed to noremap etc.) is the good command for that.
The plugin works fine when typing the original command.
What am I doing wrong? (sorry for the noob question)
:NERDTree is a command, not a mapping, so there's no reason for creating a recursive mapping, here.
:map is too overreaching. You should use :<mode>map (for recursive mappings) or :<mode>noremap (for nn-recursive mappings).
You are missing a <CR> at the end of your mapping to tell Vim to actually execute the :NERDTree command.
In this specific case, the right mapping would be:
nnoremap :tn :NERDTree<CR>
But mapping something to :<anything> is not a good idea because it will introduce a timeout whenever you try to execute an Ex command. This means that you need to find another combo. Why not <Space>n?
nnoremap <Space>n :NERDTree<CR>
With the mapping that you have, it will be require multiple keystroke. Will it be okay for you to use a single key like F2?
nnoremap <F2> :NERDTreeToggle<CR>
This will toggle open/close NERDTree upon pressing F2 and save you some key stroke.
Here
you can figure out, how vim's mapping work and look like ;). Don't forget to source your new .vimrc before using.
Related
I'm trying to create a mapping on Vim, that behaves like the following:
First of all I select a block of code or text using on Visual mode.
The I use this mapping to substitute the first column on each line for '#' effectively commenting each line.
Up until now I have the following:
vnoremap <Leader>c :normal! :s/^/#/<cr>
But for some reason it is not working. Nothing happens when I hit <Leader>c on a block of text. On the other hand, if I have:
vnoremap <Leader>c :normal! s/^/#/<cr>
it will for example subsitute:
The grey fox.
For
/^/#/he grey fox.
Any idea on how can I solve this issue?
:normal is an Ex command that allows to execute normal mode commands (from a custom command or function, or command-line mode in general). Your keys start off with :, so immediately switch from normal (or here: visual) mode to command-line mode. That doesn't make sense. Just define the mapping like this:
vnoremap <Leader>c :s/^/#/<cr>
The : will automatically insert '<,'> for you, and that's what you want here (to operate on all selected lines). You can also define a related normal-mode mapping that works on the current (or [count]) lines:
nnoremap <Leader>c :s/^/#/<cr>
If the highlighting disturbs you, append the :nohlsearch command:
nnoremap <Leader>c :s/^/#/<bar>nohlsearch<cr>
You can simply do:
vnoremap <leader>c :s/^/#/<cr>
The substitution command will get the range, '<,'>, automatically when in visual mode.
Note: you probably want to use xnoremap instead of vnoremap.
There is a better way
Commenting is a common problem, deceptively tricky, and already solved by many plugins. I prefer to stand on the shoulder's of giants and use a plugin. I personally use commentary.vim.
In my previous question I tried to "rewrite" default mapping of Netrw's NetrwRefresh command. The default mapping was <c-l> and I wanted to free it for different purpose. Suggested solution was the following:
nmap <unique> <c-r> <Plug>NetrwRefresh
" from now on I can use <c-l> for whatever I want
Everything is fine, but when I try to change <c-r> to <leader>xx every time I get this error:
Whatever I place after <leader> the error always occur. I tried to remove <unique> and I get the result. So the question is: what the <unique> is intended for?
As always, Vim's :help command should point you in the right direction. From :help unique, is documented:
If the first argument to one of these commands is "" and it is used to define a new mapping or abbreviation, the command will fail if the mapping or abbreviation already exists
So Vim is telling you that a mapping already exists for <leader>xx and refuses to overwrite it. To determine what is already using <leader>xx in your setup, you may view all mappings:
:map
It seems unusual and somewhat unlikely that anything you try following <leader> is already in use, so examining the output from :map should reveal some available combinations.
I use latex-suite to write .tex files. To easily switch between default compiler (pdflatex) and xelatex I have the following script in my _vimrc file.
function SetXeLaTeX()
let g:Tex_CompileRule_pdf = 'xelatex --interaction=nonstopmode -synctex=1 -src-specials $*'
endfunction
map <Leader>lx :<C-U>call SetXeLaTeX()<CR>
So in Vim, I can normally use \lx to call SetLaTeX() function. And actually I have similar scripts for other key-bindings such as \lp for pdflatex and \la for arara.
Everything works just fine until Voom outline command is called.
After runnig the command :Voom latex which generates Voom outline file .tex_VOOM1 alongside the .tex file, all these key mappings (\lx, \lp and \la) start behaving oddly:
They no longer calls my custom functions, instead they all trigger character-wise Visual mode.
It seems somehow Voom remapped all my commands. And I cannot over-ride it by running
:map <Leader>lx :<C-U>call SetXeLaTeX()<CR>
command in Vim's current session.
Can anybody tell me what is exactly wrong? How to fix this oddity?
Update
Strangely enough, I discovered that if I remove the <C-U> key in custom functions, no misbehavior is encountered. After this tweak, the \lx command will work as expected.
You should use :noremap; it makes the mapping immune to remapping and recursion.
Apparently, Voom defines a <C-u> mapping, and your :map commands use that, and it's wreaking havoc on them.
I'd like to map F2 in VIM so that it first saves the file with :w and then calls a script /home/user/proj/script.sh that will upload the changed files to the server.
I already tried
:map F2 :w<cr>|:! /home/user/proj/script.sh
but that doesn't work.
Please tell my why this isn't working and help me to get this working.
Try :noremap <F2> :<c-u>update <bar> !/home/user/proj/script.sh<cr>.
noremap vs. map
This creates a non-recursive mapping. See Wikia - Mappings keys in Vim
I just assumed this, because it's what people want most of the time. :-)
update vs. write
Only save the file if there were actual changes. See: :help :update.
<bar> vs. |
:help map_bar
<c-u>?
You used a generic mapping, instead of one for a certain mode, like nmap for normal mode mappings. Thus the mapping could also be triggered in visual mode. If you select something in visual mode and hit :, you'll see the commandline is prefixed with a range. But you don't want that in your case and <c-u> clears the commandline.
<cr> at the end?
Your mapping drops into the commandline mode and inserts 2 things separated by <bar>. Afterwards it has to execute what it has written, thus you need to append a <cr>.
I have noticed that when I use :cn :cp directly, vim always expands folds for me. But when I set my custom mapping to call :cn like :map <leader>n :cn<cr>, vim doesn't expand foldings when I use <leader>n in this case, I have to move horizontally after using that hotkey to unfold. I could script moving horizontally as well, but I still would like to understand why vim doesn't do that for me.
My foldopen setting contains quickfix.
Thanks.
According to this vim email archive.
This is the correct behavior and that you are supposed to open folds manually if you put :cn or :cp into mappings. The solution was to add zv to the end of the mappings.
So your maps should look like this
noremap <leader>n :cn<CR>zv
Note: I change map to noremap because noremap stops recursive mappings whereas map allows for it.