Code Monkey home page Code Monkey logo

projectroot's Introduction

Projectroot

There already are a ton of plugins (such as CtrlP) that try to guess what the main project folder is of a file that you're working on, but this plugin allows to easily re-use that functionality in your own custom mappings or scripts.

Features

  • Works out-of-the-box on most projects by detecting folders such as .git.
  • Very customizeable.
  • Easy to use in mappings and your own scripts, just call ProjectRootGuess() to get the project root of the current file or ProjectRootGuess(file) for another file.
  • Can be combined with existing plugins (see Examples)
  • Tries to be as lightweight as possible. Only searches for a project root when a method such as ProjectRootGuess is used.

Methods provided

  • ProjectRootGuess([file]): Returns the project root for the given file (or for the current file if none is given).
  • ProjectRootExe(cmd): Temporarily changes the current directory to that of the project root, then executes the command.
  • ProjectRootCD([file]): Changes the current directory to project root of the given file.
  • ProjectRootBuffers([file]): Returns all buffers belonging to the same project of the given file.

Examples

Change current working directory to project root

Using a mapping:

nnoremap <leader>dp :ProjectRootCD<cr>

Automatically:

au BufEnter * if &ft != 'help' | call ProjectRootCD() | endif

Grep

To grep with your project as base directory, you could add something like:

nnoremap <Leader>g :ProjectRootExe grep<space>

Open file relative to the root

To start the command line with :e /my/path/to/project/, you could use this:

fun! EditProjectDir()
  return ':e '.ProjectRootGuess().'/'
endf

nnoremap <expr> <leader>ep EditProjectDir()

NERDTree

If you would like NERDTree to always open at the root of your project, try adding something like this to your vim config:

nnoremap <silent> <Leader>dt :ProjectRootExe NERDTreeFind<cr>

Changing which folder is detected as root

There are several ways to do this:

  • Creating a .projectroot file in the folder which you would like to be the root of the file of the buffer
  • Changing the markers that are used to detect files by changing the g:rootmarkers variable.
  • Setting the b:projectroot variable in that buffer (using an autocommand or script).

For plugin authors

If you would like to make our plugins compatible that would be cool! I think a method like this might be the way to go (so that nothing breaks if the user didn't install both plugins):

fun! s:guessprojectroot()
  if exists('loaded_projectroot')
    return ProjectRootGuess()
  endif
  return expand('%:p:h') " Just guess it is the directory of the current file
endf

Similar projects

  • Vim Rooter: Changes the working directory to the project root when you open a file.
  • vimprj: Allows the execution of project or folder specific scripts.
  • CtrlP: Has an option to search for files relative to the root directory of your project.

projectroot's People

Contributors

dbakker avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.