Vim keyboard shortcut to replace ^M - vim

I manually replace ^M with
:%s/<ctrl-v><ctrl-m>//g after I open a log file. I have to do this several times a day so I tried to assign F11 key to perform this action.
I tried the following in .vimrc but it did not work.
:nnoremap <silent> <F11> :let _s=#/ <Bar> :%s/^M//e <Bar> :let #/=_s <Bar> :nohl <Bar> :unlet _s <CR>
Note: I pressed ctrl+vctrl+m to get ^M in the above statement.
I have a similar assignment for F10 to remove EOL whitespace and that works.
:nnoremap <silent> <F10> :let _s=#/ <Bar> :%s/\s\+$//e <Bar> :let #/=_s <Bar> :nohl <Bar> :unlet _s <CR>
What am I doing wrong here?

Check that your terminal actually forwards F11 to the commands it’s running instead of listening to the key itself. For instance, the default configuration of Windows Terminal intercepts F11 and does not forward it, so you can’t rebind the key inside Vim.
Furthermore, your command looks quite convoluted; my Vimscript knowledge is limited but if the purpose of your command is to change Windows file endings into UNIX file endings, the following, simpler command works:
:nnoremap <silent> <F11> :set fileformat=unix<CR>
There’s also a widely installed utility program, dos2unix, that does the same, and which might be even simpler for your use-case.

Related

Hitting ENTER on quickfix error jumps to an empty buffer

I'm using GVim 8.1 on Windows 10 with no external plugins.
I have the following set up in my .gvimrc file:
let g:build_file_abs_path = fnamemodify(findfile("windows-build.bat", ";."), ":p:h")
" This build script is a basic wrapper for 'clang.exe file.c -o file.exe' style invocation
let &makeprg=g:build_file_abs_path . "\\windows-build.bat"
nnoremap <silent> <C-B> :cd <C-R>=g:build_file_abs_path<CR> <bar> make! <bar> copen <bar> redraw <bar> cd -<CR>
Now, this automatically opens a quickfix window with the correct compiler output. However, when I press ENTER over the error, the cursor jumps to the buffer for the affected file, yet it is completely blank with a single line. Furthermore, this occurs as I use :cn and :cp commands inside the quickfix window. e.g:
Images showing these two states:
before
after
Please note that:
:verbose nmap <CR> returns no mappings, so there is not conflict there.
I would appreciate it if someone could provide some insight as to how to avoid the buffer becoming empty and actually jump to the error in the appropriate file. Many thanks.
Thanks to Christian Brabandt's comment, I was able to solve the issue. I was misunderstanding the distinction between the working directories of vim and the build script. I made the following changes:
let &makeprg="cd " . g:build_file_abs_path . " && windows-build.bat"
nnoremap <silent> <C-B> :make! <bar> copen <bar> redraw <CR>

<C-j> mapping not working in Vim when calling Vim from ZSH Vi mode

When calling Vim from zsh via vim, <C-j> works perfectly and I am able to use it to select completions in the same manner as <C-n>.
When starting Vim from zsh's vi mode via the v key, I cannot get <C-j> to behave the same as <C-n>, whether I input the key-bind manually or place it in the config.
Additionally, when I invoke edit-command-line manually, <C-j> works.
Note: this issue does not arise when calling Vim in a similar manner in Bash's vi mode.
.vimrc with Vim 8.0
inoremap <expr> <C-j> "\<C-N>"
inoremap <expr> <C-k> "\<C-P>"
.zshrc with zsh 5.3.1 (x86_64-apple-darwin14.5.0)
EDITOR="vim"
set -o vi
autoload -Uz edit-command-line
zle -N edit-command-line
bind key -M vicmd 'v' edit-command-line
EDIT: some further investigation reveals that when I start Vim through zsh's vi mode, <C-v><C-j> produces ^M; however, when I start Vim normally, <C-v><C-j> produces ^#.

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

Arrow keys in vim (linux) in insert mode broken for me

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

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