Invest in a good text editor

Scientists should invest time in a good text editor: pay the upfront cost of learning to use and customising a single editor for all of your text needs. This may be obvious to programmers, but less so to scientists who may have yet to recognise the benefits of a good editor.

Much scientific analysis and documentation can be achieved with plain text files (e.g., .py, .m, .f, .r, .tex, or .md). The default method to work with multiple file types is to use multiple IDEs (Integrated Development Environments): Matlab for m-files, Spyder or IPython notebooks for python scripts, TexStudio or TeXnicCenter for latex files, RStudio for R, or one of the countless editors for Markdown currently available.

Using a single editor has many benefits over using a range of editors within each IDE:

  • You need to know only one set of keyboard shortcuts
  • You can manage different projects easily since multiple file types aren’t scattered over multiple editors
  • Helpful features like Split Windows, Code Folding or Vim Emulation (a must have for me) will always be available
  • Your preferences such as dictionary language, colour scheme, code font, and indentation behaviour are consistent across file types
  • Good editors come with features you never knew you wanted like Multiple Cursors or Fuzzy Filename Matching to quickly switch files

Personal example: I use Sublime Text and to run a Python script I press Shift + Enter; to evaluate a single line in that Python script, I select it and press Shift + Enter; to generate a PDF from a LaTeX file, I press Shift + Enter; to convert a Markdown file to rich text, I press Shift + Enter. Jumping between these files requires a single keyboard shortcut.

Choose you editor

There are many editors to possibly use and a search for recommendations leads to countless subjective opinions. I recommend either Sublime Text or Atom. (Others will recommend Vim or Emacs for good reason, but these are more difficult to set up.) Both Sublime and Atom have minimal learning curve to get started. This leaves time to customise the set up.

Install the right plugins

Sublime and Atom (and most other editors) support many languages out of the box. Plugins then add extra features to really make life easy. These features may help automate completions, show documentation, run tests etc.

Examples of plugins for Sublime are Anaconda (for Python), LaTeXTools, and R-Box (for R). A quick search usually leads to the obvious choice for the language of interest.

Run a script (with the IDE’s assistance)

The plugins above come with shortcuts to run a script or file. A typical workflow of a scientist, however, is to run a script and then interact with the variables at the command prompt in a REPL-like fashion. Deciding how to proceed is the most difficult step. Despite what I said above, I recommend getting some help from your IDE at this stage.

Python and Sublime Text example: I run Python scripts from Sublime via Spyder’s runfile function in Spyder’s IPython console. Once the code has run I can inspect the variables in the console. Or, rather than running the file as a whole, I can evaluate a selected line or group of lines from Sublime. As noted above, I’ve set this up so it is all achieved by a single shortcut (see detailed instructions for Windows or Linux).

Matlab and Sublime Text example: I run Matlab m-files from Sublime in Matlab’s command window using the run function. Similarly, I can evaluate a group of lines by pasting their content from Sublime in the command window. As for the Python example, this is set up to work with a single shortcut (see detailed instructions for Windows or Linux).

Summary

By combining IDEs with Sublime Text, I maintain the benefits of having an IDE, but I also get to use my preferred editor instead of the editors that come with, say, Spyder or Matlab, which lack many features I want. Although it took me a while to figure out how to achieve what I’ve described above, it was time well spent. I recommend it if much of your time is spent editing scientific code, documentation, or publications.

Advertisements

Author: Ken Hughes

PhD student in physical oceanography

16 thoughts on “Invest in a good text editor”

  1. You actually make it appear so easy together with your presentation but I in finding this topic to be actually something which I think I’d never understand. It kind of feels too complex and extremely large for me. I’m having a look ahead in your subsequent submit, I’ll attempt to get the dangle of it!

  2. I’m now not sure where you are getting your information, but good topic. I needs to spend some time learning much more or understanding more. Thank you for great info I used to be in search of this info for my mission.

  3. Great blog here! Additionally your website loads up fast! What host are you the usage of? Can I get your affiliate link for your host? I want my website loaded up as quickly as yours lol

  4. Thank you for some other excellent article. The place else may just anyone get that kind of information in such a perfect way of writing? I’ve a presentation subsequent week, and I’m on the look for such information.

  5. I do believe all the ideas you’ve presented on your post. They are very convincing and will certainly work. Still, the posts are too brief for beginners. Could you please prolong them a little from next time? Thanks for the post.

    1. I try to strike a balance between concision and providing all the necessary details. I’m wasn’t sure that this post would appeal to beginners so I skipped over many of the details

  6. Magnificent web site. A lot of helpful information here. I’m sending it to some friends ans additionally sharing in delicious. And obviously, thank you in your effort!

  7. I followed both of your GitHub tutorial, but none of them worked. With spyder, I run a line, and the code is “pasted” to spyder, but doesn’t run, so it’s the same that copy-paste. With Matlab, I run the line, but nothing happens in the Matlab window.

    1. Windows or Linux? If the code is pasted to Spyder, are you simply missing an extra press of Enter at the end? Not sure what you mean by “I run the line, but nothing happens in the Matlab window”. Does anything happen in Sublime first?

      1. Windows 10, with Spyder 3.2.8 and Sublime Text 3.1.1.
        In Spyder, it’s weird, it’s the code goes either to the editor, where I should select and press F9, or else goes the console, where I should press enter.

        With Matlab, the code seems like running in the Sublime text window, but nothing happens in the Matlab windows (nothing in the editor, console or variable pane).

        Thanks for your answer.

    2. I have updated the instructions linked at Github for the Spyder example. I had forgotten to add a line to focus the IPython console after activating the window. As for Matlab, the problem is likely with the window title. In the example, the window is titled ‘ MATLAB’. Check to see if yours is different and then adjust accordingly.

      1. Thanks, but the Spyder solution is still not working. The default behavior is that the code got pasted into the editor, and then it adds a line. If I manually switch to the ipython console, the code works when I run one line, but when I try to run several lines, the code just get an extra line at the end (I think adding an extra enter to the code could solve that part)

  8. Please ignore the last comment. I’d changed the run_file, but no the run_selection. With the extra enter it’s working great. Thanks for this.

    PD: I’m currently modifying the code so the windows goes back to Sublime Text.

    1. Sorry for keep commenting. I added the extra enter in Spyder, plus the line “WinActivate, ahk_exe sublime_text.exe” to go back to sublime.

      With Matlab, I changed the line that selects matlab to “WinActivate, ahk_exe matlab.exe”, as my version is not for students, and also added the final line to go back to ST3.

      Thanks a lot, now both programs are working with ST3.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s