vim do not save buffer history after closing - vim

What setting should I add to my .vimrc file so that no information about previous open buffers is saved to .viminfo? I do want to save command history though.
By not preserving buffer history I mean when I open a fresh vim instance and I do Ctrl+o or Ctrl+i to not show buffers that were open in a previous session, but only buffers that were opened during the current session.
Thanks

Add this into your .vimrc:
autocmd VimEnter *
\ execute "
\ let i = 0 |
\ while i < 100 |
\ mark ' |
\ let i = i + 1 |
\ endwhile |
\ unlet i |
\ delm A-Z0-9"
This will wipe out the jump list for every session and delete marks.

Related

Vim: reopen all previous files in a new session after restart

I usually have long lived vim session with dozens of files open. Once in a while, I need to restart vim, say to install a new plugin or apply some new config.
How can I reopen all the previous files after restart?
You can use builtin sessions, perhaps with vim-session plugin.
A Vim session contains all the information about what you are editing.
This includes things such as the file list, window layout, global variables,
options and other information. (Exactly what is remembered is controlled by
the 'sessionoptions' option.)
The command
:mksession mysession.vim
saves the current session into the named file. Next time you start vim you can load the session:
vim -S mysession.vim
or inside vim
:source mysession.vim
You can automate saving sessions on VimLeave autocommand and reload a session on VimEnter but be careful abut restoring a session when vim is called, e.g., from git.
Here is a basic setup of automatic session save / load extracted from my config:
let s:session_loaded = 1
augroup autosession
" load last session on start
" Note: without 'nested' filetypes are not restored.
autocmd VimEnter * nested call s:session_vim_enter()
autocmd VimLeavePre * call s:session_vim_leave()
augroup END
function! s:session_vim_enter()
if bufnr('$') == 1 && bufname('%') == '' && !&mod && getline(1, '$') == ['']
execute 'silent source ~/.vim/lastsession.vim'
else
let s:session_loaded = 0
endif
endfunction
function! s:session_vim_leave()
if s:session_loaded == 1
let sessionoptions = &sessionoptions
try
set sessionoptions-=options
set sessionoptions+=tabpages
execute 'mksession! ~/.vim/lastsession.vim'
finally
let &sessionoptions = sessionoptions
endtry
endif
endfunction
It will only restore the session if you run vim without arguments, so if you do vim somefile.txt to do the quick edit, it will not touch the last session.
Bonus (this will restore cursor position inside files too):
" When editing a file, always jump to the last known cursor position.
" Don't do it when the position is invalid or when inside an event
" handler.
autocmd MyAutoCmd BufReadPost *
\ if line("'\"") > 0 |
\ if line("'\"") <= line("$") |
\ exe("norm '\"") |
\ else |
\ exe "norm $" |
\ endif|
\ endif

How can I save in vim a file with the actual fold text? ("+-- 43 lines [...]")?

