现象:
已经在~/.vimrc中增加了下面一行:
au BufReadPost * if line("'\"") > 0|if line("'\"") <= line("$")|exe("norm '\"")|else|exe "norm $"|endif|endif
但是只有用sudo vim的时候才会记住上次编辑的位置,直接vim总是从第一行开始。
原因:
[yuanlulu@ubuntu: ~]$ll ./.viminfo
-rw------- 1 root root 4558 2011-12-09 13:58 ./.viminfo
原来记录位置的文件仅仅对root开放了读写权限。
解决办法:
对非root用户增加对~/.viminfo的读写权限
sudo chmod a+w ./.viminfo
sudo chmod a+R ./.viminfo
附上自己的vim配置文件
--------------------------------------------------------------------------
" All system-wide defaults are set in $VIMRUNTIME/debian.vim (usually just
" /usr/share/vim/vimcurrent/debian.vim) and sourced by the call to :runtime
" you can find below. If you wish to change any of those settings, you should
" do it in this file (/etc/vim/vimrc), since debian.vim will be overwritten
" everytime an upgrade of the vim packages is performed. It is recommended to
" make changes after sourcing debian.vim since it alters the value of the
" 'compatible' option.
" This line should not be removed as it ensures that various options are
" properly set to work with the Vim-related packages available in Debian.
runtime! debian.vim
" Uncomment the next line to make Vim more Vi-compatible
" NOTE: debian.vim sets 'nocompatible'. Setting 'compatible' changes numerous
" options, so any other options should be set AFTER setting 'compatible'.
"set compatible
set nocompatible
" Vim5 and later versions support syntax highlighting. Uncommenting the next
" line enables syntax highlighting by default.
if has("syntax")
syntax on
endif
" If using a dark background within the editing area and syntax highlighting
" turn on this option as well
"set background=dark
" Uncomment the following to have Vim jump to the last position when
" reopening a file
"if has("autocmd")
" au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif
"endif
" Uncomment the following to have Vim load indentation rules and plugins
" according to the detected filetype.
"if has("autocmd")
" filetype plugin indent on
"endif
" The following are commented out as they cause vim to behave a lot
" differently from regular Vi. They are highly recommended though.
set showcmd " Show (partial) command in status line.
set showmatch " Show matching brackets.
set ignorecase " Do case insensitive matching
set smartcase " Do smart case matching
set incsearch " Incremental search
"set autowrite " Automatically save before commands like :next and :make
"set hidden " Hide buffers when they are abandoned
set mouse=a " Enable mouse usage (all modes)
" Source a global configuration file if available
if filereadable("/etc/vim/vimrc.local")
source /etc/vim/vimrc.local
endif
set number
set ai
set smartindent
set cindent
set tabstop=4
set softtabstop=4
set shiftwidth=4
"突出横向和纵向的光标
set cursorline
"highlight CursorLine guibg=lightblue ctermbg=lightgray
"set cursorcolumn
filetype on
"记住上次编辑的位置
au BufReadPost * if line("'\"") > 0|if line("'\"") <= line("$")|exe("norm '\"")|else|exe "norm $"|endif|endif
"autocmd BufReadPost *
" \ if line("'\"") > 0 && line("'\"") <= line("$") |
" \ exe "normal g`\"" |
" \ endif
"高亮显示搜索匹配项
set hlsearch
set incsearch
"显示括号配对情况
set magic
"显示状态栏
"set statusline=%F%m%r%h%w/ [FORMAT=%{&ff}]/ [TYPE=%Y]/ [ASCII=/%03.3b]/ [HEX=/%02.2B]/ [POS=%04l,%04v][%p%%]/ [LEN=%L]
"set statusline=%F%m%r%h%w/ [FORMAT=%{&ff}]/ [TYPE=%Y]/ [ASCII=/%03.3b]/ [HEX=/%02.2B]/ [POS=%04l,%04v][%p%%]/ [LEN=%L]
set laststatus=2
"---------------------------------------------------------------------------------"
let g:statusline_max_path = 20
fun! StatusLineGetPath() "{{{
let p = expand('%:.:h') "relative to current path, and head path only
let p = substitute(p,'\','/','g')
let p = substitute(p, '^\V' . $HOME, '~', '')
if len(p) > g:statusline_max_path
let p = simplify(p)
let p = pathshorten(p)
endif
return p
endfunction "}}}
nmap <Plug>view:switch_status_path_length :let g:statusline_max_path = 200 - g:statusline_max_path<cr>
nmap ,t <Plug>view:switch_status_path_length
set laststatus=2
augroup Statusline
au! Statusline
au BufEnter * call <SID>SetFullStatusline()
au BufLeave,BufNew,BufRead,BufNewFile * call <SID>SetSimpleStatusline()
augroup END
fun! StatusLineRealSyn()
let synId = synID(line('.'),col('.'),1)
let realSynId = synIDtrans(synId)
if synId == realSynId
return 'Normal'
else
return synIDattr( realSynId, 'name' )
endif
endfunction
fun! s:SetFullStatusline() "{{{
setlocal statusline=
setlocal statusline+=%#StatuslineBufNr#%-1.2n\ " buffer number
setlocal statusline+=%h%#StatuslineFlag#%m%r%w " flags
setlocal statusline+=%#StatuslinePath#\ %-0.20{StatusLineGetPath()}%0* " path
setlocal statusline+=%#StatuslineFileName#\/%t\ " file name
setlocal statusline+=%#StatuslineFileEnc#\ %{&fileencoding}\ " file encoding
setlocal statusline+=%#StatuslineFileType#\ %{strlen(&ft)?&ft:'**'}\ . " filetype
setlocal statusline+=%#StatuslineFileType#%{&fileformat}\ " file format
setlocal statusline+=%#StatuslineTermEnc#\ %{&termencoding}\ " encoding
setlocal statusline+=%#StatuslineChar#\ %-2B\ %0* " current char
setlocal statusline+=%#StatuslineSyn#\ %{synIDattr(synID(line('.'),col('.'),1),'name')}\ %0* "syntax name
setlocal statusline+=%#StatuslineRealSyn#\ %{StatusLineRealSyn()}\ %0* "real syntax name
setlocal statusline+=%=
setlocal statusline+=\ %-10.(%l,%c-%v%) "position
setlocal statusline+=%P "position percentage
setlocal statusline+=\ %#StatuslineTime#%{strftime(\"%m-%d\ %H:%M\")} " current time
endfunction "}}}
fun! s:SetSimpleStatusline() "{{{
setlocal statusline=
setlocal statusline+=%#StatuslineNC#%-0.20{StatusLineGetPath()}%0* " path
setlocal statusline+=\/%t\ E`Q
本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。