Fork me on GitHub
#vim
<
2022-05-07
>
Benjamin17:05:12

tell me a vim tip. Mine is o to go to the other side in visual mode

💡 1
nate17:05:24

gv re-does last visual mode

🤯 1
Benjamin17:05:28

:thumbsup: keep 'em comming. There is also gi to go into last insert

🚀 2
nbardiuk18:05:15

g* or g# - the same as * or # but do not limit search to word boundaries, includes partial matches

catjam 5
👍 1
nate18:05:52

Two others I use a lot are q: and q/ to bring up command and search history, respectively, and search/edit them before running again.

💡 2
dave01:05:39

All of these are awesome!

dave01:05:51

While we're on the topic of * and #: • You can also highlight part of a word in visual mode and then press * and # to search for other occurrences of that string in the buffer. • When you're highlighting all occurrences of a string via * or #, you can use :%s to replace all of the occurrences with something else, by omitting the first part ("what to replace") of the sed expression. For example, place your cursor over foo, press * to highlight all instances of foo, then enter :%s//bar to replace all foo instances with bar.

👍 5
peterh18:05:01

Wow, g* and g# are really nice when I want to search for keywords AND the corresponding symbols in let-bindings, function params, etc., which doesn’t really work with just */`#`.

👍 2
Nundrum15:05:01

I use marks frequently, but double-tick '' is great because it returns the cursor to where you were before the last jump. And '. takes you back to where you last edited something.

sheluchin19:05:41

If you have a column of some repeating number like:

1
1
1
You can select them in visual mode and hit g<C-a> to turn them into an ordered list like:
2
3
4
and you can prepend a count like 10g<C-a> to increment by some number other than 1:
11
21
31

👀 1
👍 1
Nundrum20:05:46

Vim + tmux + christoomey/vim-tmux-navigator + esamattis/slimux => an instant IDE for anything that presents a command prompt. Bash, R, TCL, Python, anything!

Dumch13:05:40

Not sure if it's a tip, but want to share anyway and may be someone will be inspired by it and decided to write something like this / copy my code. I have a hotkey (comma + k), that checks the project root dir and launches an appropriate repl (lein, cli, or clojure-dart). It's written on fennel, but it's pretty easy to achieve: https://github.com/Liverm0r/dotfiles/blob/master/.config/nvim/fnl/lang/clojure.fnl Repl runs inside the built in terminal. Vim-iced or Conjure will be able to connect to it automatically.

reefersleep21:07:22

If you’re not using macros, you’re not living. To start recording, hit q followed by some letter, which will be the name of the macro. Like qa. Then, do your stuff. Once you’re done recording, hit q once more. Now, you can replay your recorded actions with @ followed by the name of your recorded macro, like @a. What I normally use it for is doing repetitive linewise changes. It’s hard to come up with good examples, but here’s one. As a part of an edit, I want to turn

this.x = args[0];
into
this.x = args[0];
this.x = args[1];
this.x = args[2];
this.x = args[3];
this.x = args[4];
this.x = args[5];
Standing on the first column of the first line, I do qayyp<C-a>q4@a Breaking it down: qa • start recording the macro named a yyp • copy the line and paste on the next line <C-a> • increase the number by 1 q • stop recording the macro 4@a • execute the macro a 4 times. It’s a silly example. The point is that you can do arbitrarily complex sequences of actions that you can repeat freely.

reefersleep21:07:25

I’m sure I’m not even close to the awesomeness that you can achieve with macros, but I use them whenever I can, so maybe I’ll get there eventually.

nate21:07:42

Oh yes, so true. Macros in Vim are amazing. can be a bit daunting to compose them in a flexible way, but the reward is worth it.

reefersleep07:07:40

@U0510902N are there any patterns for constructing macros that you like to use?

reefersleep07:07:53

Or just any use cases where you tend to apply them?

reefersleep07:07:30

I feel like I’m holding myself back by sticking to linewise changes or “find next x and do y“ish changes

dave14:07:08

I use macros all the time, for all kinds of stuff, not limited to linewise changes. I definitely recommend using them whenever you find yourself making the same kind of mechanical change in a bunch of different places.

dave14:07:43

There is a bit of an art to using the right kinds of motions in your macro that will work at every site where you want to apply the macro. For example, avoid using hjkl to move around, because the number of spaces you'll need to move could vary between the sites. It's often better to use word-wise motions, use f to jump to a specific character like {, etc.

dave14:07:07

I tend to use qq to start recording because it's easier to type than e.g. qa. I rarely need to record more than one macro at a time, but in the extremely rare case that I do, then I use other characters, like qa, qb, qc etc. Use @q to replay your macro the first time, but then after that, you can use @@ to replay the last macro that you played. That's nice because you can quickly replay the same macro at various sites by pressing the @ key over and over. If you happen to know how many times you want to apply the macro, then you can do like 4@@, as mentioned above.

dave14:07:47

Another hot tip: Whenever possible, make your macros include the steps that it takes to end up on the next instance that you want to call the macro. For example, if I'm writing a macro that I want to apply on every other line, starting at the beginning of the line, then I might end the macro with jj0 to go down two lines and position the cursor at the beginning of the line. That way, you can really fly -- you can press @@ repeatedly to apply your macro over and over, without needing to reposition the cursor in between.

nate15:07:57

@U0AQ3HP9U basically, every thing that @U0AHJUHJN said is what I was going to say. Well put!

reefersleep20:07:33

I tend to use f, / or just moving to the next line as my "set up for the next invocation" move. And I also tend to use qq - it was just easier to illustrate qa 😄 But I did not know about @@! I'll make sure to use that from now on.

reefersleep20:07:15

Using macros feels a bit like on-the-fly programming for text editing... Write the source for the function, then call it when you need it. So damn cool.

💯 1
sheluchin18:07:43

Another macro tip I don't think has been registered here: you can record a macro to a register like q, then do a visual block selection and execute the macro on each line of the selection with :'<,'>norm @q. Or you can search for a pattern with / and then apply the recorded macro to each line that matches with :g//norm @q... or each line that doesn't match with :v//norm @q.

💡 2
reefersleep08:07:22

I feel like I don’t use the :g or :v stuff enough

reefersleep08:07:32

Thank you for the tip!

daniel.flexiana01:07:43

To delete until some pattern I usually use in normal mode d /foo