This question already has answers here:
Remapping :Wq to :wq in vim
(3 answers)
Closed 8 years ago.
I have the following keymappings in my vimrc
nmap :Q<CR> :q<CR>
nmap :W<CR> :w<CR>
nmap :WQ<CR> :wq<CR>
However, they do nothing. Vim complains that Q isn't a valid command. Same for W and WQ. I restarted vim and everything. I'm trying to figure this out and I'm also hoping to map Ctrl+W and the arrow keys to just Ctrl+h for changing windows left and etc. How can I go about doing that?
For your second question:
nnoremap <C-h> <C-w>h
nnoremap <C-j> <C-w>j
nnoremap <C-k> <C-w>k
nnoremap <C-l> <C-w>l
Related
I have the following in my .vimrc
nnoremap <c-h> zh
nnoremap <c-j> <c-e>
nnoremap <c-k> <c-y>
nnoremap <c-l> zl
and nmap reports the following mappings
n <C-H> * zh
n <NL> * <C-E>
n <C-K> * <C-Y>
n <C-L> * zl
The main problem I have is that <c-h> hasn't actually been remapped to zh. <c-h> still acts as the default behavior, which just moves the cursor left.
I've done this in a clean .vimrc file so it's not due to a plugin collision. This is happening with vim 7.4 on a redhat 7.2 system that I remote into from putty. I've tried to replicate this on my windows machine using WSL but it works as expected there.
The minor problem is the second row of nmap, why does it say <NL> instead of <C-J> The mapping seems to be behaving as expected.
I have in my .vimrc the following lines, which lets me switch windows with ctrl+hjkl:
nnoremap <C-h> <C-W>h
nnoremap <C-j> <C-W>j
nnoremap <C-k> <C-W>k
nnoremap <C-l> <C-W>l
These are fine for my desktop computer, but on my netbook, I want to have the active window completely fill the tiny screen. This means typing ctrl+w _ and ctrl+w | after each window change. The logical step would be to add those keystrokes to the mapping, yielding:
nnoremap <C-h> <C-W>h<C-W>_<C-W>|
nnoremap <C-j> <C-W>j<C-W>_<C-W>|
nnoremap <C-k> <C-W>k<C-W>_<C-W>|
nnoremap <C-l> <C-W>l<C-W>_<C-W>|
But that fails, consistently, when in a mapping, despite working when I simply type the required keys; and (as I have set 'showcmd') it seems to leave a trailing <C-W>.
I have also tried using :wincmd:
nnoremap <C-h> :wincmd h<cr>:wincmd _<cr>:wincmd |<cr>
nnoremap <C-j> :wincmd j<cr>:wincmd _<cr>:wincmd |<cr>
nnoremap <C-k> :wincmd k<cr>:wincmd _<cr>:wincmd |<cr>
nnoremap <C-l> :wincmd l<cr>:wincmd _<cr>:wincmd |<cr>
But that complains about trailing <cr> whenever my vimrc is sourced, so I'm not going to pursue that further without more research.
Any ideas?
Try using <Bar> instead of |. ie:
nnoremap <C-h> <C-W>h<C-W>_<C-W><Bar>
nnoremap <C-j> <C-W>j<C-W>_<C-W><Bar>
nnoremap <C-k> <C-W>k<C-W>_<C-W><Bar>
nnoremap <C-l> <C-W>l<C-W>_<C-W><Bar>
| are used to have multiply commands on one line and you will need to be escaped with a backslash when used literally:
nnoremap <C-h> <C-W>h<C-W>_<C-W>\|
nnoremap <C-j> <C-W>j<C-W>_<C-W>\|
nnoremap <C-k> <C-W>k<C-W>_<C-W>\|
nnoremap <C-l> <C-W>l<C-W>_<C-W>\|
On the other hand | can be useful:
nnoremap xxx :if 1 == 2 | echom "hello" | endif
I have a few mappings in vim for moving between splits set in my .vimrc,
nnoremap <C-j> <C-w>j
nnoremap <C-k> <C-w>k
nnoremap <C-h> <C-w>h
nnoremap <C-l> <C-w>l
All of these work except for <C-j>, which I suspect is being remapped in one of my plugins. I'd like to find out where, but I'm not sure how. Is there a way to find "where was <C-j> last mapped?"
:verbose nnoremap <c-j>
should help.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
Which custom key bindings do you use to increase productivity in Vim?
My two favorites are:
inoremap jj <Esc>
" Clear screen clears search highlighting.
nnoremap <C-L> :nohl<CR><C-L>
Window Management
" Window splitting
nmap <silent> <leader>sh :leftabove vnew<cr>
nmap <silent> <leader>sl :rightbelow vnew<cr>
nmap <silent> <leader>sk :leftabove new<cr>
nmap <silent> <leader>sj :rightbelow new<cr>
nmap <silent> <leader>swh :topleft vnew<cr>
nmap <silent> <leader>swl :botright vnew<cr>
nmap <silent> <leader>swk :topleft new<cr>
nmap <silent> <leader>swj :botright new<cr>
" Scroll the window next to the current one
" (especially useful for two-window splits)
nmap <silent> <leader>j <c-w>w<c-d><c-w>W
nmap <silent> <leader>k <c-w>w<c-u><c-w>W
Text Editing
" Toggle search highlighting
nmap <silent> <leader>/ :set hlsearch!<cr>
" Toggle paste mode
" (prefer this over 'pastetoggle' to echo the current state)
nmap <leader>p :setlocal paste! paste?<cr>
" Select the last edited (or pasted) text
nmap gv `[v`]
" Keep lines that do (or do not) contain the last search term
nmap <leader>v/ :v/<c-r>//d<cr>gg
nmap <leader>g/ :g/<c-r>//d<cr>gg
" Email (de-)quotation
nmap <leader>q vip:s/^/> /<cr>
vmap <leader>q :s/^/> /<cr>
nmap <leader>Q vip:s/^> //<cr>
vmap <leader>Q :s/^> //<cr>
File Opening and Saving
" Save and restore session
nmap <leader>ss :wa<cr>:mksession! $HOME/.vim/sessions/
nmap <leader>rs :wa<cr>:source $HOME/.vim/sessions/
" Write buffer through sudo
cnoreabbrev w!! w !sudo tee % >/dev/null
" Change the current directory to the directory of the file in buffer
nmap <silent> <leader>cd :cd %:p:h<cr>:pwd<cr>
" Open file located in the same directory as the current one
nmap <leader>e :e <c-r>=expand('%:p:h').'/'<cr>
Grep the word under cursor:
Using the following grepprg options it will search recursively into the current directory excluding and including some specific files.
" Quick Grep
noremap <Leader>g :grep<space><C-r><C-w><CR>:copen<CR><CR><C-W>b
set grepprg=grep\ -nH
\\--include='*.c'
\\--include='*.cpp'
\\--include='*.h'
\\--exclude-dir='.svn'
\\--exclude='*.svn-base'
\\--exclude-dir='OBJ'
\\--exclude='symTbl.c'
\\ $*
\\ -R\ .
It greps the word under cursor, then open the Quickfix Window and move the cursor to the bottow window (which should be the list of grep results)
This is probably one of the shortcut I use the most, and it saves lots of typing !
Moving around quickly between windows
noremap <C-j> <C-W>j
noremap <C-k> <C-W>k
noremap <C-h> <C-W>h
noremap <C-l> <C-W>l
It is quite intuitive and handy to move around when your screen is split horizontally and vertically.
The following command remaps ; to : in command mode, saving you from wasting precious milliseconds holding and releasing the Shift key when typing commands like :wq:
" Remap ";" to ":"
map ; :
noremap ;; ;
If you need to type an actual ;, just press it twice.
Insert mode
" <esc> in normal mode clears highlight
nnoremap <silent> <esc> :noh<cr><esc>
Command Line Editing
" copy an entire word from the line above instead of just one
inoremap <expr> <c-y> matchstr(getline(line('.')-1), '\%' .
\ virtcol('.') . 'v\%(\k\+\\|.\)')
" Insert Directory of current buffer and open completion
cnoremap <expr> <c-k> getcmdline()[getcmdpos()-2] == " " ?
\ expand("%:h") . "/\<c-d>" : "\<c-d>"
let mapleader=","
" omnicompletion : words
inoremap <leader>, <C-x><C-o>
" omnicompletion : filepaths
inoremap <leader>: <C-x><C-f>
" omnicompletion : lines
inoremap <leader>= <C-x><C-l>
" toggle TagList
nnoremap <leader>l :TlistToggle<CR>
" toggle NERDTree
nnoremap <leader>n :NERDTreeToggle<CR>
" I like vertically aligned assignation operators
nnoremap <leader>a :Tabularize<Space>
" with | marking the cursor
" it turns this
" function foo(){|}
" into this
" function foo(){
" |
" }
inoremap <C-Return> <CR><CR><C-o>k<Tab>
" push the current ligne up and down
nnoremap <M-D-Up> ddKp
nnoremap <M-D-Down> ddP
" swap word under the cursor with previous word on the left
" from the Vim wiki
nnoremap <M-D-Left> "_yiw?\w\+\_W\+\%#<CR>:s/\(\%#\w\+\)\(\_W\+\)\(\w\+\)/\3\2\1/<CR><C-o><C-l>
" swap word under the cursor with next word on the right
" from the Vim wiki
nnoremap <M-D-Right> "_yiw:s/\(\%#\w\+\)\(\_W\+\)\(\w\+\)/\3\2\1/<CR><C-o>/\w\+\_W\+<CR><C-l>
" and I have lusty-explorers "modes" mapped to:
" "files" <leader>f
" "buffers" <leader>b
" "grep" <leader>g
Useful for navigating up/down long lines that wrap to multiple lines on your display: Alt + ↑ or ↓ arrow keys move through screen lines rather than file lines.
map <A-Down> gj
map <A-Up> gk
imap <A-Up> <ESC>gki
imap <A-Down> <ESC>gji
I'm not a fan of 80(or 72) characters pr. line followed by a line-break even if your VIM inserts the line-break itself - you'll easily run into inconsistency problems when editing that line of text afterwards. Otherwise I have nothing against the editor, but somehow editing text as I do in a GUI editor makes me sleep better at night.
So, I discovered that the reason for the line breaks was primarily due to inability to move through softly wrapped lines, and hence I found this article: http://vim.wikia.com/wiki/Move_through_wrapped_lines which works, but I'm looking for a solution that would work in insert-mode as well as edit-mode.
P.S. I'm probably a newbie at VIM :)
Why would you need to move through wrapped lines in insert mode? You'd better move through such lines in command mode with gj and gk and when you need to edit something, press i, edit and go out of insert mode.
The less time you'll be spending in insert mode, the better.
imap <Down> <C-o>gj
and
imap <Up> <C-o>gk
works for me.
My configuration is as follows:
vmap <silent> <Right> l
vmap <silent> <Left> h
vmap <silent> <Up> gk
vmap <silent> <Down> gj
nmap <silent> <Right> l
nmap <silent> <Left> h
nmap <silent> <Up> gk
nmap <silent> <Down> gj
imap <silent> <Up> <C-o>gk
imap <silent> <Down> <C-o>gj
My complete configuration is here:
https://github.com/Waxolunist/vimconf