How to use <S-Up> & <S-Down> in Terminal.app & vim - vim

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

Related

How can I get the terminal key code in vim

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

How do I debug a non-functioning keymap in Vim?

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

Custom Hotkeys in gvim

I am using gvim under linux and I really love it, the problem I have with it is that Shorcuts like Crlt+C doesn't work...
I added the following code to my gvimrc but it doesn't have any effect :/
nmap <C-V> "+gP
nmap <C-V> "+y
nmap <C-A> ggVG
nmap <C-Z> u
nmap <C-Y> ^R
The problem is not that the shortcuts don't work, rather than that you don't know what the shortcuts are supposed to do.
It might help for you if you add behave mswin to your .vimrc. It remaps many key bindings to behave more like other programs.

Vim execute code on remapping in vimrc

I have created the following mapping in my vimrc file:
noremap <C-p> ! firefox -new-tab http://php.net/<cword><C-m>
The problem is that it proceeds to delete the line under the cursor.
How would I make this mapping work correctly?
If I understood what you're trying to do the following should work:
nmap <c-p> :!firefox -new-tab http://php.net/<c-r>=expand('<cword>')<CR><CR>
See docs for further information.
You are using noremap which means that it triggers:
in normal mode
in visual mode
in operator-pending mode.
You should use nnoremap or xnoremap instead. Also, note that ! is a shortcut for :.! in normal mode (acts on current line) and for :'<,'>! in visual mode (acts on current visually-selected lines). If you don't want that:
nnoremap <c-p> :!firefox ...
xnoremap <c-p> :<c-u>!firefox ...

How to increment in vim under windows (where CTRL-A does not work...)

While CtrlX works fine in vim under windows, CtrlA selects all (duh).
Is there a way to increment a number with a keystroke under windows?
You can make CtrlA to increment in windows by opening up the 'mswin.vim' file in your vim directory and finding the section that looks like:
" CTRL-A is Select all
noremap <C-A> gggH<C-O>G
inoremap <C-A> <C-O>gg<C-O>gH<C-O>G
cnoremap <C-A> <C-C>gggH<C-O>G
onoremap <C-A> <C-C>gggH<C-O>G
snoremap <C-A> <C-C>gggH<C-O>G
xnoremap <C-A> <C-C>ggVG
Comment out all of these lines as follows:
" CTRL-A is Select all
"noremap <C-A> gggH<C-O>G
"inoremap <C-A> <C-O>gg<C-O>gH<C-O>G
"cnoremap <C-A> <C-C>gggH<C-O>G
"onoremap <C-A> <C-C>gggH<C-O>G
"snoremap <C-A> <C-C>gggH<C-O>G
"xnoremap <C-A> <C-C>ggVG
and the CtrlA keystroke will increment.
This is a pretty nice option when your keyboard doesn't have a real number pad.
Try Ctrl-NumPad + ?
(from here)
I realize that this is an old question, but I ran across another option today based on the following question. Making gvim act like it does on linux will allow CTRL-A to work as you expect it to:
how to make gvim on windows behave exacly like linux console vim?
There is a section of the _vimrc that has the following items. These cause many of the control characters to act like they do on Windows.
set nocompatible
source $VIMRUNTIME/vimrc_example.vim
source $VIMRUNTIME/mswin.vim
behave mswin
I commented out (with ") the mswin lines and the set nocompatible line. From there, I added set compatible. This causes gvim to act like it does on linux. Thus, mine looks something like:
set compatible
source $VIMRUNTIME/vimrc_example.vim
"set nocompatible
"source $VIMRUNTIME/mswin.vim
"behave mswin
I just learned this trick today, so if I'm not completely correct in my information, please let me know.
I modified TMealy's solution so that CtrlA still selects all (I find this useful), while CtrlI increments (also useful).
noremap <C-I> <C-A>
" CTRL-A is Select all
noremap <C-A> gggH<C-O>G
inoremap <C-A> <C-O>gg<C-O>gH<C-O>G
cnoremap <C-A> <C-C>gggH<C-O>G
onoremap <C-A> <C-C>gggH<C-O>G
snoremap <C-A> <C-C>gggH<C-O>G
xnoremap <C-A> <C-C>ggVG
A similar problem occurs under GNU/Linux when using Vim with mswin.vim.
Remapping Alt+X to Ctrl+A prior to evoking mswin.vim solved my issue.
execute "set <A-x>=\ex"
noremap <A-x> <C-A>
source $VIMRUNTIME/mswin.vim
behave mswin
Now, Alt+X and Ctrl+X respectively increase and decrease numbers in Vim.
Mapping to Alt key combinations is often not evident in Vim; read more about this here.
I am using cygwin terminal + screen, so <c-a> is captured by the terminal multiplexer. I used this mapping:
:noremap <c-i> <c-a>
It seems that the CtrlA got mapped somewhere in startup.
As suggested before, use:
unmap <c-x>
I would use unmap, not nunmap.

Resources