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.
Related
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.
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.)
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
I ask this question generally, but I will put it in terms of the specific problem I'm having.
I'm using the vim-lawrencium plugin for a project under Mercurial source control. I use the :Hgstatus command to open the status buffer.
The status buffer comes with some nice keymaps to make it easy to add files to the commit, look at diffs, and finalize the commit.
nnoremap <buffer> <silent> <cr> :Hgstatusedit<cr>
nnoremap <buffer> <silent> <C-N> :call search('^[MARC\!\?I ]\s.', 'We')<cr>
nnoremap <buffer> <silent> <C-P> :call search('^[MARC\!\?I ]\s.', 'Wbe')<cr>
nnoremap <buffer> <silent> <C-D> :Hgstatustabdiff<cr>
nnoremap <buffer> <silent> <C-V> :Hgstatusvdiff<cr>
nnoremap <buffer> <silent> <C-U> :Hgstatusdiffsum<cr>
nnoremap <buffer> <silent> <C-H> :Hgstatusvdiffsum<cr>
nnoremap <buffer> <silent> <C-A> :Hgstatusaddremove<cr>
nnoremap <buffer> <silent> <C-S> :Hgstatuscommit<cr>
nnoremap <buffer> <silent> <C-R> :Hgstatusrefresh<cr>
nnoremap <buffer> <silent> q :bdelete!<cr>
Most of these seem to work. I've successfully tried diffing, adding, and q to close the status, but the <C-S> shortcut doesn't work at all. I can manually run the :Hgstatuscommit command it's mapped to. I've also looked at :map to verify the key is actually mapped for that buffer, and it does show up in the list.
What is my next step in debugging this? I want to fix this specific problem, but I also want to know how to fix it in the event I run across broken shortcuts in the future. I'm new to Vim, so I'm at a bit of a loss at this point.
UPDATE: Output of :verbose map <C-S>
v <C-S> *#:Hgstatuscommit<CR>
Last set from ~/.spf13-vim-3/.vim/bundle/vim-lawrencium/plugin/lawrencium.vim
n <C-S> *#:Hgstatuscommit<CR>
Last set from ~/.spf13-vim-3/.vim/bundle/vim-lawrencium/plugin/lawrencium.vim
SOLUTION: Turned out the problem was that my shell was intercepting Ctrl-S and it was never getting to Vim.
I added a Vim alias to my .zshrc to fix:
alias vim="stty stop '' -ixoff ; vim"
ttyctl -f
Found the fix on the Vim Wikia which also has a solution for bash shells.
Software Flow Control
If you are using using a terminal then it is often the case that <c-s> is being used for terminal's software flow control (XON/XOFF). Which makes <c-s> a trickier key to map.
Turn off flow control by adding the following to some startup script (e.g. ~/.bash_profile or ~/.bashrc):
stty -ixon
If you have frozen your terminal then you can unfreeze it by pressing <c-q>.
Generic map debuging
You can debug pretty much any custom vim mapping via the following command:
:verbose map
This will list out each key/chord ({lhs}) maps to what command ({rhs}), mode, and file the mapping was sourced from. For more information on this listing see :h map-listing and :h :map-verbose.
We can filter this list in a few ways:
Supplying a mode. e.g. :verbose nmap for normal mode and :verbose imap for insert mode.
Proving the key we want to query for. e.g :verbose nmap <c-s>
Can also see buffer specific mappings by adding <buffer>. e.g. :verbose nmap <buffer> <c-s>
So for your question the best way to debug what your mapping is set to would be to run the following query:
:verbose nmap <buffer> <c-s>
Note: Vim's native command are not listed via :verbose map. The best way to find one of Vim's native commands is to help. See :h for more.
First, check that <C-S> still mapped to :Hgstatuscommit
map <C-S>
Hgstatuscommit calls s:HgStatus_Commit. Open its definition on line 1134 and put some debugging print outs:
echom a:linestart
echom a:lineend
echom a:bang
echom a:vertical
After using the mapping, check :messages.
I’d suspect that <C-S> is mapped to something else. You can use :map
<C-S> to check how (or if) its mapping is configured. Even better, you can
add the prefix to see where the mapping was set from, e.g., when I run
:verbose map <C-L>, the following is displayed:
<C-L> * :noh<CR><C-L>
Last set from ~/.vimrc
By contrast, I haven’t set a mapping for <C-S> so when I run, :map <C-S>,
I get:
No mapping found
Prepending verbose to a command is a useful general debugging technique as it can show where any Vim option was set, e.g., :verbose set background? shows what the background option is currently set to and which Vim configuration file it was set in:
background=dark
Last set from ~/.vimrc
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.