I have an existing .vimrc which was created on macintosh computer and contains mappings like this:
"------------------------------------------------------------
" keyboard shortcuts
"------------------------------------------------------------
nmap <S-Tab> :wincmd W<CR>
nmap <S-q> :bp\|bd #<CR>
nmap « :bp\|bd #<CR>
nmap ¢ :sp<CR>
nmap ¶ :vsp\|wincmd w<CR>
nmap :Q :qa<CR>
nmap :W :wqa<CR>
nmap ≠ :q<CR>
nmap ESC[1;9B 15j
nmap ESC[1;9A 15k
nmap ^? :w<CR>
nmap ø :edit
nmap ESC[1;5B 15j
nmap ESC[1;5A 15k
vmap ESC[1;5B 15j
vmap ESC[1;5A 15k
nmap ^\ :edit
nmap ^] :wincmd w<CR>
nmap <F9> :w<CR>:Make<CR><CR>
imap <F9> <ESC>:w<CR>:Make<CR>
nmap <C-n> :cnext<CR>
nmap <C-p> :cprev<CR>
nmap <F5> :Grep
nmap # g~w w
nmap <F10> :ConqueGdb
nmap ^A :sav %:h/filename
nmap ESC[1;2D :NERDTreeToggle<CR>
nmap ESC[1;2B :sp\|wincmd w<CR>
nmap ESC[1;2C :vsp\|wincmd w<CR>
nmap ESC[1;5D :q<CR>
nmap ESC[1;5F :BuffergatorToggle<CR>
nmap ESC[1;6D w
nmap ESC[1;6B b
Now I have transferred this .vimrc to a new linux computer (debian-based), but half of the shortcuts don't work anymore. I guess thats because the mac interpretes some of the keystrokes differently as compared to the linux computer (both computers do have vim v8.1).
When creating shortcuts, I remember that there was a function in vim which would print out the key codes when you press the keys, however that was about 10 years ago and I don't remember it, and I can't find via google.
To be precise, it was like "press XYZ in vim, then press e.g. CTRL+S, then vim would print the corresponding key code to be used in the mappings".
How can I do this now, in order to make those shortcuts work on linux also?
You're looking for :help i_CTRL-V:
For special keys, the terminal code is inserted.
For example, in my Gnome Terminal pressing <C-V><Right> gives ^[OC and <C-V><F12> gives ^[[24~.
In general, you should prefer Vim's key-notation whenever possible. If you do need to insert specific escape codes and need to support multiple platforms with a single ~/.vimrc, you can wrap the mapping definitions in conditions like if has('mac') or if $TERM ==# 'screen256-color'.
Related
I'd like to map <Shift> + <Arrow Keys> to select text, just like expected behavior in something like sublime text (idea comes from this: Mapping <Shift>-Arrows to selecting characters/lines). Using the setup I have below, <S-Left> and <S-Right> does work, but <S-Up> and <S-Down> does not work. Believe Terminal.app needs to add a keyboard action, or something?
In ~/.vimrc:
nmap <S-Up> v<Up>
nmap <S-Down> v<Down>
nmap <S-Left> v<Left>
nmap <S-Right> v<Right>
I see that in vim (in insert mode, then <C-v> + <S-Up> gets me this ^[OA, and <C-v> + <S-Down> gets me this ^[OB.
In Terminal.app preferences (default setup):
Turns out Terminal.app doesn't send the shift+up or shift+down codes to the terminal.
From here: https://github.com/timothybasanov/terminal-app-function-keys
Add this codes to the Terminal.app profile:
⇧↑ \033[1;2A
⇧↓ \033[1;2B
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
I'm using NERD-commenter on vim version 7.4.
I'd like to map <F8> to comment out multi lines with NERD-commenter 's command \cc.
I've checked that typing \cc is well working on blocked area.
The problem is that mapping <F8> to \cc is not working properly.
I've tried following lists in my .vimrc file.
nmap <F8> <leader>cc
nmap <F8> <Leader>cc
nmap <F8> <Plug>NERDComComment
...
How can I fix it?
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
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