When I quit vim, the display reverts to whatever I was seeing before I entered. On non-vim vi, you keep the vi screen intact except for the bottom line.
Is there an option in vim that allows that latter behaviour?
Vim uses a terminal feature called the alternate screen to write its UI there, and restore the original shell contents (where Vim was launched from) on exit. This is controlled by two ANSI escape sequences (see how does vi restore terminal content after quitting it).
You can disable that from within Vim by clearing the corresponding terminal settings. Put the following into your ~/.vimrc:
set t_ti= t_te=
Alternatively, you could also disable this capability in the terminal; at least the multiplexers screen and tmux allow this. See Prevent Applications Like Vim and Less Clearing Screen on Exit for details.
May be this is what you are looking for, just paste the below commands in your .vimrc. I got it from some where in the net.
" Return to last edit position when opening files
autocmd BufReadPost *
\ if line("'\"") > 0 && line("'\"") <= line("$") |
\ exe "normal! g`\"" |
\ endif
Related
My centos 7 has two vim binaries:
/usr/bin/vi
/usr/bin/vim
When launching either of them they bring up:
VIM - Vi IMproved
version 7.4.1099
From searching around online I believe that these two versions are vim and vim-minimal. The problem is that vi and vim-minimal interpert the ~/.vimrc differently. When opening vim its great. When opening vim-minimal I get lots of errors. Below is a representation of my ~/.vimrc file:
set number
set nowrap
set modeline
set clipboard=unnamedplus "Enables mouse center clip pasting, while holding shift, in insert mode.
"Below sets up the mouse in such a way that allows vi split screen resizing while in tmux and screen.
set mouse+=a
if &term =~ '^screen'
" tmux knows the extended mouse mode
set ttymouse=xterm2
endif
""""""""""""""""""""""""""""""""""""""""""""""""
""" User Defined Functions
""""""""""""""""""""""""""""""""""""""""""""""""
"Opens up sage specific work in new tabs
fu! Setup1()
:bd!|e /home/me/example1.h | vsplit /home/me/example1.cc
:tabnew /home/me/example2.h | vsplit /home/me/example2.cc
:tabnew /home/me/example3.h | vsplit /home/me/example3.cc
:tabnew /home/me/example4.h | vsplit /home/me/example4.cc
:tabnew /home/me/example5.h | vsplit /home/me/example5.cc
:tabnew /home/me/example6.h | vsplit /home/me/example6.cc
endfunction
"Opens up the most edited rc files in new tabs
fu! RCS()
:bd!|e ~/.cshrc
:tabnew ~/.vimrc
:tabnew ~/.tmux.conf
endfunction
The problem is my user defined functions. When /usr/bin/vi is opened it opens all of the documents in my two functions in new tabs. A resonable workaround would be to not use /usr/bin/vi but it is what git opens.
Ideally I would be able to have an if statement that checks what binary is running this rc file. Is that possible?
You can configure git to use the editor of your choice. You can set the environment variable GIT_EDITOR or you can set a configuration file with git config --global core.editor /usr/bin/vim to set the core.editor variable.
If neither of those is set, it may fall back to the VISUAL environment variable. On that note, you will likely want to set that in your ~/.bashrc as a more system-wide solution so other utilities that may want to open an editor default to /usr/bin/vim over /usr/bin/vi
The following blog post discusses how to degrade vimrc for different builds of vim that may have different packages and different capabilities (and thus react to vimrc differently):
gracefully degrading .vimrc
(answer provided by max630 in the comments to my question)
I may have accidentally changed some setting but now I can't figure out what it is.
Behavior:
Whenever I edit an existing file, Vim starts in replace (R) mode. This is new as of this week.
Whenever I do vim nonExistantFile.txt, Vim starts in normal mode
Steps so far to diagnose:
/etc/vimrc has nothing relevant, and specifically does not contain startreplace
/home/<myuser>/.vimrc has nothing relevant, and specifically does not contain startreplace
alias shows nothing vim related
Platform info:
Vim version 7.3.1314
Windows 7 64-Bit
32 Bit Cygwin and Vim binary
Other potentially relevant information:
Some time ago (before this started happening) I copied /etc/vimrc from my CentOS 6.4 machine into the Cygwin /etc/vimrc
I edited some binary files recently
I'm pretty stumped, I can't think of anything else to try.
Update:
I have narrowed the problem down to this /etc/vimrc snippet
Oddly commenting out EITHER of these two commands solves the problem
Vimrc snippet (note: this does have unix line endings):
" Only do this part when compiled with support for autocommands
if has("autocmd")
"<snip>
" When editing a file, always jump to the last cursor position
autocmd BufReadPost *
\ if line("'\"") > 0 && line ("'\"") <= line("$") |
\ exe "normal! g'\"" |
\ endif
" don't write swapfile on most commonly used directories for NFS mounts or USB sticks
autocmd BufNewFile,BufReadPre /media/*,/mnt/* set directory=~/tmp,/var/tmp,/tmp
"<snip>
endif
I mapped nnoremap <ESC> :nohlsearch<CR> in my .vimrc and that caused this behaviour.
Mapping this to <Enter> solved the problem for me.
check ~/.exrc files for initial settings, there must be a flag setting your vi into REPLACE mode at startup!
http://alvinalexander.com/unix/edu/un010003/
cheers!
I added a few newlines and swapped the two commands in /etc/vimrc that seemed to be causing the problem... and this seemed to fix it. I have no idea why.
The following seemed to fix this problem for me. I think that there was some autocommand code that interfered with the cursor position restore, so clearing out previous autocommands as below fixed the problem for me:
function! ResCur()
if line("'\"") <= line("$")
normal! g`"
return 1
endif
endfunction
augroup resCur
autocmd!
autocmd BufWinEnter * call ResCur()
augroup END
http://vim.wikia.com/wiki/Restore_cursor_to_file_position_in_previous_editing_session
I've been making a transition to GVim lately, because I find it to be more aesthetically pleasing and a little bit faster then vim in the terminal. I have this really bad habit that I'm trying to break. When I used vim from the command line my work flow was like this:
vim filename.txt
# make some edits
ZZ
# do other stuff
vim otherfile.txt
# make some edits
ZZ
Now using GVim, I end up closing the editor far too frequently. I'm wondering if there is a way to force just GVim to either prompt me or open an empty buffer when I do a :wq or ZZ. Any ideas?
EDIT: I know how to remap keys, but I'm wondering if there is a way to force GVim to have a different behavior then when vim is called from the command line.
Call a function on ZZ and if there is only one tab and window left, prompt whether to close or not (default is to close). See :help confirm().
nnoremap ZZ :call QuitPrompt()<cr>
fun! QuitPrompt()
if has("gui_running") && tabpagenr("$") == 1 && winnr("$") == 1
let choice = confirm("Close?", "&yes\n&no", 1)
if choice == 1 | wq | endif
else | wq | endif
endfun
Putting the following in your vimrc could be used to completely disable the ZZ shortcut altogether:
nnoremap ZZ <Nop>
Or you could remap them to the standard behaviour of :q :
if has("gui_running")
nnoremap ZZ :q
nnoremap :wq :q
endif
Add this in your .vimrc:
set confirm
Gvim and vim handle quit a little differently. Option 1 works as you would expect in both. Option 2 works nice in vim, but in gvim you can get around it (in part) by pressing the "X" to close in X Windows. It will still confirm if the file is unedited, but it will just quit everything when everything is saved. Maybe someone else knows how to deal with that issue.
Option 1: Confirm quit only when a file is unsaved
Add this in your .vimrc:
set confirm
Option 2: Confirm quit all the time (when you do ZZ or q)
Use this plugin
Plugin 'vim-scripts/confirm-quit'
Or hand code it, following along with this to see what you need to do (BTW -- this plugin was born from this SE Question)
When I open Vim from a terminal, copy some text to the system clipboard, and exit Vim, the system clipboard gets cleared.
How to keep the copied text in the clipboard?
Synthesizing answers from superuser, just add the following to your .vimrc
autocmd VimLeave * call system("xsel -ib", getreg('+'))
Install Parcellite, or glipper for Gnome and klipper for KDE.
Restart your computer or run it manually.
see: https://wiki.ubuntu.com/ClipboardPersistence
Based on Matt's answer, the following uses xclip instead of xsel:
autocmd VimLeave * call system('echo ' . shellescape(getreg('+')) .
\ ' | xclip -selection clipboard')
I ran into this issue and a related problem where suspending vim with ctrl-z would also clear the clipboard. I've extended Matt's solution to fix both:
set clipboard=unnamedplus
if executable("xsel")
function! PreserveClipboard()
call system("xsel -ib", getreg('+'))
endfunction
function! PreserveClipboadAndSuspend()
call PreserveClipboard()
suspend
endfunction
autocmd VimLeave * call PreserveClipboard()
nnoremap <silent> <c-z> :call PreserveClipboadAndSuspend()<cr>
vnoremap <silent> <c-z> :<c-u>call PreserveClipboadAndSuspend()<cr>
endif
The if executable("xsel") guard is there to avoid errors if xsel is not installed. The nnoremap mapping preserves the clipboard when suspending from normal mode and the vnoremap mapping handles suspending from visual or select modes.
I've confirmed this works on vim 7.4 and 8.0.
Use NeoVim. It by default doesn't clear the clipboard on exit. You will still need to set clipboard=unnamedplus (typically in ~/.config/nvim/init.vim) and have xsel or xclip tools installed.
Keep in mind that some other defaults are different as well.
Please correct me if I'm wrong but from my understandings of Vim...
1) Vim uses registers instead of the clipboard to store copied/cut data.
2) These registers are preserved when exiting vim in a status file but are not accessible outside of the running process unless you manually open the file and inspect its contents
3) Saving stuff to the + registre while Vim runs allows you to paste to other applications.
4) By suspending vim (CTRL-Z) instead of closing it, these registers are still accessible.
Does that provide assistance?
Based on Matt's answer
When using his method copying multiple lines added slashes to the end of lines when pasting.
This should remedy that.
autocmd VimLeave * exe ":!echo " . shellescape(getreg('+')) . " | xclip -selection clipboard"
When i used "shellescape" with "system" newlines kept getting escaped. But that didn't happen when i used exe.
Don't know the real reason. but this worked.
According to the answer of this question, the :bd command should not quit Vim (gVim) when it is the last buffer remaining. Unfortunately, it does close gVim in my case. Did I understand something wrong about :bd?
I am also using a preconfigured vimrc file. Maybe a setting in there has that side affect, but I couldn’t find it.
Try doing the following:
:set eventignore=all | bd | set eventignore=
If this won't quit vim then you have some plugin that defines an autocommand that quits vim when no more buffers are present in the list, so after that try doing
verbose autocmd BufWinLeave,BufLeave,BufDelete,BufUnload,BufWipeout
This will show you all autocommands attached to given events (these are events that are executed when buffer is deleted) and where they were defined. Note that I do not have any autocommands attached to these events that are defined by plugins in standart vim distribution.
Update: I do not see anything bad in your output. You may also try
verbose autocmd BufNew,BufAdd,BufCreate,BufEnter,BufWinEnter
(because when you leave last buffer new empty one is created). If this does not show anything suspicious, start ignoring event types: if you are on linux try the following script:
for event in BufWinLeave BufLeave BufDelete BufUnload BufWipeout BufNew BufAdd BufCreate BufEnter BufWinEnter
do
event=$event vim file -c "set eventignore=$event | bd"
done
This script should iterate until you find event name that causes trouble. After this you can use execute "verbose autocmd" $event in vim to narrow number of plugins that should be checked. After you got list of autocmd groups (augroup names are shown just before event name in your output: railsPluginDetect is one of such groups), delete events in them (augroup {GroupName} | execute 'autocmd!' | augroup END) and find out which plugin to claim.
Alternatively, you can use debugger:
debug bd
, then s<CR>n<CR><CR><CR>... until vim quits; don't forget to remember what vim have shown above > prompt before quiting.