Filetype-specific actions in .vimrc

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Filetype-specific actions in .vimrc

Steve Vance
This is a simple one. Probably. I looked in the FAQ. Especially section
26 of it.

If I put something like this at the end of my .vimrc:

if &filetype == "perl"
    set columns=100
elseif &filetype == "python"
    set columns=75
endif

it does nothing. I guess filetype is not set until later. If I "source"
my .vimrc after the file is open, it works.

The FAQ suggested something like this:

autocmd BufRead .pl,.pm setlocal columns=100

but, that doesn't work, either. Columns stay at 80. Besides, I would
rather go by "filetype", perl programs don't always end with .pl or .pm.

Thanks in advance for your helpful answer.

   stv


Reply | Threaded
Open this post in threaded view
|

Re: Filetype-specific actions in .vimrc

Aditya Mahajan
<--- On Jul 26, Steve Vance wrote --->

> This is a simple one. Probably. I looked in the FAQ. Especially section 26
> of it.
>
> If I put something like this at the end of my .vimrc:
>
> if &filetype == "perl"
>   set columns=100
> elseif &filetype == "python"
>   set columns=75
> endif
>
> it does nothing. I guess filetype is not set until later. If I "source" my
> .vimrc after the file is open, it works.
>
> The FAQ suggested something like this:
>
> autocmd BufRead .pl,.pm setlocal columns=100
>
> but, that doesn't work, either. Columns stay at 80. Besides, I would
> rather go by "filetype", perl programs don't always end with .pl or .pm.
>

try

You should have had

autocmd BufReadPre *.pl, *.pm setlocal columns=100

If you want to always detect filetype try

autocmd FileType perl setlocal columns=100

Aditya
Reply | Threaded
Open this post in threaded view
|

Re: Filetype-specific actions in .vimrc

A.J.Mechelynck
In reply to this post by Steve Vance
----- Original Message -----
From: "Steve Vance" <[hidden email]>
To: <[hidden email]>
Sent: Wednesday, July 27, 2005 12:45 AM
Subject: Filetype-specific actions in .vimrc


> This is a simple one. Probably. I looked in the FAQ. Especially section 26
> of it.
>
> If I put something like this at the end of my .vimrc:
>
> if &filetype == "perl"
>    set columns=100
> elseif &filetype == "python"
>    set columns=75
> endif
>
> it does nothing. I guess filetype is not set until later. If I "source" my
> .vimrc after the file is open, it works.
>
> The FAQ suggested something like this:
>
> autocmd BufRead .pl,.pm setlocal columns=100
>
> but, that doesn't work, either. Columns stay at 80. Besides, I would
> rather go by "filetype", perl programs don't always end with .pl or .pm.
>
> Thanks in advance for your helpful answer.
>
>   stv

------------ $HOME/.vim/after/ftplugin/perl.vim (Unix)
------------ $HOME/vimfiles/after/ftplugin/perl.vim (Windows)
set columns=100

------------ same directory, filename: python.vim
set columns=75

See ":help ftplugin-overrule", under No. 3 (This is equivalent to setting
autocommands for the FileType event.)

Note: 'columns' is a global option, it defines the width of the full gvim
windows or of Vim's console terminal. Changing it has no effect when Vim is
running in a non-resizable console terminal. ":setlocal" for a global option
actually falls back to ":set", it _does_ affect all windows and buffers.
When you split windows horizontally, they must all have the same width (the
right side of the Vim window must be straight), and to vary the widths of
vertically split windows, you can either drag the partition with the mouse,
or use some keyboard commands which I don't know by heart, though I suspect
they start with Ctrl-W.

So you cannot edit a perl file and a python file in horizontally split
windows of different widths (unless you add a vertical split in what is to
be the narrowest one to occupy the rest of the width), and for the same
reason I cannot edit a Russian file and a Chinese file in split-windows with
different fonts (since 'guifont' is also a global option).

If you want to change the size of the Vim terminal "on the fly" when going
from one split window to another, you may want to use a different approach:

    :au BufEnter * if &ft == "perl" | set co=100 | elseif &ft == "python" |
set co=75 | else | set co=80 | endif

(all on one line). Sounds wasteful to me, but YMMV.

HTH,
Tony.


Reply | Threaded
Open this post in threaded view
|

Re: Filetype-specific actions in .vimrc

Steve Vance
In reply to this post by Aditya Mahajan
Aditya Mahajan wrote:

> If you want to always detect filetype try
>
> autocmd FileType perl setlocal columns=100
>

Thanks, Aditya, this is just what I wanted. I told you it was simple.

   stv
Reply | Threaded
Open this post in threaded view
|

