Loading ...
Sorry, an error occurred while loading the content.

Re: Conditional autocmd question

Expand Messages
  • Gary Johnson
    ... Example: if filereadable( Makefile ) let &makeprg = make else let &makeprg = gcc -o foo foo.c endif See also: help let-option help make help makeprg
    Message 1 of 3 , Jan 31, 2007
    • 0 Attachment
      On 2007-01-29, Evan Klitzke <eklitzke.lists@...> wrote:
      > Hi everyone,
      >
      > I have the F2 key mapped as a "compile" function for the programs I
      > write, e.g. if I am editing a .tex file I have an autocmd to execute
      > pdflatex, if I am editing a .py file I have an autocmd to execute
      > python. The case of .c files is somewhat trickier: I want to use autocmd
      > to map F2 to :make if a Makefile exists in the current directory,
      > otherwise it should try to compile the file with gcc. For example, if I
      > am editing foo.c, hitting F2 should execute "gcc -o foo foo.c". Do you
      > have any recommendations on how to do this?

      See:

      :help filereadable()

      Example:

      if filereadable("Makefile")
      let &makeprg = "make"
      else
      let &makeprg = "gcc -o foo foo.c"
      endif

      See also:

      help let-option
      help make
      help makeprg
      help 30.1

      > Particularly I am confused on how to have a conditional autocmd,
      > and how to extract a substring from the current buffer/file name.

      See:

      :help <afile>
      :help fnamemodify()
      :help expand()
      :help filename-modifiers
      :help extension-removal

      Example 1:

      let basename = expand("<afile>:t:r")

      Example 2:

      :set makeprg=make\ -o\ %<\ %

      HTH,
      Gary

      --
      Gary Johnson | Agilent Technologies
      garyjohn@... | Wireless Division
      | Spokane, Washington, USA
    Your message has been successfully submitted and would be delivered to recipients shortly.