0

I want to use pygmentize to highlight some script files (python/bash/...) without extension. But pygmentize requires me to specify the lexer using -l. It does not automatically identify the file type from the content.

I have the following options at hand, but none of them work now:

  1. use file -b --mime-type. But this command output x-python and x-shellscript instead of python and bash and I don't know the rules
  2. use vim -e -c 'echo &ft|q' the_file. For any file with or without file extension, vim has a mechanism to guess the file type. But it doesn't work. Since the output goes to the vim window and disappears after q.

What can I do?


@Samborski's method works fine in normal case but it does not work in python subprocess.check_output since the pts is not allocated. If you use nvim, you can use this more straightforward way:

HOME=_ nvim --headless -es file <<EOF
call writefile([&ft], "/dev/stdout")
EOF
doraemon
  • 2,296
  • 1
  • 17
  • 36
  • 1
    Python files, bash files, etc. are all just text files intended to be opened with different interpreters. You could guess at the correct interpreter to use by analyzing the file and looking for certain structures and keywords, but it would be a guess at best. – TigerhawkT3 Jan 15 '19 at 04:56
  • Of course it should be determined by the content of the file. My question is how. `vim` has a lot of code to handle this. `file` can also do this but it output is not something directly usable by pygmentize – doraemon Jan 15 '19 at 05:55

1 Answers1

2

You can use vim this way:

vim -c ':silent execute ":!echo " . &ft . " > /dev/stdout"' -c ':q!' the_file

It simply constructs command to run in the shell as a string concatenation.

Karol Samborski
  • 2,757
  • 1
  • 11
  • 18
  • This one works, but it causes screen flash. Is there a way to prevent that? – doraemon Jan 15 '19 at 09:39
  • I found that the flash is caused by my plugins in `.vim`. What how can I disable those plugins – doraemon Jan 15 '19 at 09:47
  • you can create another `.vimrc` file and the use it instead of the default one by specifying `-u ` flag – Karol Samborski Jan 15 '19 at 09:51
  • It is `.vim` not the `.vimrc`. Using that I will need to modify the `rtp` in the vimrc. I just found another better way, it is to use `HOME=_ vim -c ...` to fake a home dir so that vim will not be able to find my own settings. Do you mind to add this part to you answer? I will accept yours. Thank you – doraemon Jan 15 '19 at 09:54
  • There is also `--noplugin` flag available – Karol Samborski Jan 15 '19 at 10:04
  • `--noplugin` will not load any plugin so that file detection will fail. I need to keep the standard plugins shipped with vim but exclude my personal ones in `.vim` – doraemon Jan 16 '19 at 02:01
  • `vim -u NORC` (`:help -u`). – Amadan Jan 16 '19 at 02:15
  • @Amadan, `-u NORC` only disables `.vimrc`, The plugins in `.vim/plugin` and other folders in it will still run – doraemon Jan 16 '19 at 04:38
  • `.vim/plugin` is not a location Vim looks at. It is the package manager that you run from your `.vimrc`(Vundle, NeoBundle, Pathogen, VimPlug...) that makes Vim aware of `.vim/plugin`. No `.vimrc` = no custom plugins. However. you _do_ want Vim's default plugins to run because you want `filetype.vim`, the plugin responsible for detecting the file types. – Amadan Jan 16 '19 at 04:42