5

I found it strange that I can never have Vim show line number automatically for .py files, even if I have this as my ~/.vimrc

set background=dark
autocmd FileType py,c set number

Strangely enough, when I run vim xxx.c (regardless of whether xxx.c exists), it enables line number automatically, but never when I run vim xxx.py

I also tried some random extensions, but only c works. Like this:

autocmd FileType py,c,xxx,yyy,abcd,wxyz set number

And when I open a file with the extension written above, only .c file has line number automatically enabled. I tried re-ordering those extensions, but it made no difference.


In case they matter, here are my related settings. I've never done any modification to Vim except creating a .vimrc file under my home directory, after installing Vim using sudo apt install vim.

Output when I run vim --version on my Windows Subsystem for Linux:

VIM - Vi IMproved 7.4 (2013 Aug 10, compiled Nov 24 2016 16:44:48)
Included patches: 1-1689
Extra patches: 8.0.0056
Modified by [email protected]
Compiled by [email protected]
Huge version without GUI.  Features included (+) or not (-):
+acl             +farsi           +mouse_netterm   +tag_binary
+arabic          +file_in_path    +mouse_sgr       +tag_old_static
+autocmd         +find_in_path    -mouse_sysmouse  -tag_any_white
-balloon_eval    +float           +mouse_urxvt     -tcl
-browse          +folding         +mouse_xterm     +terminfo
++builtin_terms  -footer          +multi_byte      +termresponse
+byte_offset     +fork()          +multi_lang      +textobjects
+channel         +gettext         -mzscheme        +timers
+cindent         -hangul_input    +netbeans_intg   +title
-clientserver    +iconv           +packages        -toolbar
-clipboard       +insert_expand   +path_extra      +user_commands
+cmdline_compl   +job             -perl            +vertsplit
+cmdline_hist    +jumplist        +persistent_undo +virtualedit
+cmdline_info    +keymap          +postscript      +visual
+comments        +langmap         +printer         +visualextra
+conceal         +libcall         +profile         +viminfo
+cryptv          +linebreak       -python          +vreplace
+cscope          +lispindent      +python3         +wildignore
+cursorbind      +listcmds        +quickfix        +wildmenu
+cursorshape     +localmap        +reltime         +windows
+dialog_con      -lua             +rightleft       +writebackup
+diff            +menu            -ruby            -X11
+digraphs        +mksession       +scrollbind      -xfontset
-dnd             +modify_fname    +signs           -xim
-ebcdic          +mouse           +smartindent     -xsmp
+emacs_tags      -mouseshape      +startuptime     -xterm_clipboard
+eval            +mouse_dec       +statusline      -xterm_save
+ex_extra        +mouse_gpm       -sun_workshop    -xpm
+extra_search    -mouse_jsbterm   +syntax
   system vimrc file: "$VIM/vimrc"
     user vimrc file: "$HOME/.vimrc"
 2nd user vimrc file: "~/.vim/vimrc"
      user exrc file: "$HOME/.exrc"
  fall-back for $VIM: "/usr/share/vim"
Compilation: gcc -c -I. -Iproto -DHAVE_CONFIG_H   -Wdate-time  -g -O2 -fPIE -fstack-protector-strong -Wformat -Werror=format-security -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1
Linking: gcc   -Wl,-Bsymbolic-functions -fPIE -pie -Wl,-z,relro -Wl,-z,now -Wl,--as-needed -o vim        -lm -ltinfo -lnsl  -lselinux  -lacl -lattr -lgpm -ldl     -L/usr/lib/python3.5/config-3.5m-x86_64-linux-gnu -lpython3.5m -lpthread -ldl -lutil -lm

And the output for uname -a

Linux iBug-PC 4.4.0-43-Microsoft #1-Microsoft Wed Dec 31 14:42:53 PST 2014 x86_64 x86_64 x86_64 GNU/Linux

Extra information:

Non-existent environment variable: $VIM
Non-modified file: /usr/share/vim/vimrc
Non-existent directory: ~/.vim/
Non-existent file: ~/.exrc

Kindly tell me how to make Vim show line number for Python scripts.

Note: In this post, "Python file" means exactly any file with extension .py.

6
  • what is the output of :verbose set nu? in a python file? Commented Jan 27, 2018 at 19:56
  • FileType takes the name of the filetype (python) not an extension. Replace py with python; does that work?
    – D. Ben Knoble
    Commented Jan 27, 2018 at 23:41
  • @DavidBenKnoble That works. How can I apply autocmd to an arbitrary file extension?
    – iBug
    Commented Jan 28, 2018 at 1:45
  • Will provide detailed answer in the morning.
    – D. Ben Knoble
    Commented Jan 28, 2018 at 5:15
  • @iBug posted. Hope that helps.
    – D. Ben Knoble
    Commented Jan 28, 2018 at 12:32

1 Answer 1

8

Why it didn't work

From :help FileType,

                                      *FileType*
FileType        When the 'filetype' option has been set.  The
                pattern is matched against the filetype.
                <afile> can be used for the name of the file
                where this option was set, and <amatch> for
                the new value of 'filetype'.  Navigating to
                another window or buffer is not allowed.
                See |filetypes|.

The key bit there is

The pattern is matched against the filetype

meaning that any autocmd using FileType should be followed by an actual filetype.

The "solution"

autocmd FileType python,c set number

HOWEVER read on...

Frame Challenge : Using Vim Properly

autocmd is NOT the solution to filetype-dependent settings. It might seem like a good candidate at first, but Vim was built with a philosophy of placing files in a defined directory structure to affect behavior. Most vim users learn about vimrc first. But there are more (see :help rtp).

What this means for the OP is as follows: Python (and C) filetype-dependent settings, mappings, &c. should be placed in

~/.vim/ftplugin/$FILETYPE.vim

E.g., create ~/.vim/ftplugin/python.vim with the contents

setlocal number

to have Python files get number on. Note I used setlocal to make it buffer specific.

If you find that something in these files conflicts with other settings, you should place the offending line in ~/.vim/after/ftplugin/python.vim to have it override any other settings.

Finally, make sure you at least enable filetype plugins with filetype plugin on.


In response to comments,

Using file extensions

To perform an autocmd on an arbitrary file extension, you can use any of the various autocmds that match on the file name, and then use a glob (*.ext) as the pattern.

The two most common I see are BufNewFile and BufRead (or BufReadPost).

In practice, this is all of them (excepting a few special cases like FileType).

See :help autocmd-events for more.

3
  • note that with filetype on, vim will automatically set the filetype properly for python files.
    – D. Ben Knoble
    Commented Jan 28, 2018 at 12:31
  • Where do I find a list of know FileTypes to my installation of Vim?
    – iBug
    Commented Jan 28, 2018 at 12:32
  • check :help :filetype for some info, basically you can go digging or when you edit a file whose filetype isn't correctly detected (rare, in my exp) you create your own detecter.
    – D. Ben Knoble
    Commented Jan 28, 2018 at 12:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.