2

A common pattern I find myself doing in a non-vim text editor is:

Cmd a Cmd c

to copy all the content in the entire file, and then open a new file and

Cmd v

to then paste in all that content. When all is said and done, I can usually do this entire copy-paste in about 0.5s. What would be the most common/efficient way to do this in vim?

David542
  • 96,524
  • 132
  • 375
  • 637

1 Answers1

1

I do the same quite often by running the following commands: ggVGy.

The first command, gg, jumps to the beginning of the file. Then we select the entire first line with V. Still in visual mode, we jump to the end of the file with G, before copying (yanking) everything with y.

You can make this even faster and easier by mapping all of these commands to one key. With the mapping below, pressing the leader key and X will copy the entire file.

nnoremap <leader>X ggVGy

If you need the content to be available to the system clipboard, and not only in Vim, you should use "+y instead of y. This will yank to the "+ register used for the system clipboard. You can find more information on this topic in this question.

runar
  • 1,657
  • 1
  • 12
  • 17
  • awesome, though it doesn't copy to my system's clipboard when I try doing `"+y` (note that I'm on a mac). Is there another way to copy to system clipboard? – David542 Apr 12 '20 at 02:01
  • I am using macOS myself, and `"+y` works for me. You can try the `"*` register instead (`"*y`), as there is only a distinction between these registers on X11 systems. On macOS (and Windows) they both refer to the system clipboard. – runar Apr 12 '20 at 02:06
  • 2
    A shorter way to copy the whole file is: `:%y+` . – SergioAraujo Apr 12 '20 at 10:56
  • You can also run `:echo has('clipboard')` to see if Vim was compiled with the clipboard feature or not. If it returns 1, you are good to go. – runar Apr 12 '20 at 13:53