Vim Tips Wiki
(Move categories to tip template)
No edit summary
(12 intermediate revisions by 6 users not shown)
Line 1: Line 1:
{{review}}
 
 
{{TipImported
 
{{TipImported
 
|id=613
 
|id=613
|previous=612
+
|previous=610
 
|next=614
 
|next=614
|created=December 10, 2003
+
|created=2003
 
|complexity=basic
 
|complexity=basic
 
|author=Adam Monsen
 
|author=Adam Monsen
|version=5.7
+
|version=6.0
 
|rating=9/3
 
|rating=9/3
 
|category1=
 
|category1=
 
|category2=
 
|category2=
 
}}
 
}}
The <tt>starting.txt</tt> help file ({{help|starting.txt}}) tells you how to open the last edited file in csh. In bash, do it like so:
+
The <code>starting.txt</code> help file ({{help|starting.txt}}) tells you how to open the last edited file in csh. In bash, do it like so:
   
 
<pre>
 
<pre>
Line 19: Line 18:
   
 
==Comments==
 
==Comments==
Put this in _vimrc, and invoking vim without arguments will got last open file (it will get the last file and position from .viminfo). Oh yes, replace BS by Backslash.
+
With this in [[vimrc]], invoking Vim without arguments will open the last file (it will get the last file and position from .viminfo).
   
 
<pre>
 
<pre>
" Go to last buf if invoked without args.
+
" Go to last file if invoked without arguments.
:au VimEnter * nested if
+
autocmd VimEnter * nested if
BS argc() == 0
+
\ argc() == 0 &&
BS &amp;&amp; bufname("%") == ""
+
\ bufname("%") == "" &&
BS &amp;&amp; bufname("2" + 0) != ""
+
\ bufname("2" + 0) != "" |
BS | exe("normal `0")
+
\ exe "normal! `0" |
BS | endif
+
\ endif
   
  +
" From vimrc_example.vim distributed with Vim 7.
" From http://www.zellner.org/vim/vimrc and Campbell's correction QUOTE to BACKQUOTE.
 
  +
" When editing a file, always jump to the last known cursor position.
" Go to last cursor location, ie. goto mark(double_quote)
 
  +
" Don't do it when the position is invalid or when inside an event handler
:au BufReadPost * if
 
  +
" (happens when dropping a file on gvim).
BS line("'BS"")
 
 
autocmd BufReadPost *
BS &amp;&amp; line("'BS"") &lt;= line("$")
+
\ if line("'\"") > 1 && line("'\"") <= line("$") |
BS | exe "normal `BS""
+
\ exe "normal! g`\"" |
BS | endif
+
\ endif
 
</pre>
 
</pre>
   
 
----
 
----
  +
{{Todo}}
How is this different from using your shell to do it?
 
  +
In the above, <code>bufname("2" + 0)</code> is a confused way of saying <code>bufname(2)</code>. But what has buffer 2 got to do with it?
   
  +
----
!vim
 
  +
  +
These make sure all your tabs are restored (VIM 7.x and higher).
  +
  +
<pre>
  +
" Open last active file(s) if VIM is invoked without arguments.
  +
autocmd VimLeave * nested let buffernr = bufnr("$") |
  +
\ let buflist = [] |
  +
\ while buffernr > 0 |
  +
\ if buflisted(buffernr) |
  +
\ let buflist += [ bufname(buffernr) ] |
  +
\ endif |
  +
\ let buffernr -= 1 |
  +
\ endwhile |
  +
\ if (!isdirectory($HOME . "/.vim")) |
  +
\ call mkdir($HOME . "/.vim") |
  +
\ endif |
  +
\ call writefile(reverse(buflist), $HOME . "/.vim/buflist.txt")
  +
  +
autocmd VimEnter * nested if argc() == 0 && filereadable($HOME . "/.vim/buflist.txt") |
  +
\ for line in readfile($HOME . "/.vim/buflist.txt") |
  +
\ if filereadable(line) |
  +
\ execute "tabedit " . line |
  +
\ set bufhidden=delete |
  +
\ endif |
  +
\ endfor |
  +
\ tabclose 1 |
  +
\ endif
  +
</pre>
  +
  +
:Am I missing something? Why can't you just use <code>:mksession</code>? {{help|:mksession}} --[[User:Fritzophrenic|Fritzophrenic]] 02:00, 6 April 2009 (UTC)
   
 
----
 
----
  +
  +
You are right. This works better.
  +
  +
<pre>
  +
" Go to last file(s) if invoked without arguments.
  +
autocmd VimLeave * nested if (!isdirectory($HOME . "/.vim")) |
  +
\ call mkdir($HOME . "/.vim") |
  +
\ endif |
  +
\ execute "mksession! " . $HOME . "/.vim/Session.vim"
  +
  +
autocmd VimEnter * nested if argc() == 0 && filereadable($HOME . "/.vim/Session.vim") |
  +
\ execute "source " . $HOME . "/.vim/Session.vim"
  +
</pre>

Revision as of 12:29, 1 May 2014

Tip 613 Printable Monobook Previous Next

created 2003 · complexity basic · author Adam Monsen · version 6.0


The starting.txt help file (:help starting.txt) tells you how to open the last edited file in csh. In bash, do it like so:

alias lvim='vim -c "normal '\''0"'

Comments

With this in vimrc, invoking Vim without arguments will open the last file (it will get the last file and position from .viminfo).

" Go to last file if invoked without arguments.
autocmd VimEnter * nested if
  \ argc() == 0 &&
  \ bufname("%") == "" &&
  \ bufname("2" + 0) != "" |
  \   exe "normal! `0" |
  \ endif

" From vimrc_example.vim distributed with Vim 7.
" 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
" (happens when dropping a file on gvim).
autocmd BufReadPost *
  \ if line("'\"") > 1 && line("'\"") <= line("$") |
  \   exe "normal! g`\"" |
  \ endif

 TO DO 
In the above, bufname("2" + 0) is a confused way of saying bufname(2). But what has buffer 2 got to do with it?


These make sure all your tabs are restored (VIM 7.x and higher).

" Open last active file(s) if VIM is invoked without arguments.
autocmd VimLeave * nested let buffernr = bufnr("$") |
    \ let buflist = [] |
    \ while buffernr > 0 |
    \	if buflisted(buffernr) |
    \	    let buflist += [ bufname(buffernr) ] |
    \	endif |
    \   let buffernr -= 1 |
    \ endwhile |
    \ if (!isdirectory($HOME . "/.vim")) |
    \	call mkdir($HOME . "/.vim") |
    \ endif |
    \ call writefile(reverse(buflist), $HOME . "/.vim/buflist.txt")

autocmd VimEnter * nested if argc() == 0 && filereadable($HOME . "/.vim/buflist.txt") |
    \	for line in readfile($HOME . "/.vim/buflist.txt") |
    \	    if filereadable(line) |
    \		execute "tabedit " . line |
    \		set bufhidden=delete |
    \	    endif |
    \	endfor |
    \	tabclose 1 |
    \ endif
Am I missing something? Why can't you just use :mksession? :help :mksession --Fritzophrenic 02:00, 6 April 2009 (UTC)

You are right. This works better.

" Go to last file(s) if invoked without arguments.
autocmd VimLeave * nested if (!isdirectory($HOME . "/.vim")) |
    \ call mkdir($HOME . "/.vim") |
    \ endif |
    \ execute "mksession! " . $HOME . "/.vim/Session.vim"

autocmd VimEnter * nested if argc() == 0 && filereadable($HOME . "/.vim/Session.vim") |
    \ execute "source " . $HOME . "/.vim/Session.vim"