Mapping tab changes in vim - vim

I am trying to do a key mapping in vim, yet it doesn't appear to be working how I want it to:
In my ~.vimrc file I have:
syntax on
colorscheme delek
nnoremap < gt
nnoremap > gT
(I only include the first two comments to show that I know my .vimrc is working).
Yet, after I save the file, using < or > does not do anything, though gt and gT are still changing tabs as expected. What is wrong with my character mapping? Or do I need to 'refresh vim' or something, the equivalent of $ source ~/.bash_profile ?

I can confirm that there is nothing wrong with your mapping, and that you are correct about needing to reload/refresh your configuration for it to actually be available. One way to do so is to restart Vim, but we can do it faster with the :so (short for :source) command and the $MYVIMRC environment variable:
:so $MYVIMRC

Related

Vim simple mapping

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.

detect vrapper in .vimrc

In my .vimrc I have the following:
" Use j/k to navigate the word completion popup menu
inoremap <expr> j pumvisible() ? "\<C-N>" : "j"
inoremap <expr> k pumvisible() ? "\<C-P>" : "k"
This works perfect when using vim within a terminal, however this is not compatible with vrapper (eclipse vim plugin). Vrapper completely stops working when these settings are in my .vimrc. In my home directory I have a .vrapperrc file, which is simply a symobolic link pointing to my .vimrc file. Hence the vim settings which are loaded for vim / vrapper are equal.
Is there a way that in my .vimrc I can detect that the settings are loaded for vrapper instead of default (terminal) vim. That way I would like to disable these settings for vrapper and just load them when vim is used from the command line. Perhaps there is a different smart way to solve this issue. Of course I could create two .vimrc files, one for default vim and one for vrapper, however that way I would need to maintain two files which I would like to prevent.
I had a similar problem in which Vrapper executed the contents of my functions immediately because it didn't understand what they were.
I solved it by wrapping the vim only code with
if has("eval")
" vim only code
endif
which caused Vrapper to ignore it.
As #romainl noted, Vrapper only understands a limited set of Vim commands and features. Function definitions, ternaries and <expr> are definately not supported.
If you are willing to split your rc files, you could use the source command to at least reuse one of them.
For example, put these basic settings in your .vrapperrc:
set ignorecase
set hlsearch
Then you can do this in your .vimrc:
" Load common settings for Vim / Vrapper
source .vrapperrc
" ... Other Vim-specific settings
Note that Vrapper also has a source command, so you could in theory have 3 rc files with the "common" settings shared between the two:
Example .vrapperrc:
set novisualmouse
source .vimcommonrc
Example .vimrc
" Use j/k to naviage the word completion popup menu
inoremap <expr> j pumvisible() ? "\<C-N>" : "j"
inoremap <expr> k pumvisible() ? "\<C-P>" : "k"
source .vimcommonrc
And the common settings:
set hlsearch
set ignorecase
It's your choice.
I don't think you can detect Vrapper, but you can instead write a command that real Vim will process and Vrapper will ignore.
Example: mapping <KMinus>:
nnoremap <KMinus> q
If Vrapper runs that, it crashes. Let's make it ignore it. There are many ways:
if v:true
nnoremap <KMinus> q
endif
if v:true | nnoremap <KMinus> q | endif
exec 'nnoremap <KMinus> q'
There's also this more "aggressive" version, if the other ones don't work. It's useful for multiple commands:
if v:true |
\nmap <C-Bslash> gcc |
\vmap <C-Bslash> gc |
\imap <C-Bslash> <C-o><C-Bslash> |
\endif
You can even add a finish statement which Vim will treat as "I should stop reading this file" and Vrapper will probably ignore and keep executing the file, making it ideal to precede Vrapper-only commands:
if v:true
" Vim-only stuff
finish
endif
" Vrapper-only stuff
How do you think it would work?
Your ~/.vimrc is not a sentient being that can take decisions by itself: it needs to be sourced by a program that understands it and… that program is Vim.
Vrapper only supports a subset of Vim's vocabulary so it is not very reasonable to expect it to behave exactly like Vim. The main missing feature that would make possible the detection you are asking about is vimscript: since Vrapper doesn't support it you can't use an if-else-endif construction!
Because the two programs support vastly different sets of options and commands I'm afraid you will have to manage two separate files.
Did you try Vrapper's :source command?

