syntax coloring

92 views
Skip to first unread message

rameo

unread,
Apr 15, 2011, 3:06:33 AM4/15/11
to vim_use
When I start VIM it shows my tabs and reloads my buffers from the last
time.

I use a session to do this.

au VimEnter * exe "so d:\\Session.vim"
au VimLeave * exe "'mksession! d:\\Session.vim'"

My reopened files do not have syntax coloring.
I have to do ":e" in every file where I need syntax coloring to view
syntax coloring.

I know that I can add "localoptions" in sessionoptions but this slows
down my vim (I noted that it uses 20-25% more CPU). Without
"localoptions" my vim is much faster.

How do you resolve this?
Don't you use sessions?

Tony Mechelynck

unread,
Apr 15, 2011, 5:17:04 AM4/15/11
to vim...@googlegroups.com, rameo

try adding either

syntax on

or (near the top)

runtime vimrc_example.vim

(it's not necessary to use both) to your vimrc (or make one if you don't
yet have one, using

:e ~/_vimrc

inside Vim).


Best regards,
Tony.
--
Dimensions will always be expressed in the least usable term.
Velocity, for example, will be expressed in furlongs per fortnight.

rameo

unread,
Apr 15, 2011, 5:31:44 AM4/15/11
to vim_use

On Apr 15, 11:17 am, Tony Mechelynck <antoine.mechely...@gmail.com>
wrote:
Hello Tony,

I already had "syntax on" near the top of my _vimrc file.

...
I noted that vim doesn't detect filetypes at startup when files are
restored (by sourcing a session).

I have
filetype on
filetype plugin on
written in my _vimrc

Best regards,
Rameo

Tony Mechelynck

unread,
Apr 15, 2011, 6:25:31 AM4/15/11
to vim...@googlegroups.com, rameo

Whell, how exactly do you restore your sessions? When I used "gvim -S"
with :mksession it did read my vimrc. (Now I use a handtyped Session.vim
which does exactly what I want and not all the :mksession hocus-pocus.)

Best regards,
Tony.
--
hundred-and-one symptoms of being an internet addict:
58. You turn on your computer and turn off your wife.

rameo

unread,
Apr 15, 2011, 6:47:08 AM4/15/11
to vim_use
I restore my sessions using VimEnter/VimLeave --> see 1st message.

I just checked what it does when I do it manually..
Syntax works. (maybe because it uses the sessionoptions "localoptions"
or "options")


Tony Mechelynck

unread,
Apr 15, 2011, 7:26:02 AM4/15/11
to vim...@googlegroups.com, rameo

If you're using gvim, try making a desktop icon which would call it as

gvim -S d:\Session.vim

If you're using Console Vim, try making... well, I was about to say a
shell alias, but maybe these don't exist on Windows. You can make a bat
script, however, named maybe svim.bat, which would call Vim as

vim -S d:\Session.vim

In all cases, you may need to call vim, svim or gvim with a full path if
its executable's directory is not in your PATH environment variable.

see :help -S


Best regards,
Tony.
--
"When are you BUTTHEADS gonna learn that you can't oppose Gestapo
tactics *with* Gestapo tactics?"
-- Reuben Flagg

ZyX

unread,
Apr 15, 2011, 7:43:29 AM4/15/11
to vim...@googlegroups.com
Reply to message «syntax coloring»,
sent 11:06:33 15 April 2011, Friday
by rameo:

> When I start VIM it shows my tabs and reloads my buffers from the last
> time.
>
> I use a session to do this.
>
> au VimEnter * exe "so d:\\Session.vim"
> au VimLeave * exe "'mksession! d:\\Session.vim'"

You have made just the same mistake as me some time ago: all filetype-related
files are loaded using an autocommand, but by default no autocommands are
allowed when you are executing an autocommand, so you should add `nested' just
after the pattern. By the way, why do you use `exe'? The following works as
well:
augroup SaveRestoreSessions
autocmd!
autocmd VimEnter * nested source D:\session.vim
autocmd VimLeave * nested mksession! D:\session.vim
augroup END

