Vim backspace not working [duplicate] - vim

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
vim backspace leaves ^?
My Vim Backspace is not working correctly. When I press it, it prints out ^?. I found some other suggestions on how to fix it such as adding set backspace=indent,eol,start to the .vimrc. But this does not seem to work right. Does anyone have any other ideas on how to fix this?
Thanks!

perreal's answer tells you how to alter the Linux console to match vim's current settings, but you also have the option of altering vim's settings to match the Linux console. The relevant options are t_kb and t_kD to tell vim what characters the terminal generates when you press the Backspace and Delete keys respectively. Query the current settings first so you can see what they're set to:
:set t_kb? t_kD?
and then change them like this:
:set t_kb=XXX t_kD=XXX
In place of the XXX's you should type Ctrl-V followed by Backspace for the first one and Ctrl-V followed by Del for the second one.
There's another question you could be asking, about why these settings weren't properly detected in the first place. If you're interested in investigating that, we'll need to see your $TERM variable and the corresponding terminfo database entry, which you can get with:
echo $TERM
infocmp | egrep 'kdch1|kbs'

From vim-docs :help fixdel
Note about Linux: By default the backspace key produces CTRL-?,
which is wrong. You can fix it by putting this line in your
rc.local:
echo "keycode 14 = BackSpace" | loadkeys

Related

how to clear the screen after exit vim

