47

I have tried using "reindent lines" after highlighting the lines but that doesn't work (in fact it removes indentation). For example I want a quick way to

take a script like this

#parameters
b=2
c=1
d=4
q=3

and turn it into this

#parameters
    b=2
    c=1
    d=4
    q=3

in Rstudio

lmo
  • 37,904
  • 9
  • 56
  • 69
WetlabStudent
  • 2,556
  • 3
  • 25
  • 39

4 Answers4

83

Highlighting the code of interest and hitting tab should add another level of indentation. Shift-tab will remove a level of indentation.

Dason
  • 60,663
  • 9
  • 131
  • 148
  • 4
    wow, how incredibly simple. I just assumed that would delete all your code and replace it with a single indentation. – WetlabStudent Jun 12 '13 at 04:49
  • 9
    @MHH Other similar things RStudio does that may differ from what you might expect is that you can highlight a section and type a `"` or `{` or others and it'll enclose your highlighted section with the desired character instead of replacing it like a text editor would. – Dean MacGregor Jun 12 '14 at 14:10
  • 1
    Thank you for mentioning Shift-tab. Life changing! – Nova Sep 24 '18 at 14:01
  • SOOO helpful! Thanks! – Jeff Jarvis Nov 08 '21 at 23:14
5

You could use a shortcut. First, highlight the lines you want to indent, and then press: Ctrl + } (at least, that is the combination on my keyboard).

If you want to move in the other direction, then press: Ctrl + {.

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
0

RStudio will let you use Regex in Find and Replace. You can search for \n and replace with \n\t, which will do what (I think) you want.

The automatic indent is designed to format your code so that there is indenting where appropriate (inside a pair of brackets, for example). 'Reindent' is for when there has been automatic indenting applied which is no longer appropriate (eg brackets have been removed).

alexwhan
  • 15,636
  • 5
  • 52
  • 66
  • This doesn't seem to work. `\n` finds no results for me (and it should, because I do have more than one line in my file), and searches of the internet for multiline search in Rstudio have so far not found any hint that this is possible. I am using Rstudio 1.1.456 – Dannid Oct 31 '18 at 20:28
0

re format code and reindent works for me. But, for your example, I think it is more based on the space a tab creates. Also, the below link is a good way to write good codes

https://google-styleguide.googlecode.com/svn/trunk/Rguide.xml

http://r-pkgs.had.co.nz/r.html

user5249203
  • 4,436
  • 1
  • 19
  • 45
  • the link to the style guide doesn't work any more. it's now: https://google.github.io/styleguide/Rguide.xml – tjebo Sep 02 '18 at 19:35