问题
What is the command or keyboard shortcut equivalent to pressing the little broom button in the History window of RStudio which clears out the entire history buffer in the current session?
Note that I don't mean Ctrl + L which clears the command window, I mean clearing history which is accessed by pressing the up-arrow.
回答1:
Function based on Carl Witthoft's helpful comment which clears the history (assumes you can write to the working directory):
clearhistory <- function() {
write("", file=".blank")
loadhistory(".blank")
unlink(".blank")
}
回答2:
I guess there is no shortcut for the broom icon. Another method can be to manually delete the .Rhistory
in the RStudio
directory, but you have to restart the session to see this taking effect.
回答3:
I created a snippet. Go to Tools> Global Options> Code> Snippets> Edit Snippets and paste this in the R section and click Save. There should be tabs after the first line or else the snippet won't work. I like to use the here
package which will find the file at the project level no matter where the current working directory is. You can change that part of the snippet if you wish. Change h
to whatever you want.
snippet h
`r eval(parse(text = '
write("", file=here::here(".Rhistory"))
loadhistory(here::here(".Rhistory"))
'))`
Now all you have to do is type h
then Shift+Tab to run the snippet and the code in it! Read more about snippets here
来源:https://stackoverflow.com/questions/19754128/command-or-keyboard-shortcut-to-clear-command-history-in-rstudio