Re: Filetype-specific actions in .vimrc

Steve Vance
In reply to this post by Steve Vance

 > ------------ $HOME/.vim/after/ftplugin/perl.vim (Unix)
 > ------------ $HOME/vimfiles/after/ftplugin/perl.vim (Windows)
 > set columns=100
 >
 > ------------ same directory, filename: python.vim
 > set columns=75

Gee, Tony, I guess you didn't "get" my inferrence that I didn't want to
change my perl.vim file, python.vim file, etc. I wanted to do it in
.vimrc. Like it says in the subject line. And, BTW, the Russian and
Chinese part of your answer seemed a bit irrelevant. Thanks, anyway, for
trying. Aditya helped me out!

   stv

Reply | Threaded
Open this post in threaded view
|

Re: Filetype-specific actions in .vimrc

A.J.Mechelynck
----- Original Message -----
From: "Steve Vance" <[hidden email]>
To: <[hidden email]>
Sent: Wednesday, July 27, 2005 1:59 AM
Subject: Re: Filetype-specific actions in .vimrc


>
> > ------------ $HOME/.vim/after/ftplugin/perl.vim (Unix)
> > ------------ $HOME/vimfiles/after/ftplugin/perl.vim (Windows)
> > set columns=100
> >
> > ------------ same directory, filename: python.vim
> > set columns=75
>
> Gee, Tony, I guess you didn't "get" my inferrence that I didn't want to
> change my perl.vim file, python.vim file, etc. I wanted to do it in
> .vimrc. Like it says in the subject line. And, BTW, the Russian and
> Chinese part of your answer seemed a bit irrelevant. Thanks, anyway, for
> trying. Aditya helped me out!
>
>   stv

I don't _change_ your $VIMRUNTIME/ftplugin/perl.vim, .../python.vim, etc.; I
_add_ "customizations" to them under the same name in _another_ directory
(an "after-directory", see ":help after-directory"), where these
customizations will be kept even if an upgrade overwrites the
filetype-plugin from the distribution. The "Russian and Chinese" part is
about 'columns' being global, i.e., a ":setlocal" for it is somewhat
pointless. Just trying to make the case a little more vivid by means of an
example of a different case of the same phenomenon.

And BTW, if you reread my earlier post, you will notice I also mentioned the
FileType event.

Best regards,
Tony.


Reply | Threaded
Open this post in threaded view
|

Re: Filetype-specific actions in .vimrc

adah
In reply to this post by Steve Vance
The question itself is solved already. Just a minor doubt: why `columns'?
Is it possible that you really wanted `textwidth'? Try `:h textwidth'.

Best regards,

Yongwei





Steve Vance <[hidden email]>
2005-07-27 06:45

 
        To:     [hidden email]
        CC:
        Subject:        Filetype-specific actions in .vimrc

This is a simple one. Probably. I looked in the FAQ. Especially section
26 of it.

If I put something like this at the end of my .vimrc:

if &filetype == "perl"
    set columns=100
elseif &filetype == "python"
    set columns=75
endif

it does nothing. I guess filetype is not set until later. If I "source"
my .vimrc after the file is open, it works.

The FAQ suggested something like this:

autocmd BufRead .pl,.pm setlocal columns=100

but, that doesn't work, either. Columns stay at 80. Besides, I would
rather go by "filetype", perl programs don't always end with .pl or .pm.

Thanks in advance for your helpful answer.

   stv

Reply | Threaded
Open this post in threaded view
|

HHKB [off-topic]

John (Eljay) Love-Jensen
In reply to this post by A.J.Mechelynck
Hi everyone,

I am curious if anyone uses the Happy Hacking Keyboard, from PFU Limited.

I own the Happy Hacking Keyboard Lite 2, and I am very happy with it.

I'm considering purchasing Happy Hacking Keyboard Professional (charcoal
grey, with Sholes keycaps labeling) -- and I'm looking for feedback on the
quality of the keyboard and the key mechanism.

If you are not aware of Happy Hacking Keyboard, it seems to be a keyboard
custom made for Vi (and hence, Vim), by Vi aficionados.

Thanks,
--Eljay

PS:  As far as I can tell, "PFU Limited" seems to be the post-merger name of
PANAFACOM Limited, Fujitsu, and USAC Electronic Industrial.

PPS:  I do not work for PFU, nor is this intended as a PFU advert.  I'm just
a happy hacker (in the good sense of "hacker") looking for the ultimate zen
keyboard.  I also like the Tactile Pro Keyboard from Matias (I own two of
them), and the old IBM/Lexmark/Unicomp clickety-clack keyboards (I own five
of them).