News for hackers. Stuff that matters.
Re:How Did Vim Become So Popular? by cat199 on Tuesday June 30, 2020 @08:31PM (#23696424)
copy 10 lines from the current position into buffer q, go to the start of function jnk, copy 5 lines into buffer w, jump to line 10, paste contents of q, contents of w, and contents of q sequentially one after the other:

"q10yy /jnk "w5yy 10G "qp "wp "qp

now, go to the start of the document and indent the next 20 lines by 10 spaces:

0G .,+20s:^: :

ok great, now find JUNK and replace 'a1' with 'A1' in the 15 lines that follow:

/JUNK .,+15s/a1/A1/g

probably not as efficient for multi-file refactoring, but for raw 'drag race' style text munging, everything is there, loaded in muscle memory, and under the home row of your keyboard so you don't have to lift your hands at all.

Re:How Did Vim Become So Popular? by ricksharp on Tuesday June 30, 2020 @10:36PM (#23697147)
Thank you for that example!

Just thinking out loud how to do that in vscode for comparison:

- I don’t think repeating actions a specific number of times makes sense in most use cases. Counting 10 lines would take longer than doing shift up/down n times (which would be exact with feedback) to select the desired lines.

- Multiple Copy Paste buffers sounds interesting, I might look up a vscode extension for that. (Had a copy paste queue in Visual Studio but it would break sometimes and would lose something important).

- go to start of function jnk, there are many ways to do that in vscode. Ctrl+f jnk enter would be one.

- Goto start of document is Ctrl+Home

- Indent 20 lines by 10 spaces, could do Alt+PageDown, then up/down to get to the right place (without counting or caring that it is exactly 20 lines), then with multi-line insertions could space,tab, etc.

That operation would probably justify mouse usage though, in which you simply hold alt, drag ~20 columns/~10 cells and hit space - which itself would be faster than counting 20 lines.

- find junk: ctrl+f junk enter

- select 15 lines (shift+page down,up/down to adjust) I can preview I have selected the correct area, other possibilities to select current function, etc.

- Ctrl+h to replace, a1 tab A1 Alt+L Enter

- of course in most cases, I am renaming a symbol, so “F2 A 1 Enter“ would do

I think the reality is that counts are not known before hand, so being able to increment/decrement with visual feedback is perhaps faster than a miscount, but I’d have to see it to know.

Anyway, thanks for the detailed examples! I can certainly see how it would be powerful if committed to muscle memory, but even with vscode there are many shortcuts I don’t use because of formed habits.

Re:How Did Vim Become So Popular? by MAGZine on Wednesday July 01, 2020 @02:30AM (#23698397)
I have a plug-in installed in vim and vscode (and ilellij) that provide me with relative line numbers in addition to absolute line numbers. So, the current active line might display "353" in the gutters for the 353rd line. Both the line above and below are 1. The next lines above and below are 2. And so on.

There is never any "counting lines". If I need line's number, I can do a quick addition/substraction in my head or j/k $offset to see the other line's absolute number.

I can do line-based operations a lot faster than shift-tapping or click-dragging, with exact precision because my editor has been customized to provide the data I need. Eventually you get pretty good at estimating line numbers and character columns too.

Re:How Did Vim Become So Popular? by cat199 on Wednesday July 01, 2020 @11:02AM (#23702140)
cheers :)

for line counting - indeed a good point and clearly required for the examples -

After time, eyballing this becomes a skill. For things that fit on a screen (~50 lines or so, give or take) I'm usually correct 1st time or within a few lines. I can check quickly by moving the cursor (20j to go down 20, 20k to go back, repeat if necessary to get the line count)

also as others have pointed out, I might not be doing the best/optimal thing for some of these, just some examples of how I do things that popped in my head :)

Re:How Did Vim Become So Popular? by Lio on Wednesday July 01, 2020 @04:02AM (#23699029)
> Counting 10 lines would take longer than doing shift up/down n times

Vim get round that by providing the option of relative line numbers to the current line.

That’s accessed via

  :set relativenumber
Re:How Did Vim Become So Popular? by CGamesPlay on Tuesday June 30, 2020 @11:24PM (#23697436)
I find it interesting how we use vim so differently. I'd never type "10yy", I'd use "y9j". I never use "10G", I use ":10<CR>" (<CR> = enter for those not familiar). Similarly, "0G" is "gg" for me. Call me a notepad user, but that whole second snippet for me would be "ggV19j>" followed by "." until things lined up correctly. Similarly, the third would have been "V14j:s/a1/A1/g".

Nit pick: "q selects register q. A buffer is a different thing.

Re:How Did Vim Become So Popular? by cat199 on Wednesday July 01, 2020 @11:05AM (#23702190)
yep - probably a few ways to do things and I might not be doing the cleanest, just the way I learned/figgured it out
Re:How Did Vim Become So Popular? by lostmsu on Tuesday June 30, 2020 @09:34PM (#23696793)
> now find JUNK and replace 'a1' with 'A1' in the 15 lines that follow

The real scenario is usually to replace, for example, in a function block. How do you do that? Counting function lines seems like a non-starter.

Re:How Did Vim Become So Popular? by tvb12 on Tuesday June 30, 2020 @10:34PM (#23697138)
If the language uses curly braces to define blocks, it's easier: ciB. Parentheses are easy as well: cib. Otherwise, I'm not sure there's a good solution currently. You can substitute by indentation with a plugin (https://github.com/kana/vim-textobj-indent/blob/master/doc/t...). For Lua, I usually do c/end<enter>.

I believe this is an area where treesitter can help, so I'm looking forward to Neovim's integration.

Re:How Did Vim Become So Popular? by jcranmer on Tuesday June 30, 2020 @10:17PM (#23697015)
Highlighting the initial { of a function and then typing 'va}' ('v' for entering visual mode, and 'a}' moves to the matching close brace. Similar commands work the [], (), and <> pairs) selects the entire function body. From there, a regular s command will operate on the entire selected text.
Re:How Did Vim Become So Popular? by joatmon-snoo on Tuesday June 30, 2020 @10:24PM (#23697069)
Mine is v% :)
Re:How Did Vim Become So Popular? by alwillis on Wednesday July 01, 2020 @06:02AM (#23699785)
You know these aren’t new issues, right? Vim has been around since 1991 and back to 1976 from Bill Joy’s editor.

Of course Vim can replace a function block easily and has been able to do so for decades.

Re:How Did Vim Become So Popular? by ricksharp on Tuesday June 30, 2020 @10:51PM (#23697250)
Ok, also am curious about multi-insertion with cntr-left/right that vscode does:

For example, let’s say I have a json object with variety of field names, one per line and I want to remove the quotes.

With vscode, I can alt select all lines, Home Ctrl+right del cntl+right del.

The trick is the field names are different length, does vim have multi-edit with the ability to navigate relative to each insertion point?

Edit: Here is a good video example (https://youtu.be/iomWEDMBlcE)

Can vim do something like that?

Re:How Did Vim Become So Popular? by dhmiller on Wednesday July 01, 2020 @01:31AM (#23698065)
There are things that vim does better. There are things that VS Code does better. There are things that Visual Studio does better. You can memorize all sorts of things about your particular editor of choice and you will be more efficient and effective in that editor.

If I wanted to cherry pick examples of what vim does better, I could start all of them with, "first ssh into a random server."

What makes vim great is that it has a tremendous number of features and is available nearly everywhere. Can VS Code do that?

Re:How Did Vim Become So Popular? by snorlaxle on Wednesday July 01, 2020 @08:13AM (#23700530)
VS Code can (sort of) do that: https://github.com/cdr/sshcode
Re:How Did Vim Become So Popular? by imtringued on Wednesday July 01, 2020 @04:26AM (#23699196)
sshing into a server invalidates all the time you spent on customizing your local vim.
Re:How Did Vim Become So Popular? by alwillis on Wednesday July 01, 2020 @05:54AM (#23699733)
If you’re on your machine, you can do all of your stuff because you can edit files remotely from within Vim using SSH as the transport: http://usevim.com/2012/03/16/editing-remote-files

If you’re on a new machine, you can grab your dotfile repo from wherever you keep it—like GitHub for example—and you’ve got your complete config, since there just text files. Takes 5 minutes.

But even plain vanilla Vim is more than enough for many tasks on a new Unix/Linux box.

Re:How Did Vim Become So Popular? by bjoli on Thursday July 02, 2020 @05:04AM (#23711342)
Not if you use Emacs/evil. Tramp invalidates most of the "well, vim is available on all my remote servers, whereas Emacs never is". I just remote into a server from within Emacs.

Evil is also the best and most complete vim experience outside of vim.

Re:How Did Vim Become So Popular? by RMPR on Wednesday July 01, 2020 @04:56AM (#23699378)
For your use case there are a lot of ways to do that in Vim without using the multi cursor, here[0] is one thing I just recorded to show you a glimpse of what's possible with just a search and replace: I just find a pattern and replace it with a part of the same pattern.

> Here is a good video example (https://youtu.be/iomWEDMBlcE)

Again, you can do everything here without using multicursors, just record a macro for one line and repeat with a count and you're set.

Now if you absolutely want the multicursor feature, there are more than one plugin[1][2] for that, but I never felt the urge to use multiple cursors in Vim.

0: https://youtu.be/YR7rRC-vwj4

1: https://github.com/terryma/vim-multiple-cursors

2: https://github.com/paradigm/vim-multicursor

Re:How Did Vim Become So Popular? by Lio on Wednesday July 01, 2020 @04:22AM (#23699174)
There's a couple of ways you could tackle editing that depending on what your json looks like.

For example you could use the visual block mode (ctrl-v) and highlight to the end of the line using $ and then use A to edit the end of every line at the same time. That works with lines of different length.

Or you could use a regex inside of a block selection.

Or there's a multi-cursor plugin available for the same approach as you're currently using in VSCode:

https://github.com/terryma/vim-multiple-cursors

Vim Golf is an excellent resource for seeing how to efficiently edit things in Vim: https://www.vimgolf.com/

Re:How Did Vim Become So Popular? by stewbrew on Wednesday July 01, 2020 @01:12AM (#23697969)
I vaguely remember there is a plugin that does multi line edits - what I never found useful.

I personally would rather use :s (search-replace) for that. Regexp is your friend.