Vim start with cursor where last went off - vim

How do i make Vim always start at the line I was at when i exited that given file last time?

Put this in your .vimrc:
" When editing a file, always jump to the last cursor position
au BufReadPost *
\ if ! exists("g:leave_my_cursor_position_alone") |
\ if line("'\"") > 0 && line ("'\"") <= line("$") |
\ exe "normal g'\"" |
\ endif |
\ endif
then you can use :let g:leave_my_cursor_position_alone=1 at runtime to deactivate the feature.

put this into your .vimrc
set viewoptions=cursor,folds
au BufWinLeave * mkview
au BufWinEnter * silent loadview

Related

Restoring cursor position after typing :make

I am using the following in my .vimrc to restore the cursor position after reopening the file:
" Copied from defaults.vim
" Put these in an autocmd group, so that you can revert them with:
" ":augroup vimStartup | au! | augroup END"
augroup vimStartup
au!
" When editing a file, always jump to the last known cursor position.
" Don't do it when the position is invalid, when inside an event handler
" (happens when dropping a file on gvim) and for a commit message (it's
" likely a different one than last time).
autocmd BufReadPost *
\ if line("'\"") >= 1 && line("'\"") <= line("$") && &ft !~# 'commit'
\ | exe "normal! g`\""
\ | endif
augroup END
The problem is that when I type :make, the cursor position is not restored; the cursor merely appears at the beginning of the line on which it was before typing :make.
Do you have any advise on how to fix this? Thank you for help.
The command :make doesn't seem to throw BufReadPost, so the autocommand will not execute.
You can do :make | norm! ``
:help ''

Applying augroup to more than one extension

I have the following augroup for binary files in my `.vimrc':
augroup Binary
au!
au BufReadPre *.bin let &bin=1
au BufReadPost *.bin if &bin | %!xxd
au BufReadPost *.bin set ft=xxd | endif
au BufWritePre *.bin if &bin | %!xxd -r
au BufWritePre *.bin endif
au BufWritePost *.bin if &bin | %!xxd
au BufWritePost *.bin set nomod | endif
augroup END
How can I make it work with file extensions other than .bin (without repeating all the lines quoted above)?
Auto commands take comma separated lists for the autocommand pattern
So if you wanted to add the file extension *.xxx to the list the autocommands in your group it would look like
augroup Binary
au!
au BufReadPre *.bin,*.xxx let &bin=1
au BufReadPost *.bin,*.xxx if &bin | %!xxd
au BufReadPost *.bin,*.xxx set ft=xxd | endif
au BufWritePre *.bin,*.xxx if &bin | %!xxd -r
au BufWritePre *.bin,*.xxx endif
au BufWritePost *.bin,*.xxx if &bin | %!xxd
au BufWritePost *.bin,*.xxx set nomod | endif
augroup END
Take a look at :h autocommand-pattern

Vim Error E20 'Mark not set' when running BuffWrite Command

I set up an auto run script in my vimrc to condense any block of 3 or more empty newlines down to 3 newlines. I set a mark so after the script executes, I retain my cursor position but I'm getting an E20 Mark not set error when the cursor is within an area that is being removed.
How can I fix this issue/silence the error when this happens?
" .vimrc file:
autocmd BufWrite * mark ' | silent! %s/\n\{3,}/\r\r\r/e | norm''
You could replace your marks with winsaveview() and winrestview().
autocmd BufWrite * let w:winview = winsaveview() | ... | if exists('w:winview') | call winrestview(w:winview) | endif
Also silence the normal command:
autocmd BufWrite * mark ' | silent! %s/\n\{3,}/\r\r\r/e | silent! exe "norm! ''"

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