By comphys

2018-08-15 07:49:09 8 Comments

Say, I have 'a.txt'. I would like to copy the file and open the newly copied file using VI.

cp a.txt b.txt
vi b.txt

How to combine the two command in a command?


@Kamaraj 2018-08-15 08:15:20

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


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

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

@comphys 2018-08-15 08:47:31

I would like to know an ad hoc method which can be written in one command line. Anyway, thank you for the helpful answer.

@Barmar 2018-08-15 17:27:42

@comphys If this is something you do frequently, why not define a function for it? If you don't do it enough to need a function, why does it even matter?

@comphys 2018-08-26 03:08:46

@Barmar I don't frequently encounter this kind of situation. However, I wanted to know the answer to apply it to the other similar situation and the curiosity makes me ask!

@Hielke Walinga 2018-08-15 08:24:21

If you want a way to save you typing you can use bash build in functionality to repeat the last word of the previous command. You can do this by ALT+.

> cp a.txt b.txt
> vi ALT+.

Very useful, and reminds you of the dot operator of vim.

Happy golfing.

@Konrad Rudolph 2018-08-15 10:22:30

Hmm this doesn’t seem to work for me. Is this a readline Emacs command (I’m working in vim mode)?

@Grump 2018-08-15 12:30:40

!$ may be better than ALT+.

@Hielke Walinga 2018-08-15 12:40:54

@Grump That's indeed an interesting suggestion. I haven't thought of using that stuff. Actually $_ is more interesting, because it repeats the last executed command as opposed to the last command in history (that is what !$ does). But why would it be better. If we count keystrokes it is more and I also think the special symbols are a bit hard to reach.

@Hielke Walinga 2018-08-15 12:42:43

@KonradRudolph Indeed, this is for the emacs readline mode. Vim mode has the same functionality, but since . already has a function underscore is used here. ALT + Underscore also works in emacs mode btw.

@Grump 2018-08-15 12:43:42

!$ is the last argument from the last command in history. I suggest it because it works in more shells and doesn't rely on keybindings

@Kevin 2018-08-15 18:10:31

Agree with Grump, but @KonradRudolph try hitting escape (not holding it) and then ..

@Hielke Walinga 2018-08-15 19:46:42

@Kevin This is indeed a trick that works if your alt key does not work in emacs mode. The reason for this is that most terminal emulators treat key after escape and alt plus key the same. So if your alt key does not work, you can trick the terminal emulator with a key after escape. But if you are in vi mode this most likely will not work. On my machine I could however force it to work with '"\e.":yank-last-arg', but somehow this binding then only worked when in normal mode, and not when in insert mode.

@filbranden 2018-08-15 08:01:41

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.

@D. Ben Knoble 2018-08-15 12:57:42

Im not sure if vi has saveas (it may have come from vim) but it would shorten the command.

@Random832 2018-08-15 21:03:51

The :f command may be useful - that will just change the filename, and the user can write it the normal way at the end of the editing session with :wq.

@msp9011 2018-08-15 07:52:19

using && operator

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

@comphys 2018-08-15 07:57:09

Thank you. Is there any other way where I can type b.txt only once?

@Kirill Bulygin 2018-08-15 09:43:49

@comphys src=a.txt; dst=b.txt; cp "$src" "$dst" && vi "$dst"

@Hielke Walinga 2018-08-15 12:44:18

@comphys Yes we actually can: cp a.txt b.txt && vi $_. Grump reminded me of this in another comment thread.

Related Questions

Sponsored Content

2 Answered Questions

[SOLVED] How do I combine two arrays in bash, by summing like-indices?

  • 2019-05-08 09:06:03
  • Pureferret
  • 45 View
  • 0 Score
  • 2 Answer
  • Tags:   bash

4 Answered Questions

[SOLVED] Printing the sum-size of copied files in bash

  • 2018-09-09 16:07:35
  • user90704
  • 212 View
  • 3 Score
  • 4 Answer
  • Tags:   bash

3 Answered Questions

1 Answered Questions

[SOLVED] In a bash script, using the conditional "or" in an "if" statement

  • 2012-09-08 20:37:35
  • Andrew
  • 332851 View
  • 101 Score
  • 1 Answer
  • Tags:   bash shell-script

2 Answered Questions

4 Answered Questions

2 Answered Questions

[SOLVED] grep for repeated values from pattern list

  • 2014-12-05 12:02:32
  • Roli
  • 1152 View
  • 4 Score
  • 2 Answer
  • Tags:   bash awk grep

3 Answered Questions

[SOLVED] How to fork a "cat" command?

  • 2014-06-17 21:51:45
  • user72685
  • 666 View
  • 0 Score
  • 3 Answer
  • Tags:   bash cat fork

1 Answered Questions

[SOLVED] Input a list of files & directories to be copied with ditto

  • 2012-01-24 04:21:22
  • sardean
  • 747 View
  • 2 Score
  • 1 Answer
  • Tags:   bash osx backup

Sponsored Content