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 ...
Related
How can I configure Vim to set
"require 'pry'; binding.pry"
in Ruby and
"debugger;"
in JavaScript when pressing F2 via key mapping?
You can set this in your .vimrc as follows:
autocmd FileType ruby map <F2> orequire 'pry'; binding.pry<ESC>
autocmd FileType javascript map <F2> odebugger;<ESC>
When the F2 key is pressed in a *.rb file, "require pry" will be set and "debugger" is set in a *.js file.
The other answer is correct, but not completely correct. You should use the noremap variant of map (see :h noremap), and the proper noremap for whatever mode your are in. If that's insert mode, then it's inoremap <F2> require..., or nnoremap for normal mode, etc.
You can also put those mappings into their own file instead of your vimrc so that you don't need to use autocommands (see :h ftplugin). And (thanks to the comments for reminding me) use <buffer> mappings so they only apply to the file you set them on (see :h <buffer>). In all, this is a good setup for you:
In ~/vim/after/ftplugin/ruby.vim, put the line:
inoremap <buffer> <F2> require 'pry'; binding.pry
and in ~/vim/after/ftplugin/javascript.vim, put the line:
inoremap <buffer> <F2> defbugger;
On windows, the vim directory is instead the vimfiles directory. If you want those mappings in normal mode instead of insert mode, you need to put i or O or another character like that at the front to go into insert mode and put <Esc> on the end to exit insert mode.
I am looking for a way to automatically resize my open v-split panes in Vim after I call NERDTreeToggle.
I have NERDTreeToggle being called on the shortcut "ctrl+\" at the moment, and ideally what I want is to call the keyboard shortcut "ctrl+w =" immediately afterwards.
Any ideas? Thanks.
If this is your current mapping:
:nnoremap <C-\> :NERDTreeToggle<CR>
You can just append the window command after it:
:nnoremap <C-\> :NERDTreeToggle<CR><C-w>=
Alternatively, you can execute this from command-line mode as well, via :normal!:
:nnoremap <C-\> :NERDTreeToggle<Bar>execute "normal! \<lt>C-w>="<CR>
Note that for window commands, there's also a special :wincmd to invoke them:
:nnoremap <C-\> :NERDTreeToggle<Bar>wincmd =<CR>
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
When I use the arrow keys in vim in insert mode I get letters inserted instead of movement.
Up produces an A
Down produces a B
Left products a D
Right produces a C
Does anyone know what would cause this?
Thanks in advance
If these keys work fine in normal mode, but do not in insert then you must have some mappings to the first one or two characters (normally <Up> is either <Esc>[A (terminals that use CSI) or <Esc>OA (xterm)). Try checking out output of
verbose imap <Esc>
, there should be not much mappings starting with <Esc> in insert mode (I have none, for example). I can say, that with arrow keys working normally in insert mode, using
inoremap <Esc> <Esc>
produces just the same behavior as if you had problems with terminal recognition or had 'compatible' set.
Your vim seems to be starting in the vi compatibility mode. Do this
Open Vim editor,
Get the path of your home directory by typing :echo $HOME
Check if you have .vimrc file in $HOME location,(if you don't have create it)
Add the following line line to .vimrc file
:set nocompatible
Find more solutions for the same problem here ( Especially if your problem is terminal related, the re-mapping of keys solution might work for you )
The following worked for me. Just put it in your .vimrc
:set term=cons25
Open Vim editor.
Get the path of your home directory by typing: :echo $HOME.
Check if you have .vimrc file in $HOME location, and if you don't have create it.
Add the following line line to .vimrc file: :set nocompatible
Reference: http://vim.wikia.com/wiki/Fix_arrow_keys_that_display_A_B_C_D_on_remote_shell
None of the answer here worked for me. I'm in Linux, with konsole/yakuake terminal and tmux. This fix works for me:
nnoremap <silent> <ESC>OA <ESC>ki
nnoremap <silent> <ESC>OB <ESC>ji
nnoremap <silent> <ESC>OC <ESC>hi
nnoremap <silent> <ESC>OD <ESC>li
inoremap <silent> <ESC>OA <ESC>ki
inoremap <silent> <ESC>OB <ESC>ji
inoremap <silent> <ESC>OC <ESC>hi
inoremap <silent> <ESC>OD <ESC>li
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.