Skip to content

Open a text file with vi/vim in another Terminal

4 messages · Patrice Kiener, Dirk Eddelbuettel, Ivan Krylov

#
With Debian and macOS, the default editor (getenv("EDITOR") or 
getOption("editor")) is "vi" which opens vi/vim. The following instruction:

   fileREP <- file.path(R.home("etc"), "repositories")
   system2(getOption("editor"), fileREP, wait = FALSE)

works fine with Windows but is inappropriate in the Debian/RStudio/RCode 
console as it launchs vi/vim, warns about input and outputs and prints 
the text file WITHIN the R console and then does nothing. Closing vi/vim 
requires a manual action. In several tests, I even had to force R to 
recover. Hence My question is:

What is the system(), sytem2(), eventually file.edit() instruction that 
opens the text file with vi/vim in a NEW Unix terminal and allows to 
keep working in the R console?

Thank you for your advices.
#
On 17 December 2021 at 18:48, Patrice Kiener wrote:
| 
| With Debian and macOS, the default editor (getenv("EDITOR") or 
| getOption("editor")) is "vi" which opens vi/vim. The following instruction:
| 
|    fileREP <- file.path(R.home("etc"), "repositories")
|    system2(getOption("editor"), fileREP, wait = FALSE)
| 
| works fine with Windows but is inappropriate in the Debian/RStudio/RCode 
| console as it launchs vi/vim, warns about input and outputs and prints 
| the text file WITHIN the R console and then does nothing. Closing vi/vim 
| requires a manual action. In several tests, I even had to force R to 
| recover. Hence My question is:
| 
| What is the system(), sytem2(), eventually file.edit() instruction that 
| opens the text file with vi/vim in a NEW Unix terminal and allows to 
| keep working in the R console?

It all depends on (on all systems including Debian) your value of EDITOR,
which also reflects the value of VISUAL. In shell that is written (in
Renviron) as

    ## Default editor
    EDITOR=${EDITOR-${VISUAL-vi}}

which takes vi if VISUAL is not set, and the value of that if EDITOR is not
set.

Now vi itself can be one several vi implementations. Right now on my system

    /etc/alternatives/vi -> /usr/bin/vim.basic

but one could also use nvi etc. You could possibly set up a shell script to
take the file name argument and do tricks with terminal (say) and or
sessions.  So the idea is that this under your control.

That said, I don't have tips. If I need to edit, I edit outside of R and/or R
already runs in an editor, or ...

Dirk
#
On Fri, 17 Dec 2021 18:48:29 +0100
Patrice Kiener <patrice.kiener at inmodelia.com> wrote:

            
Starting a new terminal is somewhat hard, but the shortest path to
getting this particular command working would be options(editor =
'gvim'). It's almost like Vim in a terminal, plus a few features useful
in a windowed interface.

Note that gvim backgrounds itself by default, so this would break
edit() and file.edit(): since the GVim process started by R terminates
almost immediately after spawning a child to do the rest of the work, R
decides that the editing session is done. Unfortunately, it's hard to
make R's edit() pass the -f flag to gvim to prevent it from doing that,
but a trivial shell script wrapper could be used for that purpose:

#!/bin/sh
exec gvim -f "$@"
#
Patrice,

Also: if you are on a terminal, have you discovered tmux / byobu yet to multiplex?
It is fairly magic as you can just open as many 'sessions with the outer
sessions', the sessions persist and many more advantages.  I talked a little
about this (with short videos) last year

    https://dirk.eddelbuettel.com/blog/code/t4/

in episodes 4, 5 and 6.

Dirk