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

RE: Running the current file

Expand Messages
  • HALIM,SALMAN (HP-NewJersey,ex2)
    ... This functionality is nice, but I don t always like it (such as when I m editing source code or some such) so I have the following function which toggles
    Message 1 of 19 , May 30, 2002
    • 0 Attachment
      > * Tobias Reif <tobiasreif@...> [2002-05-28 15:33]:
      >
      > I have this in my vimrc:
      >
      > " chdir to the directory containnig the current buffer
      > autocmd BufEnter * exe 'cd '.expand("%:p:h")
      >
      > Every time I enter a buffer, the working directory becomes the
      > directory where the file lives. Very useful when there are multiple
      > files to edit:
      >
      > vim /some/random/far/away/path/file1.c
      >
      > Then from within vim, I can just :e file2.c instead of
      > :e /some/random/far/away/path/file2.c.

      This functionality is nice, but I don't always like it (such as when I'm
      editing source code or some such) so I have the following function which
      toggles the behaviour on or off:

      " automatically cd to the directory of the current buffer upon entry
      function! ToggleAutoCD()
      if(exists('g:autocd'))
      silent! au! AutoCD
      unlet g:autocd
      else
      augroup AutoCD
      au! BufEnter * cd %:p:h
      cd %:p:h
      augroup END
      let g:autocd=1
      endif
      endfunction
      com! Toggleautocd call ToggleAutoCD()

      I then use the following mapping to affect this behaviour:
      map <s-f3> :Toggleautocd<cr>

      I also have the following mappings:
      map <Leader>cd :cd <C-R>=GetCurrentFileDirectory()<CR>
      map <Leader>ed :e <C-R>=GetCurrentFileDirectory()<CR>

      Where the 'cd' mapping changes the directory to the current buffer's
      just this once and the 'ed' one edits a file from the current directory
      without requiring me to BE in that directory. (Note that these do NOT
      press enter, allowing for customization of the command-line first.)

      The function GetCurrentFileDirectory is defined as:

      " returns the path of the current file, making sure there is a slash
      " following it (inserts one if it needs to)
      function! GetCurrentFileDirectory()
      let currdir=escape(expand("%:p:h"), " ")
      if (currdir =~ '[\\/]$')
      return currdir
      else
      return currdir . '/'
      endif
      endfunction

      I think I created this function because the trailing slash is necessary
      if I want to then append a filename for editing -- however, I couldn't
      blindly ADD a slash to the directory name in the mapping because the
      directory returned on a Windows machine might be 'C:/' (with the slash
      already there).

      Hope this helps,


      --
      "Life is too important to take seriously."
      -- Corky Siegel

      Salman Halim
      Current Product Engineer
      HP Bluestone
    Your message has been successfully submitted and would be delivered to recipients shortly.