How to open copied file by VI

You can use vi itself to do the copy, by opening a.txt then saving the contents to b.txt (effectively copying it) and then switching to b.txt.

Putting it all together:

vi -c 'w b.txt' -c 'e#' a.txt

This is equivalent to running vi a.txt, followed by the :w b.txt command (inside vi), which will save the contents to a file named b.txt. But vi will still be editing a.txt at this point, so you follow up with the :e# command, which means "edit alternative file" (or "edit last file") and given vi has just touched b.txt, it will switch to editing that file.


using && operator

cp a.txt b.txt && vi b.txt

You can write your own function and use that function. In the below example, you can use cp1 as a command.

example:

$ cat test.txt
function cp1() {
  source_file=$1
  destination_file=$2
  cp "${source_file}" "${destination_file}"
  vi "${destination}"
}

$ . ./test.txt    
$ cp1 a.txt b.txt 

Tags:

Bash