Sometimes vim will leave something, i.e. press any key to continue, on the terminal and is there any way to return to a clear terminal after exiting vim?
I am new to vim and please tell me exactly what I should do.
Sorry I did not express my idea clear enough the first time. What I actually want to ask is that is there a way to return to a clear terminal after typing :q in vim without further input of commands.
I am using VIM 7.4 in Ubuntu, terminal type is xterm.
Add to your ~/.vimrc:
au VimLeave * :!clear
I use an alias to clear on any exit method including :q, :wqa. (this is for my osx brewed binary, find your own vim binary with which vim)
# .zshrc
alias vim="/usr/local/Cellar/vim/8.0.0094/bin/vim && clear"
There is a way to do more stuffs by editing your .vimrc file
Add this to your .vimrc
command Qc :call ClearAndExit()
function ClearAndExit()
:!clear
:q!
endfunction
use :Qc to quit.... it will clear the screen as well
If Vim is compiled with support for switching xterm-screens, it can do this by default, if you set the t_ti and t_te (Vim usually figures out, to what values this needs to be set by itsself). The gory details are explained at :h xterm-screens (pasted below)
(From comp.editors, by Juergen Weigert, in reply to a question)
:> Another question is that after exiting vim, the screen is left as
it :> was, i.e. the contents of the file I was viewing (editing) was
left on :> the screen. The output from my previous like "ls" were
lost, :> ie. no longer in the scrolling buffer. I know that there is a
way to :> restore the screen after exiting vim or other vi like
editors, :> I just don't know how. Helps are appreciated. Thanks. : :I
imagine someone else can answer this. I assume though that vim and vi
do :the same thing as each other for a given xterm setup.
They not necessarily do the same thing, as this may be a termcap vs.
terminfo problem. You should be aware that there are two databases
for describing attributes of a particular type of terminal: termcap
and terminfo. This can cause differences when the entries differ AND
when of the programs in question one uses terminfo and the other uses
termcap (also see +terminfo).
In your particular problem, you are looking for the control sequences
^[[?47h and ^[[?47l. These switch between xterms alternate and main
screen buffer. As a quick workaround a command sequence like
echo -n "^[[?47h"; vim ... ; echo -n "^[[?47l" may do what you want. (My notation ^[ means the ESC character, further down you'll
see that the databases use \E instead).
On startup, vim echoes the value of the termcap variable ti (terminfo:
smcup) to the terminal. When exiting, it echoes te (terminfo: rmcup).
Thus these two variables are the correct place where the above
mentioned control sequences should go.
Compare your xterm termcap entry (found in /etc/termcap) with your
xterm terminfo entry (retrieved with "infocmp -C xterm"). Both should
contain entries similar to:
:te=\E[2J\E[?47l\E8:ti=\E7\E[?47h:
PS: If you find any difference, someone (your sysadmin?) should better
check
the complete termcap and terminfo database for consistency.
NOTE 1: If you recompile Vim with FEAT_XTERM_SAVE defined in
feature.h, the builtin xterm will include the mentioned "te" and "ti"
entries.
NOTE 2: If you want to disable the screen switching, and you don't
want to change your termcap, you can add these lines to your .vimrc:
:set t_ti= t_te=
Yes. You most certainly can.
Use the UNIX command to clear the screen.
clear
I didn't want to have to use a different command to exit Vim (e.g. :Qc as suggested) by manoj, and the EntangledLoops' .vimrc method didn't work for me.
Inspired by Plato's answer, I found a similar solution by putting the following function in .bashrc:
# Vim exits to clear terminal screen
function vim {
/usr/bin/env vim "$#" && clear
}
Using only vim inside the function was problematic due to the function calling itself recursively and creating an infinite loop, but adding /usr/bin/env eliminates this issue, ignoring the function and executing the first vim in the PATH.

Fixing Backspace in Vim

I'm having issues with my backspace in Vim, it is inserting ^? instead of deleting characters. I found this wonder bit of Vim documentation that will hopefully fix this problem.
If the backspace key terminal code is wrong you can
use this:
:if &term == "termname"
: set t_kb=^V<BS>
: fixdel
:endif
Where "^V" is CTRL-V and "<BS>" is the backspace key
(don't type four characters!). Replace "termname"
with your terminal name.
I am trying to add this bit of code to my .vimrc as the site suggests, but how do I know what my "terminal name" is?
When you see &something in Vimscript, the something stands for one of Vim’s options (as are set with :set); &something refers to the current value or setting of something. You can use echo (or echom) to get the current setting of a given option. So in this case, you can get the terminal name — what is referred to in that code as &term — by starting Vim and running
:echom &term
and you can then replace termname in the code with the result.
(You can also use :set for this, as it turns out, by appending a ? to the option; running
:set term?
would print out the current setting of term, as e.g. term=xterm-color.)

How to disable 'vi compatible' mode for Vim in Cygwin on Windows 8?

I am using Cygwin 1.7.22 (32-bit) on Windows 8 (64-bit). Within Cygwin, I am using Vim 7.3.1152, which is the default version.
Behavior that seem like bugs:
When I press I to enter insert mode, it does not say -- INSERT -- in the bottom left. In fact, it doesn't say anything. It does behave correctly, though.
When I delete letters using Backspace in insert mode, the letters do not disappear but the cursor does move to the left.
When I use the arrow keys in insert mode, it enters the letters A, B, C, and D, rather than moving the cursor. The arrow keys work normally outside of insert mode.
How do I make Vim behave as I expect?
Create a ~/.vimrc file with the following contents to put vim in nocompatible mode (actually the mere presence of the file is sufficient.)
set nocompatible
The behavior you are seeing is how vi used to behave. These are not bugs.
Take a look at :h nocompatible
In vim compatible mode tries to emulate vi as closely as possible.
--insert-- is not part of vi so it not shown in compatible mode.
I believe vi did a lazy redraw of the screen and didn't update until you exited back to normal mode. Also backspace is only usable also only works on stuff that was entered in the current insert mode. Overall its not very user friendly.
The arrow keys are sent to vim as escape sequences (escape followed by a coupled of letters). Let ^[ be escape. ^[OA is up on my computer its probably something similar on yours. vim sees this as an escape (goes back to normal mode), and O (add a line above the current) and A which is the A you see entered onto your screen. This just means that vim in compatible mode does not interpret the escape characters properly. Most likely because vi did not interpret them (nor was it designed to use them).
set nocompatible fixes problems 1 and 3.
I think set backspace=indent,eol,start should fix problem 2.
This was asked months ago, but I am answering for future reference for anyone else who encounters this problem.
I was just bitten by this issue. All advice listed in this post, and in other posts on this forum (not to mention posts on other forums) does not work, at least for some of us. I finally figured out the real issue.
vim on cygwin, for whatever reason (at least this was the case for me) does not use the .vimrc you put in your directory. Let's say you copy the example one to your working directory, or copy some .vimrc from online. Or maybe you create a new one from scratch, and put all the settings the good people here and elsewhere recommend (set backspace = blahblah, set nocompatible, set this, set that). It doesn't work. Why? Because for whatever reason (at least in my case) vim isn't looking at the .vimrc you just created.
The solution is to FORCE vim to use a particular .vimrc, by passing in -u on the command line like so:
vim -u [/INSERT/PATH/TO/.vimrc]
For the love of all that is holy, DO NOT type square brackets or the words "/INSERT/PATH/TO/.vimrc" verbatim. Use your brain please.
Anyway, this solved my problems and I was able to use the default example .vimrc and get proper delete and backspace behavior while in insert mode, not to mention other goodies.
You might want to alias the vim command in your .bashrc like this:
alias vim='vim -u [/INSERT/PATH/TO/.vimrc]'
Regarding A,B,C,D for arrow keys in Vim, adding:
:set term=cons25
to ~/.vimrc worked like a charm.
source: http://vim.wikia.com/wiki/Fix_arrow_keys_that_display_A_B_C_D_on_remote_shell
Following different answers in this topic I found a simple solution.
$ vi --version | head
VIM - Vi IMproved 8.2 (2019 Dec 12, compiled Mar 30 2020 21:54:08)
Included patches: 1-486
Modified by <cygwin#cygwin.com>
Compiled by <cygwin#cygwin.com>
$ vi --version | grep 'user vimrc'
user vimrc file: "$HOME/.virc"
2nd user vimrc file: "~/.vim/vimrc"
So I just created ~/.virc (not vimrc) and it works! The content of the file:
set nocompatible
Probably, if you already have this file you will add the above string in it. Or, as people say above, if you have an empty ~/.virc, nocompatible mode must be already in use (I didn't check it).
Apart of the question, line numbers (that I find very useful) may be shown in vi by adding:
set number

Vim how to remove line number on copy [duplicate]

This question already has answers here:
How to clear the line number in Vim when copying?
(12 answers)
Closed 9 years ago.
When I copy lines with vim on my ssh term I always have the line number:
1 <?php
2 echo "test";
3
I try many things like:
set mouse=a
set mousehide
set pastetoggle=
I enter into visual mode it's doesn't highlight line numbers but I can't copy.
I don't want to unset number and set again each time I want to copy something ;)
Does a script exist? Like autocmd or something like that?
I'm on Mac Os X, but I use vim over ssh (-clipboard).
Note it's not a duplicate of: How to clear the line number in Vim when copying?
So it's a duplicate of: Scrolling inside Vim in Mac's Terminal
If moderator can (re)link please?
Don't use the mouse. Copy to the + register instead with
:1,3y+
which copies it to your system's clipboard. For this to work, your vim must have been compiled with the clipboard option (i.e., :version should have +clipboard in the options).
Most Vims can interact with the clipboard; just yank the text into register "+ (or "*), see :help quoteplus.
If you absolutely must copy directly from the terminal with the mouse, you have to temporarily :set nonumber.
You can write a mapping to speed that up:
:nnoremap <Leader>n :set invnumber number?<CR>

Delete Key is changing letter case in Vim

I'm trying to get into Vim. I'm running it in the terminal on OS X.
Anytime I hit the delete key, it simply changes case of that letter instead of deleting it. When I SSH into my server and use Vim there, it deletes normally.
Any ideas what may be going wrong?
The problem
The Del key generates the code ^[[3~ in my urxvt terminal on GNU/Linux, and might generate a similar code in your OS X terminal.
My theory is that Vim for some reason doesn't recognize any keybinding for the delete key, and simply tries to interpret the string ^[[3~ as input instead. ^[ is the keycode for the Esc key (which puts you in normal mode), and ~ is the Vim command for changing the case of a letter (from normal mode).
You can confirm the keycodes I mentioned by pressing Ctrl+V Esc and Ctrl+V Del from insert mode in Vim. Ctrl+V means that the next character should be inserted as text instead of being interpreted by the editor.
The solution
As for the solution, try editing your Vim configuration file (presumably ~/.vimrc):
vim ~/.vimrc
And append the following code to it:
nmap <Ctrl-V><Del> x
imap <Ctrl-V><Del> <Ctrl-V><Esc>lxi
I hope this helps :)
The problem was that in my .vimrc I had
set term = ansi
Took that out and all was well - sorry about the troubles, thanks!
Well, this took forever for me to resolve. When using vim I was in Iterm2 on macOSx to access a Centos5 system via gnu screen. Not only was the delete key changing the letter case, and causing delays in vim, but also the arrow keys didnt work. I think the problem was simply in the old configuration on the Centos5 machine, but may have had something to also do with either iterm or gnu screen, but i hadnt had this issue at all on any Centos6 systems.
What did Not work:
I compile vim 7.4, that did not resolve it, so don't go down that path.
I also tried these configurations that did not work
"set term=ansi
"set backspace=indent,eol,start
"set nocompatible
"fixdel
":if &term == "xterm"
":if &term == "xterm-256"
": set t_kD=^V<Delete>
":endif
"nmap <Ctrl-V><Del> x
"imap <Ctrl-V><Del> <Ctrl-V><Esc>xi
I finally found the solution.
Solution: Set the following in .vimrc
set term-builtin_xterm
Additionally, you may also notice that 256 colors have an issue on this same system. So if it helps, you can check your TERM environment variable echo $TERM. If it is xterm you can switch it to 256 colors, e.g. export TERM='xterm-256color to get the color schemes back on track for this same system. reference
I had the same issue where vim suddenly interpreted keys differently.
The answer for me was that the environment variable $TERM had somehow been to to 'dumb' in my terminal.
I added
export TERM=xterm-color
to ~/.bashrc (in my case) and that fixed all the issues with keys; DEL worked as expected etc.
I didn't need to add or change anything in ~/.vimrc
I should mention this affected more than just vim for me - if you also see 'less' behave differently for example, you may have the same cause as I did.
I use vim regularly on my OSX machine (vim version 7.2.108), and I do not have this issue. Try renaming your vimrc file and then reload vim, and see if the issue persists. If there is no issue after renaming you vimrc file, then your issue is in that file.
On my machine, my vimrc file is pretty much empty:
set ruler
set tabstop=2
set cindent
set number
syntax on
None of these solutions worked for me, until I found a solution on the vim fandom website.
Those solutions are for a similar problems with arrow keys, which I was experiencing in addition to the delete key problem.
Solution 8 fixed it for me, the problem was that I had remapped <Esc> to <Esc>l in order to move the cursor right every time I entered normal mode, evidently this caused any other key code with included ^[ (which is the escape code) including the arrow keys ( ^[OA ^[OB etc ) and the delete key ( ^[[3~ ) to be misunderstood.
Removing the offending line from my .vimrc fixed the problem.

Resources