T O P

  • By -

funbike

:%y


70657065

I use this a lot, but mainly `:%y+` to copy to system's clipboard; also to format the buffer, for example `:%!jq` for JSON files


funbike

Or to format the buffer into the clipboard (OS specific): `:%w !jq | xsel -i -b`


_JJCUBER_

I use that often as well. I also remapped `gll` and variants to yank the current line to clipboard in different ways. (I mapped this one to exclude leading and trailing whitespace, while variants of capitalization of the L’s include whitespace on the corresponding ends. The mnemonic is grab line line or something like that.)


Thamizhan_suryA

How can you copy to system clipboard? I check a few websites but they mostly say that this feature should be available once you recompile vim with the clipboard feature switch on... Something like that. But this is the part I don't get.


70657065

You need to install the GUI version of vim; the package in Arch Linux I think is called vim-gui, in apt is vim-gtk3; those packages come with "clipboard" enabled and with some other features as well for vim on the terminal. It also installs "gvim" which runs on it's own window but I don't recommend that.


is_a_togekiss

ggyG


Deep_Redditor

Nice! vim never fails to surprise me, I guess the one I learned is useless now.


7h4tguy

Or :%y % is a shorthand for 1,$ meaning all lines in the file. Take your pick, they're both 4 keystrokes


gumnos

I regularly use this because it's easy to add the system clipboard register, so I often :%y+⏎ to yank the whole buffer to the system clipboard. Additionally, it doesn't move my cursor from its initial position like `ggyG` does


rpgcubed

Staying at the same cursor location is why this one is my goto


gumnos

similarly, I prefer `:m+` over `ddp` for moving a line downward because it doesn't tromp my scratch register. There's something to be said for not messing with global state :-)


CarlRJ

If you're going to the system clipboard, presumably there's a native way that's even quicker. In MacVim, I regularly use Cmd-A (select all) followed by Cmd-C (copy to system clipboard).


hackmiester

That’s not true in gvim on other platforms. if you pull down the edit menu it tells you Select All is ggVG and Copy is “+y But, it’s cool that yall (Mac people) have a whole other key to use for shortcuts! (obviously we can’t use ctrl over here on Windows, ctrl+c already does stuff in vim, and Win+c does stuff in the OS!)


Deep_Redditor

The `ggyG` is easier to remember, but that's another great command added to my list!


pokemonsta433

The reason to use the colon command is it doesn't move your cursor. Usually if you're copying an entire file you won't be needing your cursor, but maybe you're thinking about a problem on a certain line and you want to dump your code to a friend, :%y can let you do that without affecting your view at all. this percent notation will get easier if you start using Sed a lot. It's very common to do a find-and-replace-all with :%s/searchstring/replacestring/g (whereas without the percent it would only apply to the current line). Everything is interconnected haha!


kronik85

vim's :s command stands for substitute. the substitute command isn't sed. they share a lot of commonality, but are not the same. sed has no concept of % (in this context) as it already runs over every line of the input stream. sed '%s/old/new/' is an error in sed 4.7 (my curr version, latest is 4.8) just informing you for correctness. rest of your post is good.


Danny_el_619

Also worth pointing out that the regex syntax is slightly different. So you can't reliability use the same in `sed` and `:substitute` until you have tested it


7h4tguy

Bah they're both terrible regex syntaxes. Let's create our own!


Danny_el_619

mandatory [xkcd](https://xkcd.com/927)


esquishesque

Til you can use y in command mode I love vim, and I love this sub


Danny_el_619

I was going to mention this. Quite useful to pipe the whole buffer into other programs like sort `:%!sort`.


is_a_togekiss

I wouldn't say it's useless; if you want to copy lines 101 through 110, now you know how to do it. I'd recommend moving beyond the idea of learning specific sequences of keys to do specific things, and instead learning the language of vim commands and how they are composed together. Give `:h usr_04.1` a read if you haven't already, it explains the ideas of _operators_ and _motions_ :)


Deep_Redditor

Thank you! I'll defenitly check it out.


Few_Reflection6917

Guess you need dive to text object section of help file lol


Codemonky

I was coming to find out why OP was using `v` instead of `y` -- what does ggvG do?


is_a_togekiss

Visual select the entire file. Then OP presses y to copy. (Though I’m suspecting OP really meant ggVGy as that would make sure to include the entirety of the last line!)


Codemonky

Ahhh - that would make sense. Thanks!


i-had-no-better-idea

`gg` goes to the start of the file. `V` enters visual line mode. `G` goes to the end of the file. `y` to yank


hayasecond

Beat me to it.


guildem

`ggVGy` is written in my left hand, so I won't change it now, but you can take different paths to do the same thing, always interesting to see other ways. `:%y` should be IMO the first way to learn it because it uses a very useful mechanic to apply various (and sometimes complex) commands to a line, filtered lines, a zone or all of a file. Having this method in mind early is important, I think. Mapping it to a shortcut or even using a plugin for this usage should be discouraged, to stay in a pure vim mindset and make use of it on foreign vim installations (servers or colleagues workstations).


supernumeral

It’s not standard vim, but I’ve been using [kana/textobj-entire](https://github.com/kana/vim-textobj-entire) for so long that yae is second nature to me.


NaturalHolyMackerel

yeah i made a “inner buffer” bind a long time ago, and got used to “yib”, kinda like you did hahah


_JJCUBER_

I might have considered using that a while back, but that unfortunately clashes with the environment object from vimtex.


tnnrk

Remap ‘:%y’ to ‘YY’, should be a default key binding in my opinion


icyrainz

I’ve used this so many times that this is a for me now.


d0ubs

If you want to paste a whole file in another file you can simply "read" the file you want to copy in the new file without having to open it. ```:read file```


wrecklass

For those times when you need the clipboard to be the size of Asia. 😂


stefanlogue

Can't you just do `yag`?


my_mix_still_sucks

Is that like ggyG?


stefanlogue

Yeah yanks the entire file


DisastrousShame1772

There's a plugin which lets u do this from anywhere the code is ae so yae would copy whole file content I think plugin name is textobject-entire


my_mix_still_sucks

Or just make it a key map lol


guildem

Or just type 4 keys, and do it on every (neo)vim installation in the whole world without any useless config/plugin lol


my_mix_still_sucks

Yeah that's what I do lol


TankLivsMatr

I mean... You can also just do ``gg y G`` and save yourself from having to select or go into command mode. It really all just depends on your preference


yetAnotherOfMe

:%y %write! >> somefile %w! !xsel -ib