Vim - Only 1 'bind' in .vimrc file - vim

I've got problem with .vimrc file. I've installed NerdTree and I added this line into vimrc file:
map <C-n> :NERDTreeToggle<CR>
It works perfectly, but I want to use python in vim. I added this line:
nnoremap <buffer> <F5> :exec '!python' shellescape(#%, 1)<cr>
And It doesn't work. When I've only had "python bind" it was working, but when I added NerdTree link "python bind" stopped working.

The <buffer> in nnoremap <buffer> <F5> :exec '!python' shellescape(#%, 1)<cr> means that the mapping is local to the current buffer.
Since you have that mapping in your vimrc, it is defined not for the vimrc but for the first buffer you edit and only the first buffer.
As soon as you open another buffer, no matter what kind of buffer (NERDTree included), your mapping won't work anymore for any other buffer than the first one.
Here is a revised version of your mapping that will only work in Python buffers, all of them:
augroup PythonThings
autocmd!
autocmd FileType python nnoremap <buffer> <F5> :exec '!python' shellescape(#%, 1)<cr>
augroup END
See:
:help <buffer>
:help autocommand
Never add anything to your config that you don't fully understand.

Related

How to use different .vimrc than what is the default .vimrc

I share lab server with my colleagues, but want my separate .vimrc file. How to get that?
$ cat .vimrc
color desert
$ pwd
/fvs101/home
We have our separate working directory, so my directory is inside
/fvs101/home/sp
At /fvs101/home there is common .vimrc file.
I want to play with vim but do not want to touch this /fvs101/home/.vimrc file.
I want to create my own .vimrc file.
++++++++++my vimrc file
[sp]$ cat .vimrc
noremap - ddp
noremap _ dd2kp
inoremap <c-u> <esc>lviwU<esc>i
nnoremap <c-u> <esc>viwU<esc>
let mapleader = "-"
let maplocalleader = "\\"
nnoremap <leader>ev :vsplit $MYVIMRC<cr>
nnoremap <leader>sv :source $MYVIMRC<cr>
iabbrev ssig -- <cr>Sachin Pawar<cr>sachin.sp.pawar#oracle.com
vnoremap <leader>' vi<esc>`<<esc>i'<esc>`><esc>i'<esc>
nnoremap H 0
nnoremap L $
inoremap jk <esc>
inoremap <esc> <nop>
augroup filetype_js
autocmd!
autocmd FileType javascript nnoremap <buffer> <localleader>c I//<esc>
augroup END
augroup filetype_python
autocmd!
autocmd FileType python nnoremap <buffer> <localleader>c I#<esc>
augroup END
augroup filetype_sql
autocmd!
autocmd FileType sql nnoremap <buffer> <localleader>c I--esc>
augroup END
augroup filetype_shell
autocmd!
autocmd FileType shell nnoremap <buffer> <localleader>c I#esc>
augroup END
augroup filetype_html
autocmd!
autocmd FileType html nnoremap <buffer> <localleader>f Vatzf
augroup END
++++++++++
If i use my separate .vimrc file i am not able to see the mapping for some mappings like
inoremap jk
Why is this happening and how to avoid it.
In order to use my own .vimrc file i have create a separate alias.
alias vi='vim -u /fvs101/home/sp/.vimrc'
Make a different .vimrc (as you have stated in the comments, /refresh/oracle/spawar/.vimrc) and make an alias for your own user
myvi='vim -Nu /refresh/oracle/spawar/.vimrc'
Note that without the -N flag, using the -u flag will start vim in compatibility mode causing all sorts of nasty, unexpected side-effects such as mappings not working entirely.
I found this out by going to see what the -u flag really does and following from there.
Helpful links below.
u flag
N flag
Seeing as you specifically want everyone to log into the same user, the easiest way I can think of how to do this is if you make your own .vimrc (give it a short name, such as ~/.vrc) and every time you open vim, you type
:so ~/.vrc
which will load that vimrc for you. Every time you open vim it will use the defaults, until you load your own vimrc, so you will have to do this each and every time. Also remember that this way, ~/.vimrc loads automatically before you manually load ~/.vrc
You can call Vim with a different runtime configuration file than the default one with
vim -N -u path/to/your/vimrc
If you want to use this always, edit your users shell runtime configuration to define an alias for that.
alias vim="vim -N -u path/to/your/vimrc"
I think the simplest solution would be to append a single line to the end of the default vimrc which reads:
source ~/.vimrc.local " Or whatever you want to call it
Where .vimrc.local contains your custom configuration.

How to change nnoremap for different file types?

When I hit <leader>r inside a PHP file, I want to do:
nnoremap <leader>r ifile_put_contents(ini_get('error_log'), '');<cr>error_log(print_r(, true));<esc>
But when I do the <leader>r inside a JS file, I want to do:
nnoremap <leader>r iconsole.log();<esc>
How could I do this?
I also want to position the cursor inside the console.log/print_r. Any idea how to do that as well?
You can use autocomand and map to the buffer only. Here's an example:
augroup quick_debug
autocmd!
autocmd FileType php nnoremap <buffer> <leader>r ifile_put_contents(ini_get('error_log'), '');<cr>error_log(print_r(, true));<esc>
autocmd FileType javascript nnoremap <buffer> <leader>r iconsole.log();<esc>
augroup END
To move cursor around you can use <left> and <right> before <esc>. Like this: nnoremap <buffer> <leader>r iconsole.log();<left><left><esc>.
If you don't mind using plugins you could take a look at the UltiSnips and use snippets instead of mappings.
You can define that for certain filetypes by prepending :autocmd Filetype {filetype} ..., and put that into your ~/.vimrc. But that gets unwieldy as you add mappings and other settings for various filetypes. Better put the commands into ~/.vim/ftplugin/{filetype}_mappings.vim. (This requires that you have :filetype plugin on.)

remove netrw s-up and s-down mapping in vim

I have the following mappings in my .vimrc file that I use to move between windows, but when in an explore window :e of netrw plugin the shift down key will produce a warning window instead of respecting my mappings. I am assuming this mapping must be hard coded into the plugin itself. How can I remove the shift-up and shift-up mappings in the plugin.
nnoremap <silent> <S-Up> :wincmd k<CR>
nnoremap <silent> <S-Down> :wincmd j<CR>
The window that shows up when trying to do a shift-up or shift-down is below, which shows up in a new split which is very annoying:
**warning** (netrw) using Nexplore or <s-down> improperly; see help for netrw-starstar
As a result, my question is how can I shut off this behavior in the netrw plugin so that it respects my mappings shown above instead.
A buffer map for that is created by the plugin, so you could overwrite it again after created with an autocmd:
autocmd filetype netrw nnoremap <buffer> <s-down> :wincmd j<cr>
If using multiple times, it might be useful to create a group:
augroup netrw_maps
autocmd!
autocmd filetype netrw call ApplyNetrwMaps()
augroup END
function ApplyNetrwMaps()
nnoremap <buffer> <s-up> :wincmd k<cr>
nnoremap <buffer> <s-down> :wincmd j<cr>
" ...
endfunction

Change the mapping of F5 on the basis of specific file type

The current mapping of my F5 key is:
imap <F5> <esc>:w\|!python %<CR>
Now I want that if I'm editing any python file (it will be better if it also recognizes file other than standard .py format like .pyd etc) then this mapping works as it is. But, if I edit a Java file it is mapped to something like:
imap <F5> <esc>:w\|!javac %<CR>
And when I'm editing any .c or .cpp file then F5 is mapped to this:
imap <F5> <esc>:w\|!make %<CR>
I have no idea how to proceed.
There are problems with both given answer and original mapping. First of all, for buffer-local mappings there is *map <buffer>. Second, with <buffer> you don’t need to use BufEnter events and can instead use Filetype which are launched only once. Third, you have one error (2.), one potential problem (1.) and one place that can be optimized in original mappings:
you should not be using imap, it makes it very easy to accidentally break old mappings when adding new ones
!python % will break once file contain a special symbol (space, semicolon, quot, dollar, …)
using :update instead of :write avoids useless writes in some cases
My variant:
autocmd Filetype c,cpp inoremap <buffer> <F5> <C-o>:update<Bar>execute '!make '.shellescape(expand('%:r'), 1)<CR>
autocmd Filetype python inoremap <buffer> <F5> <C-o>:update<Bar>execute '!python '.shellescape(#%, 1)<CR>
autocmd Filetype java inoremap <buffer> <F5> <C-o>:update<Bar>execute '!javac '.shellescape(#%, 1)<CR>
Try this:
au BufEnter *.py map <F5> <esc>:w\|!python %<CR>
au BufEnter *.java imap <F5> <esc>:w\|!javac %<CR>
au BufEnter *.c, *.cpp imap <F5> <esc>:w\|!make %<CR>
I'm not 100% sure about the comma separated file types, trying to verify...
The vim docs are usually pretty nasty to try to figure out how to use commands, but these should help get you started:
:h BufEnter
:h :autocmd
Note: You may have to restart vim for these changes to overwrite the current autocommand groups.

Prevent certain command mappings while in NERDTree window in Vim

I have the following keys mapped in my .vimrc file:
noremap <silent> <C-h> :bprev<CR>
noremap <silent> <C-l> :bnext<CR>
The commands they execute are provided from the buftabs script.
What I would like to do is prevent those key mappings from being executed when I'm in the NERDTree split. The reason for this is if the commands are run while in NERDTree, a file buffer gets loaded in the split instead. Then, to fix it, the window needs to be closed and opened again.
This is a similar problem as explained in another question, but the problem there was corrected by configuring the plugin, while the buftabs script does not have such an option.
In order to disable a mapping in certain buffers, one can define
a buffer-local mapping for the same key sequence, overriding the
original mapping with a no-op:
:autocmd FileType nerdtree noremap <buffer> <c-h> <nop>
:autocmd FileType nerdtree noremap <buffer> <c-l> <nop>
(See :help :map-arguments and :help <nop> for details on
<buffer> and <nop>, respectively.)
I updated my vimrc by looking at ib.'s solution.
autocmd FileType nerdtree noremap <buffer> <A-PageDown> <ESC>:wincmd w <bar> bnext<CR>
autocmd FileType nerdtree noremap <buffer> <A-PageUp> <ESC>:wincmd w <bar> bprevious<CR>
It goes back to the previous window and executes the command.

Resources