remap Enter in normal mode but not in command line - vim

I have remapped the Enter key in normal mode to add a new line:
nnoremap <CR> o<Esc>k
Works great, but now in the command line (entered by q:) this command add new line either, but I would like to leave it in default mode, this is execute the selected command from history.
So the question is, how can I remap a key in normal mode, but not in command line?

This autocommand does what you want:
augroup commandlinewindow
autocmd!
autocmd CmdwinEnter * nnoremap <buffer> <CR> <CR>
augroup END
It's the "command-line window", by the way, not the "command line".

In addition to command-line window you'll almost certainly need this one. For quickfix window:
autocmd BufReadPost quickfix nnoremap <buffer> <CR> <CR>

Related

How do I map <CR>/<Enter> properly in Vim

I found vimrc key remapping of / in vim-better-default plugin and added to my config.
The remapping works as expected, but this remap also causes an issue with jump using the relative number. For example, when I run 5<CR>, I need to type enter once more for it to work and it only jumps for 4 lines. The output in the bottom display is :.,.+4
I tried adding these but it did not work either.
autocmd CmdwinEnter * nnoremap <CR> <CR>
autocmd BufReadPost quickfix nnoremap <CR> <CR>
Thanks,

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.

vim re-enter insert mode on CmdWinLeave

I'm trying to map the <C-q> combination to enter/exit the Command Line Window (CmdWin).
But when I leave the CmdWin, I'm not able to return to the insert mode, but vim automatically goes back to the normal mode
In my .vimrc the following snippet is used, but doesn't work.
nmap <C-q> <Esc>:<C-f>
imap <C-q> <Esc><C-q>
au CmdWinLeave * startinsert!
I've also tried with:
au CmdWinLeave * startinsert
au CmdWinLeave * normal
You can map: imap <C-q> <Esc>:q<CR>a, then it will re-enter insert mode when you press <C-q> when you are editing in CmdWin.

Automatically call HTMLBeaufity when exiting Insert mode in Vim

I'd like to automatically format html and js code when I exit insert mode. Currently I have ctrl f mapped to format the current file in my vimrc:
map <c-f> :call JSBeautify()<cr>
Is there a way I can trigger this command each time I exit insert mode?
Thanks
Try to put this
augroup AuJsBeautify
au!
au InsertLeave * call JsBeautify()
augroup END
in your .vimrc.
To know more about autocommands, read :h 40.3 and :h autocommand.
Or if you prefer mapping, you can just map it on your Esc
inoremap <Esc> <Esc>:call JsBeautify()<cr>

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