Delete vim config and plugin files

Simple mappings like let mapleader = "," don't seem to be working, so I reinstalled vim and removed all plugins. Yet some mappings like this still don't work. Where can I find vim plugin config files? I've looked in ~/.vim and ~/.config. When I type a comma in command mode, instead of starting the mapleader command, my mac gives me an alarm bell sound. I included let mapleader = "," as the first line in my .vimrc.
UPDATE To help diagnose, I have this line in my .vimrc:
noremap <silent> <Leader>w :w<CR>
When I type :map in vim, I see, among other mappings:
,w * :w<CR>
The most elaborate information you'll get by running vim via vim -V.
This will give you information on every file that vim looks for, finds and opens.
The most important are ~/.vimrc and /usr/share/vim/*.
Besides, please verify that your mapleader command is at the beginning of your vimrc.
From the manual:
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.
EDIT
To verify if it's enabled for sure, type :let mapleader in a running vim. It should print something like mapleader ,. Please verify it, so we can think of other causes.
Try placing your mappings in a file called ~/.vimrc.
The ~/.vim directory is used to store plugin files, whereas the ~/.vimrc file is typically used for generic settings like mappings.
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.
Are you sure you are defining map leader before you create your mappings? If not, your mapleader is probably , just as your define, but all your macros will be mapped to \.
A Vim plugin usually "configures itself" in ~/.vim/plugin/pluginname.vim, not in an external file. The configuration typically consists of variables (e.g. g:pluginnameSettingName) and mappings, which should appear in this format:
nnoremap <silent> <Plug>(PluginMapping) :<C-u>call pluginname#MyFunc()<CR>
if ! hasmapto('<Plug>(PluginMapping)', 'n')
nmap <Leader>x <Plug>(PluginMapping)
endif
If the default mapping starts with <Leader>, your intended change via :let mapleader should work. Alternatively, you can try to explicitly override the mapping by mapping your own keys to the <Plug>... mapping.
I really suggest you use a practical plugin Vundle to manage your vim plugins. It would enable you to manage your plugins in text format and config parameters of each plugins separately.
For instance, if you want to install a new plugin 'a.vim', you just need to add a line into your bundle.vim
Bundle 'a.vim' (for any plugins you can access from VimScripts you could just type the short name like 'c.vim' or 'Tagbar'). Restart Vim and run :BundleInstall, Vim will install the new plugins automatically.
In the same way, when you want to uninstall the plugin, just remove the line Bundle 'a.vim' in your configuration file and run :BundleClean, the plugin will be removed automatically.

Missing leader in Vim

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>.

some vimrc commands not loaded at startup

Some time ago the line in my vimrc (d:/programs/vim/_vimrc) stopped working (not the only one):
nnoremap <unique> ç :execute ':let #/ = "\\<' . expand('<cword>') . '\\>"'<CR>:set hlsearch<CR>
(This line overwrite the functionality of * but without jumping to the next observation, just highlight the word under the cursor. I mapped it to ç because this char is in my keyboard near * and vim does not uses it, but you could map any other key)
The problem is that for some time it stopped working if placed in _vimrc, but works if placed in $VIM/vimfiles/plugins/myPlugins.vim
Have you experienced this issue?
It could be because of installing a new plugin?
I am mostly intrigued for this behavior. I have tried with :scriptnames and other copies of _vimrc, .vimrc, .vim/vimrc, etc., but can not find the source of the misbehavior.
From :h <unique>:
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.
Use :verbose nmap ç to see where it is defined.
Or simply drop <unique> which doesn't seem useful at all.

Resources