开发者

How do I show the time on the statusbar after I do a write in vim?

Whenever I do a write in vim on the bottom of the screen it gives some information about the write, like the file name and the amount written. I would also like to include the time of the write in the information. I frequently find myself editing a file and then forgetting if I've done a write or not. I also would like the time there to compare when the last time I wrote a particular file compared to开发者_开发百科 other files I'm working on. Does anyone have any suggestions on how to accomplish this?

Thanks!


Have a look at the 'statusline' option. This allows you to completely customise what appears on the status line.

:help 'statusline'

You can either make a function that creates the whole status line or you can use %{} to make part of it be the result of a function line.

%{strftime('%c',getftime(expand('%')))}

should give the 'modification time' of the current file.

It would be better to stick this in a function as you can check that the current file exists prior to getting the time (which would be more robust). I'll leave it as an exercise for you to read the help for statusline and decide what else you want in there. Try a few things out and see how you get on!

 :help function-list
 :help strftime()
 :help getftime()
 :help expand()
 :help :function


So heres what I ended up with thanks mainly to Al's direction:

:hi User1 term=NONE cterm=NONE ctermfg=Magenta ctermbg=Black
:set laststatus=2 
:set statusline=%1*%F%h%m%w\ [Time:\ %{strftime(\"%H:%M\")}]\ [Mod\ Time:\ %{strftime(\"%H:%M:%S\",getftime(expand(\"\%\%\")))}]%=\ [%p%%]\ [%l/%L]

Description:

:hi User1 term=NONE cterm=NONE ctermfg=Magenta ctermbg=Black

This command sets the background color of a profile called user1 equal to Black and the word color equal to Magenta. What we do is make the status line equal to this profile, so that we can change the color of our status bar.

:set laststatus=2 

This command makes the status bar visible constantly by putting it two lines about the bottom of the vim command.

:set statusline=%1*%F%h%m\ [Time:\ %{strftime(\"%H:%M\")}]\ [Mod\ Time:\ %{strftime(\"%H:%M:%S\",getftime(expand(\"\%\%\")))}]%=\ [%p%%]\ [%l/%L]

This is the command that actually shows what is going to be in the status bar. In this command we use several functions (eg. strftime,expand...) that are vim specific. We also use the %(letter) to denote specific vim variables. The rest of the text just prints out what you write to the screen with the \ and then a space after to represent a space. Now to describe the specific variables.

  • %1* is the var the corresponds to the color that we did in the first command, this sets the status bar equal to those colors
  • %F%h%m says display the full file name, the help file flag, and the modified flag
  • [Time:\ %{strftime(\"%H:%M\")}]\ says display [Time: (current time)] and display it at hours and minutes. If you want a different time format like maybe to include the year or something do a :help strftime.
  • [Mod\ Time:\ %{strftime(\"%H:%M:%S\",getftime(expand(\"\%\%\")))}] says display [Mod Time: (mod time)]. Its in hours:minutes:seconds.
  • %=\ [%p%%]\ [%l/%L] says right align and show the percent of the file you've been through and [(current line num)/(max line num)].
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