Editing multiple files in a single buffer

It is a fact of programming that software written in modern programming language is divided into multiple files. That is all programmers use text editors such as vim, emacs, and eclipse, that have facility to work with and move easily between files.  With all these facilities, however, there are times when what you really want is to be able to concentrate and work the functionality you are implementing on a single file.  I frequently had this desire, and solving it seamed so easy, although I didn’t see anyone doing this.  So, I decided to implement a simple scheme by which I can edit multiple files in the same editor buffer.

For example, suppose that you are writing a C++ application. You need to create one file for each class. Also, for each class you need an implementation and an interface file. The number of files quickly grows, so it becomes harder to track the files you are using and you need to change between buffers frequently. All of this contributes to make it harder to visualize you program.

The simple solution I am now using is the following: create only one file (with an extension .cc in the case of a C++ source). Then, divide the file into sections, where each section starts with a few magic characters (in my case these characters are “@****”), followed by the name of the file.  For example, the separator “@**** class1.h”, when used in a line by itself, means that the next lines are part of the file class1.h.  In order to create real files from the original buffer, I use a small python script that reads the main file, parses the magic characters and writes lines to the appropriate file. The script is called “multifile.py”, and it is invoked as

python multifile.py <name of multipart file>

For example, I usually compile code written in C using

python multifile.py mycode.c

This way, I can edit multiple file with easy using the same original vim buffer. Whenever I want to create a new file (a header file for example), I just add a new section with the file name and start typing.  The next time I run my python script, the file will be created.

Moreover, I can easily add existing files to the edited buffer. I just need to add a new section with the desired name, then use the :r vim command to load the file in the following lines.  

The only concern you might have is that “make” will be confused by what is going on — since you are going to write your files back to disk at each edit iteration.  However, notice that the main buffer needs only to contain the files that you are editing at this moment. The remaining of the files in the application will be untouched as always. So you are overwriting only the files that are currently part of your edits. This also means that you should remove from your working set all files that you are not really editing — you can always load back a file into the editing buffer as described above.

I find this method to be simple and efficient. It has allowed me to work on project much quickly, while minimize the number of files that I need to have open at any moment.

Here is a list of the script I am using. While it can be improved in many ways, it does what I need in order to implement the scheme described above. Feel free to provide suggestions for improvements.


import sys
if len(sys.argv) < 2:
   print 'usage: multifile fileName'
   quit()

stdout = sys.stdout
for line in open(sys.argv[1], "r"):
   if line[:5] == "@****":
      words = line.split('*')
      if len(words) < 5:
         print 'error parsing', line
      file = str(words[-1:][0]).strip()
      sys.stdout = stdout
      print 'writing file', file
      sys.stdout = open(file, "w")
   else: print line,

Similar Posts:

About the Author

Carlos Oliveira holds a PhD in Systems Engineering and Optimization from University of Florida. He works as a software engineer, with more than 10 years of experience in developing high performance, commercial and scientific applications in C++, Java, and Objective-C. His most Recent Book is Practical C++ Financial Programming.

4 Responses to “Editing multiple files in a single buffer”

  1. Hi,

    I had the same idea: editing multiple files in a single window.

    Unfortunately, I cannot find any editor that support this feature (and postprocessing as you do, while smart, is not an option for me at this point in time).

    So, did you eventually find an editor supporting this feature?

    If so, please send me a link by mail,

    Thanks!

    By JeanHuguesRobert on Sep 15, 2010

  2. I still use the same idea when I think it makes sense. I like to keep all my work in a single buffer for simplicity. I found an editor called Code Browser that supports an idea that is not the same but related. It divides that files into sections, and shows only the section you’re working on. I will write about it later.

    By coliveira on Sep 21, 2010

2 Trackback(s)

  1. Nov 29, 2008: interface software | CNN.com
  2. Nov 29, 2008: c programming language | NBA.COM

Post a Comment