See `:h autocmd-nested' for more details.

Original message:

signature.asc

rameo

unread,
Apr 15, 2011, 11:13:18 AM4/15/11
to vim_use
>  signature.asc
> < 1KViewDownload

Great! It does work.
Thanks a lot!

Please let me ask you one more question...
How do you close this session and reopens a custom one (happens once
in a while)?

ZyX

unread,
Apr 15, 2011, 11:43:48 AM4/15/11
to vim...@googlegroups.com
Reply to message «Re: syntax coloring»,
sent 19:13:18 15 April 2011, Friday
by rameo:

> Please let me ask you one more question...


> How do you close this session and reopens a custom one (happens once
> in a while)?

I don't use one continious vim session and don't load any sessions at
vim startup (the `just the same error' meant that I used an autocommand
in a similar way and got the same error, not that I used the same code).
Instead I create a new session file almost each time when vim exits
and load one of created sessions at vim startup:
1. due to virtual desktops I almost never close any of my projects' windows,
2. I use vim to quickly edit something from command-line, loading any sessions
will discard the most significant part of vim invocation arguments
for this use-case.
Saving session at vim exit is for the case when I have to reload vim
(mostly plugin or kernel updates).
Here is my code:

"▶2 mksession
if has('mksession')
function s:InitSes()
let sesdir=expand('~/.vimsessions')
if !isdirectory(sesdir)
call mkdir(expand(sesdir), "", 0700)
endif
let sesfile=os#JoinPath(sesdir, '.sessions.lst')
let sessions=[]
if filereadable(sesfile)
let sessions+=readfile(sesfile, 'b')
while !empty(sessions) && empty(sessions[-1])
call remove(sessions, -1)
endwhile
endif
let sessplitted=map(copy(sessions),
\'split(v:val, ''\v%(\\@<!%(\\\\)*)@<=,'')')
let sesdirs=map(copy(sessplitted), '((empty(v:val))?(""):(v:val[0]))')
let sesfiles=map(copy(sessplitted), '((empty(v:val))?([]):(v:val[1:]))')
execute "cd ".fnameescape(resolve(fnamemodify('.', ':p')))
let curdir=escape(fnamemodify('.', ':p'), ',\')
return [sesdir, sesfile, sessions, sesdirs, sesfiles, curdir]
endfunction
let s:_functions['s:InitSes']=function('s:InitSes')
function s:MakeSession()
let [sesdir, sesfile, sessions, sesdirs, sesfiles, curdir]=s:InitSes()
let files=sort(map(filter(map(filter(range(1, bufnr('$')),
\ 'getbufvar(v:val, "&bt")!=#"help"'),
\ 'bufname(v:val)'),
\ 'filereadable(v:val)'),
\ 'escape(fnamemodify(resolve(fnamemodify(v:val, ":p")),
":~:."), ",\\")'))
if empty(files)
return
endif
let sesstr=join([curdir]+files, ',')
let sindex=index(sessions, sesstr)
if sindex==-1
let sindex=index(sessions, '')
endif
if sindex==-1
let sesname=os#JoinPath(sesdir, len(sesdirs).'.vim')
call add(sessions, sesstr)
else
let sesname=os#JoinPath(sesdir, sindex.'.vim')
let sessions[sindex]=sesstr
endif
execute 'mksession! '.fnameescape(sesname)
call writefile(sessions, sesfile, 'b')
endfunction
let s:_functions['s:MakeSession']=function('s:MakeSession')
function s:LoadSession(bang, ...)
let [sesdir, sesfile, sessions, sesdirs, sesfiles, curdir]=s:InitSes()
if a:0 && string(+a:1)==#a:1 && a:1<len(sessions) &&
\!empty(sessions[a:1])
let dirsessions=[[+a:1, sesdirs[a:1], sessions[a:1]]]
else
let dirsessions=filter(map(copy(sesdirs),
\ '[v:key, v:val, sessions[v:key]]'),
\ 'v:val[1]==#curdir'.
\ ((a:0)?(' && v:val[2]=~?a:1'):('')))
endif
if len(dirsessions)==1 || (!empty(dirsessions) && a:bang)
let sesid=dirsessions[0][0]
let sesname=os#JoinPath(sesdir, sesid.'.vim')
execute 'source '.fnameescape(sesname)
call delete(sesname)
if sesid==(len(sessions)-1)
call remove(sessions, -1)
else
let sessions[sesid]=""
endif
call writefile(sessions, sesfile, 'b')
endif
endfunction
let s:_functions['s:LoadSession']=function('s:LoadSession')
function s:PrintSessions()
let [sesdir, sesfile, sessions, sesdirs, sesfiles, curdir]=s:InitSes()
call map(sessions,
\'printf("%'.len(len(sessions)-1).'u %s", v:key, v:val)')
echo join(sessions, "\n")
endfunction
let s:_functions['s:PrintSessions']=function('s:PrintSessions')
autocmd VimLeavePre * call s:MakeSession()
command -nargs=? -bang LoadSession call s:LoadSession(<bang>0, <q-args>)
command -bar PrintSessions call s:PrintSessions()
let s:_commands+=['LoadSession', 'PrintSessions']
endif
"▲2

I am going to evolve this into plugin sometime.

Original message:

signature.asc

rameo

unread,
Apr 15, 2011, 1:39:21 PM4/15/11
to vim_use
>  signature.asc
> < 1KViewDownload


Still have a little problem.
I had also a VimLeave argdel command in my _vimrc.

Is this correct?:

augroup SaveRestoreSessions
autocmd!
autocmd VimEnter * nested source D:\session.vim
autocmd VimLeave * nested mksession! D:\session.vim
autocmd VimLeave * if argc() != 0 | exe 'argdel *'
augroup END

btw tnx. for your script.

ZyX

unread,
Apr 15, 2011, 2:55:09 PM4/15/11
to vim...@googlegroups.com
Reply to message «Re: syntax coloring»,
sent 21:39:21 15 April 2011, Friday
by rameo:

> Still have a little problem.


> I had also a VimLeave argdel command in my _vimrc.
>
> Is this correct?:
>
> augroup SaveRestoreSessions
> autocmd!
> autocmd VimEnter * nested source D:\session.vim
> autocmd VimLeave * nested mksession! D:\session.vim
> autocmd VimLeave * if argc() != 0 | exe 'argdel *'
> augroup END
>
> btw tnx. for your script.

1. Missing endif.
2. You don't need `exe' here.
3. I don't get why do you need this command at all.

signature.asc

rameo

unread,
Apr 15, 2011, 3:02:40 PM4/15/11
to vim_use
>  signature.asc
> < 1KViewDownload

Tnx.. this is what I added:
autocmd VimLeave * if argc() != 0 | 'argdel *' endif


I noted that (even after a buffer delete :bd) certain buffers do
remain in "session.vim"
I had no troubles anymore after removing "argdel *" and troubles again
after having changed the script as above.

ZyX

unread,
Apr 15, 2011, 3:14:32 PM4/15/11
to vim...@googlegroups.com
Reply to message «Re: syntax coloring»,
sent 23:02:40 15 April 2011, Friday
by rameo:

> Tnx.. this is what I added:


> autocmd VimLeave * if argc() != 0 | 'argdel *' endif

Reread help. This is false: you should not use strikes around `argdel *' and you
should have bar before `endif' just like you have before `argdel'.

> I noted that (even after a buffer delete :bd) certain buffers do
> remain in "session.vim"
> I had no troubles anymore after removing "argdel *" and troubles again
> after having changed the script as above.

I know this issue. In this case you should have `argdel' before mksession,
I don't get why you have it *after*.

signature.asc

rameo

unread,
Apr 15, 2011, 3:25:10 PM4/15/11
to vim_use
> ...
>
> read more »
>
>  signature.asc
> < 1KViewDownload

I don't know why I had it after mksession.
...oh...maybe I know... not enouth knowledge of vim :)

I found a new way to integrate argdel:

augroup SaveRestoreSessions
autocmd!
autocmd VimEnter * nested source $VIM\vimfiles\sessions\Session.vim
autocmd VimLeave * call MakeSession()
autocmd VimLeave * nested mksession! $VIM\vimfiles\sessions
\Session.vim
augroup END

function! MakeSession()
if argc() != 0
execute 'argdel *'
endif
endfunction

Karl

unread,
Apr 15, 2011, 3:29:17 PM4/15/11
to vim...@googlegroups.com
Unsubscribe
/ktb

rameo

unread,
Apr 15, 2011, 3:29:29 PM4/15/11
to vim_use
> ...
>
> read more »
>
>  signature.asc
> < 1KViewDownload

(sorry for my previous empty message).


I don't know why I had it after mksession.
...oh...maybe I know... not enouth knowledge of vim :)

I found a new way to integrate argdel:

augroup SaveRestoreSessions
autocmd!

rameo

unread,
Apr 15, 2011, 3:33:40 PM4/15/11
to vim_use
>  signature.asc
> < 1KViewDownload

(I don't know what happens but messages are shown empty (even aften
writing text inside).)

----------------------------------------------

I don't know why I had argdel * after mksession.
...oh...maybe I know... not enouth knowledge of vim :)

I found a new way to integrate argdel:

augroup SaveRestoreSessions
autocmd!

Tim Gray

unread,
Apr 15, 2011, 3:36:59 PM4/15/11
to Karl, vim...@googlegroups.com
On Apr 15, 2011 at 07:29 PM +0000, Karl wrote:
>Unsubscribe

Why not try sending an email to:
<mailto:vim_use+u...@googlegroups.com>

ZyX

unread,
Apr 15, 2011, 4:02:02 PM4/15/11
to vim...@googlegroups.com
Reply to message «Re: syntax coloring»,
sent 23:29:29 15 April 2011, Friday
by rameo:

> I found a new way to integrate argdel:


>
> augroup SaveRestoreSessions
> autocmd!
> autocmd VimEnter * nested source $VIM\vimfiles\sessions\Session.vim
> autocmd VimLeave * call MakeSession()
> autocmd VimLeave * nested mksession! $VIM\vimfiles\sessions
> \Session.vim
> augroup END
>
> function! MakeSession()
> if argc() != 0
> execute 'argdel *'
> endif
> endfunction

You are doing a strange things again:
1. You should either rename `MakeSession' to `ClearArgs' (because `MakeSession'
will confuse somebody (maybe even you) who will read you code as it does not
makes any sessions) or, better, move `mksession!' call inside a function.
2. Why are you again writing `execute'?
3. You don't need `nested' for mksession, you need it only for VimEnter.
4. Never write anything user-specific to application folder, it may be purged by
application update, removed by the package manager (if any) or uninstaller, or
you may have to create more then one user on the machine.

signature.asc

rameo

unread,
Apr 15, 2011, 4:32:32 PM4/15/11
to vim_use
> ...
>
> read more »
>
>  signature.asc
> < 1KViewDownload

Is this the correct one?

augroup SaveRestoreSessions
autocmd!
autocmd VimEnter * nested source D:\Session.vim
autocmd VimLeave * call ClearArgs()
autocmd VimLeave * nested mksession! D:\Session.vim
augroup END

function! ClearArgs()
if argc() != 0
argdel *
endif
endfunction

-----------------

Regards,
R.

ZyX

unread,
Apr 15, 2011, 4:42:34 PM4/15/11
to vim...@googlegroups.com
Reply to message «Re: syntax coloring»,
sent 00:32:32 16 April 2011, Saturday
by rameo:

> Is this the correct one?
>
> augroup SaveRestoreSessions
> autocmd!
> autocmd VimEnter * nested source D:\Session.vim
> autocmd VimLeave * call ClearArgs()
> autocmd VimLeave * nested mksession! D:\Session.vim
> augroup END
>
> function! ClearArgs()
> if argc() != 0
> argdel *
> endif
> endfunction

Previous variant was also correct (working), but strange. It is better, but why
don't you use this instead:

let sesfile='D:\Session.vim'
augroup SaveRestoreSessions
autocmd!
autocmd VimEnter * nested :source `=sesfile`
autocmd VimLeave * :if argc() | argdel * | endif | mksession! `=sesfile`
augroup END

signature.asc

rameo

unread,
Apr 16, 2011, 4:13:11 AM4/16/11
to vim_use
> ...
>
> read more »
>
>  signature.asc
> < 1KViewDownload


Thank you..
Nice to put the session file path in a variable.
(The only drawback is that it seems not to be enable to expand
variables as $HOME and $VIM)

ZyX

unread,
Apr 16, 2011, 9:38:57 AM4/16/11
to vim...@googlegroups.com
Reply to message «Re: syntax coloring»,
sent 12:13:11 16 April 2011, Saturday
by rameo:

> Thank you..


> Nice to put the session file path in a variable.
> (The only drawback is that it seems not to be enable to expand
> variables as $HOME and $VIM)

Just use string concatenation (:h expr-.): `$HOME.'\vimsessions\main.vim''
instead of `'$HOME\vimsessions\main.vim''. And don't use $VIM, if your system is
configured properly then you do not have write access to it at all.

signature.asc

Patrick Texier

unread,
Apr 16, 2011, 12:42:57 PM4/16/11
to vim...@googlegroups.com
Le Sat, 16 Apr 2011 17:38:57 +0400, ZyX a �crit dans le message
<201104161738.58234@-zyx>�:

> > > > > > > > > > >

Eleven and 306 lines for three usefull :-(

Please cut unusefull citations in this thread.
--
Patrick Texier

tux.

unread,
Apr 16, 2011, 1:26:53 PM4/16/11
to vim...@googlegroups.com
Patrick Texier schrob am 16.04.2011 18:42:

> Eleven and 306 lines for three usefull :-(

Heh, with Thunderbird's Quote Colors add-on it looks like really good
art at least :)

Reply all
Reply to author
Forward
0 new messages