I am looking for a way to save to a new text file a file that is folded, with all the folds closed. In other words, just as I see it on the screen.
Is it possible?
(I will have to print the code later, and parts of it are irrelevant to my purposes; the folding mechanism would be ideal for this, my other alternative is manually adding "[X lines omitted]" to the saved text.)
Fold your text as needed and then use :TOhtml to convert to an HTML file. This will preserve your folding. If you need it as a plain text file, you can post-process e.g. with w3m, which renders HTML to text and allows to dump it to a text file.
Here is a custom :RenderClosedFolds command, which will modify the current buffer / range. It also attempts to maintain the Folded highlighting of the original folds.
":[range]RenderClosedFolds
" Replace all lines currently hidden inside closed folds
" with a single line representing 'foldtext'.
function! s:RenderClosedFolds()
if line('.') == foldclosed('.')
let l:result = foldtextresult('.')
call setline('.', l:result)
execute printf('syntax match renderedFold "\V\^%s\$" containedin=ALL keepend', escape(l:result, '"\'))
else
delete _
endif
endfunction
command! -bar -range=% RenderClosedFolds
\ highlight def link renderedFold Folded |
\ let g:ingocommands_IsEntireBuffer = (<line1> == 1 && <line2> == line('$')) |
\ if g:ingocommands_IsEntireBuffer | syntax clear renderedFold | endif |
\ let g:save_foldmethod = &l:foldmethod | setlocal foldmethod=manual |
\ execute '<line1>,<line2>folddoclosed call <SID>RenderClosedFolds()' |
\ if g:ingocommands_IsEntireBuffer | setlocal nofoldenable | endif |
\ let &l:foldmethod = g:save_foldmethod | unlet g:save_foldmethod g:ingocommands_IsEntireBuffer
I have once created a script, that saves all folds to a new buffer.

Setting the cursor to a vertical thin line in vim

I am trying to set the cursor in insert mode to be a thin vertical line and I am unable to. I have tried this in my .vimrc file:
set guicursor+=i:ver100-iCursor
It does not set the cursor to a vertical bar on insert mode.
What am I missing and how do I do this?
This code in my /home/el/.vimrc worked for my console:
if $TERM_PROGRAM =~ "iTerm"
let &t_SI = "\<Esc>]50;CursorShape=1\x7" " Vertical bar in insert mode
let &t_EI = "\<Esc>]50;CursorShape=0\x7" " Block in normal mode
endif
Which does this for me:
Source:
https://hamberg.no/erlend/posts/2014-03-09-change-vim-cursor-in-iterm.html
For gnome terminal version>3.15
Add this to your ~/.vimrc.
if has("autocmd")
au VimEnter,InsertLeave * silent execute '!echo -ne "\e[2 q"' | redraw!
au InsertEnter,InsertChange *
\ if v:insertmode == 'i' |
\ silent execute '!echo -ne "\e[6 q"' | redraw! |
\ elseif v:insertmode == 'r' |
\ silent execute '!echo -ne "\e[4 q"' | redraw! |
\ endif
au VimLeave * silent execute '!echo -ne "\e[ q"' | redraw!
endif
You will get a block cursor in normal mode and a thin one in insert mode.
This did the trick:
set guicursor=i:ver25-iCursor
I had to reduce the 100 to 25
I use iTerm2 on mac and none of the above worked. Silly (vim and interface ain't right) but works. To switch between vertical bar or box. Profiles -> Open Profiles... -> Edit Profiles... -> Text

Colorschemes when entering a buffer

I have this in my .vimrc:
augroup filetype_colorscheme
au BufEnter *
\ if !exists('b:colors_name')
\ | if &ft == "vim"
\ | let b:colors_name = 'color_dark'
\ | else
\ | let b:colors_name = 'color_light'
\ | endif
\ | endif
\ | exe 'colorscheme' b:colors_name
augroup END
What it does:
When I open a .vim page it opens my dark colorscheme "color_dark.vim"
when I open whatever other page it opens my light colorscheme "color_light.vim"
This is very nice but it is not so nice in split windows.
Every time when I click in a split window with p.e. a text file, all split windows become light colored with the light colorscheme (even the .vim files).
When I switch to a vim file in a split window all other files in the other split windows become dark as well.
Is it possible to retain every filetype his own colorscheme in a split window?
What do I have to change in above code?
EDIT
If this is not possible would it be possible to disable above code when I enter in a split window? (in order to let me choose the colorscheme myself, the same for all split windows)
Colorschemes will always affect the entire vim instance. It is not possible to have a different color scheme per split window.
Edit 1: To answer your second question in the edit, you can probably add && winnr('$') == 1 to that first if to stop this from happening when you have multiple split windows open.
Edit 2: Whoops, the edit above would not work, however I think wrapping everything in another if should.
Edit 3: Missed a couple of pipes.
augroup filetype_colorscheme
au BufEnter *
\ | if winnr('$') == 1
\ if !exists('b:colors_name')
\ | if &ft == "vim"
\ | let b:colors_name = 'color_dark'
\ | else
\ | let b:colors_name = 'color_light'
\ | endif
\ | endif
\ | exe 'colorscheme' b:colors_name
\ | endif
augroup END

vim : insert mode problem : remaps (imap) and abbreviations (ab) in .vimrc don't work

I have a problem in vim:
If I modify the .vimrc file and add this lines:
map ;bb A78
it just works in normal mode.
If I got it, it should work in insert mode too, shouldn't it?
While editing, I've verified that everything was read properly (command ":map"):
i ;bb A78
If I do the same thing with "imap", I got the same problem: command ":imap" shows it's configured, but if I go in insert mode, and type ";bb" or ";bb" or ";bb" nothing is changed, I don't get the A78
What am I missing?
(And the marvellous codeSnippet plugin works only in normal mode too, which is a big problem to me)
If forgot to precise: I have only the plugin Tabularize, it's vim version 7.3 under cygwin, but I get the same problem in SSH / Linux Debian / vim version 7.0
If I try to do exactly what written here (to give another try, if it may help), that doesn't work either: "To use the abbreviation, switch to Insert mode and type th, followed by any whitespace above (space, tab, or carriage return)." doesn't work at all. This drives me nuts.
Here follows my .vimrc file, maybe there's something wrong here I didn't see:
set nocompatible
filetype plugin on
syntax enable
set ignorecase
set paste
set ruler
set modeline
set showcmd
set expandtab
set tabstop=2
set autoindent
set smartindent
set number
colorscheme desert
set vb t_vb=
set backup
set backupdir=~/.vim/backup
set directory=~/.vim/tmp
set fileencodings=utf-8,ucs-bom,default,latin1
set scrolloff=5
set undolevels=1000
nmap ;bw :. w! ~/.vimxfer<CR>
nmap ;br :r ~/.vimxfer<CR>
nmap ;ba :. w! >>~/.vimxfer<CR>
" Tell vim to remember certain things when we exit
" '10 : marks will be remembered for up to 10 previously edited files
" "100 : will save up to 100 lines for each register
" :20 : up to 20 lines of command-line history will be remembered
" % : saves and restores the buffer list
" n... : where to save the viminfo files
set viminfo='10,\"100,:20,%,n~/.viminfo
" when we reload, tell vim to restore the cursor to the saved position
augroup JumpCursorOnEdit
au!
autocmd BufReadPost *
\ if expand("<afile>:p:h") !=? $TEMP |
\ if line("'\"") > 1 && line("'\"") <= line("$") |
\ let JumpCursorOnEdit_foo = line("'\"") |
\ let b:doopenfold = 1 |
\ if (foldlevel(JumpCursorOnEdit_foo) > foldlevel(JumpCursorOnEdit_foo - 1)) |
\ let JumpCursorOnEdit_foo = JumpCursorOnEdit_foo - 1 |
\ let b:doopenfold = 2 |
\ endif |
\ exe JumpCursorOnEdit_foo |
\ endif |
\ endif
" Need to postpone using "zv" until after reading the modelines.
autocmd BufWinEnter *
\ if exists("b:doopenfold") |
\ exe "normal zv" |
\ if(b:doopenfold > 1) |
\ exe "+".1 |
\ endif |
\ unlet b:doopenfold |
\ endif
augroup END
set backspace=2
inoremap <silent> <Bar> <Bar><Esc>:call <SID>align()<CR>a
function! s:align()
let p = '^\s*|\s.*\s|\s*$'
if exists(':Tabularize') && getline('.') =~# '^\s*|' && (getline(line('.')-1) =~# p || getline(line('.')+1) =~# p)
let column = strlen(substitute(getline('.')[0:col('.')],'[^|]','','g'))
let position = strlen(matchstr(getline('.')[0:col('.')],'.*|\s*\zs.*'))
Tabularize/|/l1
normal! 0
call search(repeat('[^|]*|',column).'\s\{-\}'.repeat('.',position),'ce',line('.'))
endif
endfunction
:autocmd BufNewFile * silent! 0r ~/.vim/templates/%:e.tpl
:autocmd BufNewFile *.php call search('w', '', line("w$"))
Thanks a lot!
You need to make sure that vim is not in "paste" mode.
Try
:set nopaste
map doesn't make the mapping work in insert mode: for ALL modes, you want map!. See :help :map! for more information on this.
However, imap should work, so you're probably having issues either with timeouts or the 'paste' setting. The way a mapping works in insert mode is that it gives you a certain amount of time to enter the mapping (I think the default is 1 second) and if you type it slower than that it assumes you mean the individual characters. So if you do:
:map! ;bb A78
And then type:
;<pause>bb
(where <pause> is just a pause, not something you type)
You'll get ;bb, but if you type:
;bb
really quickly, you'll get A78.
To find out more about timeouts, have a look at these help pages:
:help 'timeout'
:help 'ttimeout'
:help 'timeoutlen'
:help 'ttimeoutlen'
The 'paste' option also has an effect: it disables mappings in insert mode and abbreviations. Try :set paste? to find out if you have this set and :set nopaste to disable it.
See:
:help 'paste'

Resources