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

Conditional autocmd question

Expand Messages
  • Evan Klitzke
    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,
    Message 1 of 3 , Jan 29, 2007
      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? Particularly I am confused
      on how to have a conditional autocmd, and how to extract a substring
      from the current buffer/file name.

      I have also thought about modifying my existing auto commands to execute
      only if the current buffer is unmodified (i.e. has been saved since the
      last edit). How would I add such a check to my auto commands?

      Thanks in advance!

      -- Evan Klitzke
    • A.J.Mechelynck
      ... autocmd EventName * if ! &mod | DoSomething | endif see ... and possibly ... Best regards, Tony.
      Message 2 of 3 , Jan 30, 2007
        Evan Klitzke 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? Particularly I am confused
        > on how to have a conditional autocmd, and how to extract a substring
        > from the current buffer/file name.
        >
        > I have also thought about modifying my existing auto commands to execute
        > only if the current buffer is unmodified (i.e. has been saved since the
        > last edit). How would I add such a check to my auto commands?
        >
        > Thanks in advance!
        >
        > -- Evan Klitzke
        >
        >

        autocmd EventName * if ! &mod | DoSomething | endif


        see
        :help expr-option
        :help :bar
        and possibly
        :help line-continuation


        Best regards,
        Tony.
      • 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 3 of 3 , Jan 31, 2007
          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.