Textmate setup (Mac only)

By L. Collado-Torres.

For the past 6-7 years I have been using TextMate 2 as my text editor which I’ve found useful for R code, bash, Markdown, etc. You could also look into Sublime Text or use RStudio (post about this setup coming soon).

textmate look

Sometimes students are interested in this setup, which is what I’ll document here. Though I want to highlight that you can get a very similar setup using other tools. Note that this setup only works for Mac computers.

Setup

First, install TextMate 2 for free. TextMate allows users to contribute bundles which are a set of tools that enhance the editor. For example, if you want to quickly insert an R code chunk in a .Rmd file you can add a command for it inside a bundle. You can also use a bundle to get the editor to recognize R code inside an R markdown code chunk. Probably the easiest way to get jump-started is to copy my exact setup.

Change some preferences

So next, go to the preferences menu

preferences

and under bundle, choose the R bundle as shown below.

bundle

As you can see, it hasn’t been updated in a while. I have made a few edits myself here and there which I’ll describe in the next section.

You should also enable running TextMate from the terminal.

enable terminal

Finally, here are my main file preferences: I want my files to be .R files by default and to use linux line endings to avoid issues later on.

main prefs

Adding bundles from git

TextMate allows you to install bundles by adding the bundle files in a specific folder. The bundle files are most likely in a GitHub repository, so you just need to clone (download) them to where TextMate expect them to be.

## Go to main bundle directory
cd ~/Library/Application\ Support/TextMate/

## Download Leonardo's bundles (he uses the leo branch)
## For R, sendind code to iTerm2
git clone https://github.com/lcolladotor/r.tmbundle.git

## For merging
git clone https://github.com/noniq/Merge-Markers.tmbundle.git

## For more color themes
git clone https://github.com/elia/base16-themes.tmbundle.git

## For commenting Markdown files
git clone https://github.com/lcolladotor/criticmarkup.tmbundle.git

## For knitr::knit()
git clone https://github.com/lcolladotor/knitr.tmbundle.git

## For rmarkdown::render()
git clone https://github.com/lcolladotor/markdown-redcarpet.tmbundle.git

As you can see, these bundles help adapt TextMate2 for working with R files of different flavors. But it’s not beginner friendly, hence the upcoming blog post about using RStudio.

Some feature highlights

One of the features that I really like from TextMate is searching/replacing (you can use regex) across all the files and sub-directories of a given directory. It’s very useful when trying to fix a common typo across different files or finding all the places where a function/argument was used. The latter one is handy when you are looking at someone else’s code. It’s basically like searching inside a GitHub repository: example, search baseurl in all of blogdown for finding the code that reads it from a config file, which I did for this PR.

search in dir

You can also comment all the lines of code you have selected fairly easily using the Source bundle.

comment all lines

I’ve also used the Text, LaTeX and Gist bundles, though not as frequently. Also, TextMate automatically spell checks for you and knows to ignore R markdown code chunks.

Evaluating code in R console or iTerm2

If you download and install the iTerm2 terminal, you can configure TextMate to evaluate the code in that terminal. The particular code I have for doing this is in the leo branch of the following repo https://github.com/lcolladotor/r.tmbundle/commits/leo. In total I use 3 different keyboard shortcuts depending on whether I want to evaluate the code:

  • in an R console window;
  • in an R console window after automatically running setwd() to the directory that contains the files I’m looking at;
  • to the iTerm2 terminal, which is useful when working with a computing cluster.
getwd() ## run in terminal with cmd + enter shortcut

getwd() ## run in R console using backtip (`) shortcut

getwd() ## run in R console using cmd + R, runs setwd() first

evaluating code

Running rmarkdown::render()

If I’m working with an R Markdown file (.Rmd extension), I frequently use the alt (option) + t shortcut for running rmarkdown::render() and viewing the output file.

knit with rmarkdown

For example, if I’m working with the recount-brain/index.Rmd file (you can get it here), my setup allows me to run all the R shortcuts. That’s because it recognizes the R code chunk syntax and uses the source.r scope.

rmd scope

Anyway, after using alt (option) + t TextMate shows me the final html.

rendered html

A lot of the bundles in TextMate are from the days when we run Sweave(). So they work well with .Rnw files and all the like. I did modify one of them to use knitr::knit() instead of Sweave().

mate and rmate

If you enable the terminal preferences you can now use the mate command in any directory in your laptop. TextMate will open and show you all the tabs of files you had last opened in that same directory. This behavior is also a part of the .Rproj files with RStudio.

run mate

The command I really like is rmate because it enables me to remotely open a file from the cluster in TextMate, which combined with the evaluate in iTerm2 command makes it easy to work. Basically, I power up an iTerm2 terminal, log into the cluster, navigate to the directory that contains the files I’m working with, and then open them remotely with rmate.

rmate

Setting up rmate takes a bit of work but it’s definitely worth it.

  1. In the cluster, install rmate following the instructions at https://github.com/textmate/rmate
  2. Find a port that works for doing the forwarding. The default one will likely be taken already by another user. Find more about this here. There they use ssh -R 8080:localhost:80 public.example.com for testing. Sadly, I don’t know of a quick and easy way to find a port for you to use :/
  3. Edit your cluster’s ~/.bashrc file with the port information. Mine includes these lines where someSecretPortNumber is replaced by my port number.
## rmate port
# https://github.com/textmate/rmate
export RMATE_PORT="someSecretPortNumber"
  1. Edit your laptop’s ~/.ssh/config file so you don’t have to specify the port every time you ssh into the JHPCE cluster:
