remove netrw s-up and s-down mapping in vim - 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

Related

Vim - Only 1 'bind' in .vimrc file

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.

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

How to unmap vim shortcuts upon quickfix exits

Background: I'm using skwp's dotfiles, and his recently changes is breaking some functionalities I use on daily basis.
Instead of set up the mappings globally, I'm trying to nnoremap two shortcuts upon quickfix enters and nunmap after quickfixes quits.
BTW, I think syntastic is used for linting, which invokes the quickfix/location lists.
Here's the code:
augroup quickfixShortcutsGroup
autocmd!
autocmd BufWrite * :echom "Foo"
" au BufReadPost quickfix nnoremap <silent> <C-z> :cp<CR>
" au BufReadPost quickfix nnoremap <silent> <C-x> :cn<CR>
au BufWinEnter quickfix nnoremap <silent> <C-z> :cp<CR>
au BufWinEnter quickfix :echo '1'
au BufWinLeave quickfix nnoremap <silent> <C-z> :cp<CR>
au BufWinLeave quickfix :echo 'BufWinLeave'
au BufLeave qf :echo 'BufLeave'
au BufUnload qf :echo 'unload qf'
" au BufLeave qf noremap <silent> <C-z> :cb<CR>
" au BufLeave quickfix noremap <silent> <C-z> :cb<CR>
" au BufWinLeave quickfix noremap <silent> <C-z> :cb<CR>
" au BufWinLeave quickfix nunmap <C-z>
" au BufWinLeave quickfix :echom 'Hello'<cr>
" BufWinEnter
augroup END
After read reference:
http://vimdoc.sourceforge.net/htmldoc/autocmd.html#BufWinLeave
http://vimdoc.sourceforge.net/htmldoc/autocmd.html#autocmd-patterns
I still could not get unmap events working, i.e. BufWinLeave, BufUnload, BufLeave are not invoked.
Can Vimers tell me which event(s) I should be using and help me out on this? Thank you in advance for the help.
As :help BufWinLeave explains, the current buffer "%" may be different from the buffer being unloaded "". So you need a global autocmd, and resolve the buffer number that has been left, and then check for the quickfix 'buftype':
autocmd! BufWinLeave * if getbufvar(bufnr(expand('<afile>')), '&buftype') ==# 'quickfix' | echo "leaving quickfix" | endif
But in general, I'd advise against such tricks and especially conditional mappings. Your <C-z> / <C-x> mappings are still global, now just depending on whether the quickfix list is visible. That's bad for muscle memory, and the overload of the key combos is mentally taxing. I'd rather get rid of the mappings completely, or assign different (if potentially longer) keys.
And there's the next complication: Vim "distributions" and other people's dotfiles lure you with a quick install and out-of-the-box settings, but you pay the price with increased complexity (you need to understand both Vim's runtime loading scheme and the arbitrary conventions of the distribution) and inflexibility (the distribution may make some things easier, but other things very difficult). Vim is incredibly customizable, using someone else's customization makes no sense.
If you would like to nnoremap these two mappings upon quickfix enters and nunmap after quickfix quits, you could
" map silently upon entering Quickfix
autocmd BufWinEnter * if &buftype == 'quickfix'
\| nnoremap <silent> <C-x> :cn<CR>
\| nnoremap <silent> <C-z> :cp<CR>
\| endif
" unmap upon leaving Quickfix
autocmd BufWinLeave * if &buftype == 'quickfix'
\| nunmap <C-x>
\| nunmap <C-z>
\| endif
Or you can make use of local buffer mapping to make your code shorter
" map silently upon entering Quickfix, and only for Quickfix
autocmd BufWinEnter * if &buftype == 'quickfix'
\| nnoremap <buffer><silent> <C-x> :cn<CR>
\| nnoremap <buffer><silent> <C-z> :cp<CR>
\| endif
These autocmd's are invoked every time you enter or leave a buffer. So it is really better just, as Sato Katsura suggested, add in your ~/.vim/ftplugin/qf.vim
nnoremap <buffer><silent> <C-x> :cn<CR>
nnoremap <buffer><silent> <C-z> :cp<CR>
You may consider to read these:
:h ftplugins
:h map-local
:h buftype
:h line-continuation

vim removes indentation from the current line

Every time I enter the Insert mode in vim using A and press Enter to go to the next line, vim automatically removes all indentation from the current line. For instance, ideally, it should be like this -
14 echo $sample;
15 $ai = system("python ai.py " + $sample, $retVal);
If I press A on line 15 and then <CR>, this is what my vim looks like -
14 echo $sample;
15 $ai = system("python ai.py " + $sample, $retVal);
16
This is my .vimrc -
"General
set nu " Set line numbers
set hlsearch " Set search highlight
set shiftwidth=2
set tabstop=2
"Pathogen
execute pathogen#infect()
call pathogen#helptags()
syntax on
filetype plugin indent on
"ConqueTerm
command Cterm split | ConqueTerm bash
nnoremap <silent> sh :Cterm<CR>
"NerdTree -
"Fs open Nerdtree on the same tab. Nfs opens NerdTree in new tab.
command Fs NERDTree
command Nfs tabedit | NERDTree
nnoremap <silent> fs :Fs<CR>
"TagBar
nnoremap <silent> tt :TagbarToggle<CR>
"Omni Completion
filetype plugin on
filetype indent on
inoremap <C-Space> <C-X><C-o>
"Editor
command Ide NERDTree | TagbarToggle
"Move between split windows
nmap <silent> <A-Up> :wincmd k<CR>
nmap <silent> <A-Down> :wincmd j<CR>
nmap <silent> <A-Left> :wincmd h<CR>
nmap <silent> <A-Right> :wincmd l<CR>
"Drag words like in Windows
nnoremap <C-Left> b
vnoremap <C-S-Left> b
nnoremap <C-S-Left> gh<C-O>b
inoremap <C-S-Left> <C-\><C-O>gh<C-O>b
nnoremap <C-Right> w
vnoremap <C-S-Right> w
nnoremap <C-S-Right> gh<C-O>w
inoremap <C-S-Right> <C-\><C-O>gh<C-O>wA
" Start Syntastic in passive mode
let g:syntastic_mode_map = { 'mode': 'passive' }
nnoremap <silent> ch :SyntasticCheck<CR>
Any idea what's going on?
You probably want to set autoindent in your vimrc file:
:set ai
You can also verify if autoindent was previously set (for example through the filetype plugins) by checking its value with
:set ai?

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