How to Get to the Next Line in the R Command Prompt Without Executing

How do I get to the next line in the R command prompt without executing?

Use'SHIFT-ENTER' to get to a new line in R without executing the command.

How to output text in the R console without creating new lines?

Use cat() instead of print():

cat("0%")
cat("..10%")

Outputs:

0%..10%

paste single line command in R console without executing

If you do not copy a newline character at the end of the command, it will not be executed when you paste it. You will need to enter one manually.

Rather than copy/paste, you might want to use an editor where you can highlight the code you want to run and then send it to the R console via a button or shortcut key. The Windows R GUI has this feature, as does Rstudio.

Avoid Line break at end of cmd output?

Mofi has provided the crucial pointers in comments:

  • When executing a command interactively, cmd.exe unconditionally appends a a newline (line break) to the command's output, presumably for readability and perhaps also to ensure that the next prompt always starts on a new line.

    • This applies irrespective of what that command is. In other words: It doesn't matter that your command happens to be a PowerShell command.
  • However, that trailing newline does not become part of the command's output, therefore programmatic processing of a command's output is not affected, such as when you redirect > to a file or process the output lines one by one with for /f.

    • In other words: for programmatic processing you need not remove the trailing newline, because it isn't part of the actual command output.

    • Conversely, if you really need to in effect suppress the trailing newline for display, you'll have to modify the command's output - if that is even an option - so that the output itself doesn't end in a newline, as shown in this SuperUser answer for cmd.exe's own echo command; for PowerShell, you could do pwsh -c Write-Host -NoNewLine hello.


Edge case:

When capturing output from a batch file that is running without @echo off (or with echo on) - in which case the trailing newlines do become part of the output - you can filter out empty lines by piping to findstr /r /v /c:"^$" (as also shown in the linked answer); e.g.

foo.cmd | findstr /r /v /c:"^$"

However, note that all empty lines are filtered out this way - potentially including actual empty lines in the output from commands executed by the batch file.

If preventing that is required, a more sophisticated approach is required, which, however (a) relies on the standard prompt string (e.g., C:\>) being used and (b) can still yield false positives:

foo.cmd | powershell -nop -c "@($Input) -join \"`n\" -replace '\n(?=[a-z]:\\.*?>)'"

Finally note that if you execute the above commands without capturing or redirecting their output, their overall output in the cmd.exe console will again have a trailing newline.

How to make Windows console cursor jump to next line at the end of the line

I found the solution:
https://docs.microsoft.com/en-us/windows/console/setconsolemode

The flag DISABLE_NEWLINE_AUTO_RETURN seems to be enabled by default in VS.

How can I run multiple lines of code in R

Click on header Tools, then Global Options..., then Code, then Ctrl + Enter Executes and choose Multi-line R statement

How can I echo a newline in a batch file?

echo hello & echo.world

This means you could define & echo. as a constant for a newline \n.

Is there a hack to be able to run the current line or selection in RStudio without moving the cursor?

I updated to version 0.98.83 of RStudio using the daily build section.

It appears that at some point in recent versions of RStudio, the cursor no longer jumps when code is run from a selection in the script window.

That's great news.



Related Topics



Leave a reply



Submit