## Will work later as (aka, less typing):
## ssh j
## ssh cluster
Host j cluster
    User yourUsernameGoesHere
    Hostname jhpce01.jhsph.edu
    RemoteForward someSecretPortNumber localhost:someSecretPortNumber
    ForwardX11 yes
    ForwardX11Trusted yes
  1. Edit your cluster’s ~/.ssh/config file so the port gets forwarded also when you access a compute node with qrsh. All of JHPCE’s compute nodes are named computesomething, so we can take advantage of that in the config file.
# For rmate
Host compute*
    RemoteForward someSecretPortNumber localhost:someSecretPortNumber

Once you do these steps, rmate should work on a new terminal window.

Source

TextMate variables

I don’t remember right now if I manually edited the TextMate variables, but just in case, here’s the info.

textmate vars

References

[1] C. Boettiger. knitcitations: Citations for ‘Knitr’ Markdown Files. R package version 1.0.8. 2017. URL: https://CRAN.R-project.org/package=knitcitations.

[2] A. Oleś, M. Morgan and W. Huber. BiocStyle: Standard styles for vignettes and other Bioconductor documents. R package version 2.6.1. 2017. URL: https://github.com/Bioconductor/BiocStyle.

[3] H. Wickham, J. Hester and W. Chang. devtools: Tools to Make Developing R Packages Easier. R package version 1.13.5. 2018. URL: https://CRAN.R-project.org/package=devtools.

[4] Y. Xie, A. P. Hill and A. Thomas. blogdown: Creating Websites with R Markdown. ISBN 978-0815363729. Boca Raton, Florida: Chapman and Hall/CRC, 2017. URL: https://github.com/rstudio/blogdown.

Reproducibility

## Session info ----------------------------------------------------------------------------------------------------------
##  setting  value                       
##  version  R version 3.4.3 (2017-11-30)
##  system   x86_64, darwin15.6.0        
##  ui       X11                         
##  language (EN)                        
##  collate  en_US.UTF-8                 
##  tz       America/New_York            
##  date     2018-03-11
## Packages --------------------------------------------------------------------------------------------------------------
##  package       * version date       source                               
##  backports       1.1.2   2017-12-13 CRAN (R 3.4.3)                       
##  base          * 3.4.3   2017-12-07 local                                
##  bibtex          0.4.2   2017-06-30 CRAN (R 3.4.1)                       
##  BiocStyle     * 2.6.1   2017-11-30 Bioconductor                         
##  blogdown        0.5.10  2018-03-10 Github (lcolladotor/blogdown@471b086)
##  bookdown        0.7     2018-02-18 cran (@0.7)                          
##  colorout      * 1.2-0   2018-02-19 Github (jalvesaq/colorout@2f01173)   
##  compiler        3.4.3   2017-12-07 local                                
##  datasets      * 3.4.3   2017-12-07 local                                
##  devtools      * 1.13.5  2018-02-18 CRAN (R 3.4.3)                       
##  digest          0.6.15  2018-01-28 CRAN (R 3.4.3)                       
##  evaluate        0.10.1  2017-06-24 CRAN (R 3.4.1)                       
##  graphics      * 3.4.3   2017-12-07 local                                
##  grDevices     * 3.4.3   2017-12-07 local                                
##  htmltools       0.3.6   2017-04-28 CRAN (R 3.4.0)                       
##  httr            1.3.1   2017-08-20 CRAN (R 3.4.1)                       
##  jsonlite        1.5     2017-06-01 CRAN (R 3.4.0)                       
##  knitcitations * 1.0.8   2017-07-04 CRAN (R 3.4.1)                       
##  knitr           1.20    2018-02-20 cran (@1.20)                         
##  lubridate       1.7.3   2018-02-27 CRAN (R 3.4.3)                       
##  magrittr        1.5     2014-11-22 CRAN (R 3.4.0)                       
##  memoise         1.1.0   2017-04-21 CRAN (R 3.4.0)                       
##  methods       * 3.4.3   2017-12-07 local                                
##  plyr            1.8.4   2016-06-08 CRAN (R 3.4.0)                       
##  R6              2.2.2   2017-06-17 CRAN (R 3.4.0)                       
##  Rcpp            0.12.15 2018-01-20 CRAN (R 3.4.3)                       
##  RefManageR      0.14.20 2017-08-17 CRAN (R 3.4.1)                       
##  rmarkdown       1.9     2018-03-01 cran (@1.9)                          
##  rprojroot       1.3-2   2018-01-03 CRAN (R 3.4.3)                       
##  stats         * 3.4.3   2017-12-07 local                                
##  stringi         1.1.6   2017-11-17 CRAN (R 3.4.2)                       
##  stringr         1.3.0   2018-02-19 cran (@1.3.0)                        
##  tools           3.4.3   2017-12-07 local                                
##  utils         * 3.4.3   2017-12-07 local                                
##  withr           2.1.1   2017-12-19 CRAN (R 3.4.3)                       
##  xfun            0.1     2018-01-22 CRAN (R 3.4.3)                       
##  xml2            1.2.0   2018-01-24 CRAN (R 3.4.3)                       
##  yaml            2.1.18  2018-03-08 cran (@2.1.18)
Avatar
Continuous rstats learning

We are researchers at the @LieberInstitute, blogging about R packages, how-to guides & occasionally our own open-source software (opinions r our own) #rstats

comments powered by Disqus