News for hackers. Stuff that matters.
Re:VS Code can do that? by coding123 on Saturday April 21, 2018 @11:45AM (#16891837)
My favorite is multi-selection mode. It's not the same as "Column Mode" you find in other IDEs but try it and you will never want another editor again:

(Option)Alt-Cmd + Down or Up arrow key

From there, you can use arrow keys as normal, but instead of controlling one carrot, you control all the ones you created. So if I use the shortcut key to move to the next word, they all do, same with end of line, or what have you. In the past I had to use regex in my editors to extract quoted text in each line... now I just use multi-select. Each carrot can also be created with the mouse in the Selection menu.

Edit: Apparently it's a feature lifted from Sublime and in other IDEs - thanks!

Re:VS Code can do that? by staticassertion on Saturday April 21, 2018 @11:57AM (#16891903)
This exists in most editors I've used (intellij is alt + shift + click or double tap alt, if I recall). I don't know how people program without this feature - they should teach this shit in school.

Multicursor is amazing.

One thing it's been great for:

I have code like:

byte b1 = array[0];

And I want to do this for 16 bytes. Control + D to duplicate 15x. Double tap control, hit up 15 times - now there's a cursor at every row. Hold control, hit right (jump by word), index to end of 'b1' on each line. Ctrl + shift + a, 'increment duplicates' - now every line is 1, 2, 3, 4... 16. `ctrl + right` my way to the index key and do the same thing.

It's awesome.

edit: I just found 'duplicate and increment' :o

Re:VS Code can do that? by tmerr on Saturday April 21, 2018 @12:24PM (#16892010)
And here I am using for loops like a caveman
Re:VS Code can do that? by alexeldeib on Saturday April 21, 2018 @01:20PM (#16892280)
I think loop unrolling is super cool. We're typically trained in programming to try to eliminate the kind of reuse that unrolling typically looks like, but when applied properly it can work really well. I guess that applies to most tools, though.
Re:VS Code can do that? by tzahola on Saturday April 21, 2018 @01:44PM (#16892372)
Can you give us an example where manual loop unrolling would be desired over a for loop?
Re:VS Code can do that? by alexeldeib on Tuesday April 24, 2018 @03:02AM (#16910493)
What actually brought it to mind was a hardware class where we were instantiating a bunch of the same module but didn't have a programmatic way to do it with our tooling. So...sorta like loop unrolling :)
Re:VS Code can do that? by andrepd on Saturday April 21, 2018 @02:21PM (#16892558)
Languages/compilers that don't do loop-unrolling automatically. If they do, then yes, it's probably best to let the compiler do it.
Re:VS Code can do that? by monkpit on Saturday April 21, 2018 @03:21PM (#16892898)
Like which?
Re:VS Code can do that? by staticassertion on Saturday April 21, 2018 @04:19PM (#16893220)
All of the languages I can think of rely on the compiler backend for loop unrolling, which means it isn't a guarantee. You might want this if you need to guarantee the unroll.

But I wasn't doing this for a loop unroll.

I had a fixed size array that I wanted to serialize/ deserialize to/ from capnproto - there are no fixed size arrays in capnproto, so I wrote a message that just had b1, b2, b3... b16, and then some code to pull it out into a statically sized array. It allowed me to pull a bunch of bytes into a much larger statically allocated array without any allocation or bounds checking.

Re:VS Code can do that? by solipsism on Saturday April 21, 2018 @01:10PM (#16892227)
I don't know how people program without this feature

You must do some kind of programming I've not encountered in my many years. While my editor of choice can do this, I would have no idea how, because a feature like "increment duplicates" is needed so infrequently that I would never remember the shortcut.

Re:VS Code can do that? by monkpit on Saturday April 21, 2018 @03:23PM (#16892907)
That quote was referring to column editing mode, not “increment duplicates” shortcut.
Re:VS Code can do that? by derimagia on Saturday April 21, 2018 @02:05PM (#16892462)
It's one of those things that is incredibly useful. Of course it's possible to survive without it, but it's like saying "I don't know how people program without Copy and Paste". It helps.
Re:VS Code can do that? by khedoros1 on Saturday April 21, 2018 @11:05PM (#16894509)
Copy and paste is something that I use many times a day, even when I'm not programming. Needing to do the same edits over multiple lines doesn't happen often in my code.

There are appropriate plugins available for my editor, but I've never felt the need to install them.

Re:VS Code can do that? by staticassertion on Sunday April 22, 2018 @12:52AM (#16894763)
I don't use this as much as copy/paste, but I absolutely use it daily.
Re:VS Code can do that? by staticassertion on Saturday April 21, 2018 @04:23PM (#16893243)
Increment duplicates is just one of many, many utilities that pairs well with multiple carets.

Formatting is another.

Boilerplate code, for example today I had to implemented code like this:

    impl Into<Node> for ProcessNode {
      fn into(self) -> Node {
        Node {some_boilerplate(self)}
      }
    }
I had 6 node types. Using multiple cursors I:

* Implemented it once

* Duplicated it 5x

* Copied the names of the types I needed to impl for

* Attached a cursor to each impl

* Used ctrl + left/ right to word-jump, and past in the appropriate area

Now it's not like this saved me more than a minute or two, but this sort of thing is extremely common in my experience, across languages.

Re:VS Code can do that? by xfer on Saturday April 21, 2018 @04:55PM (#16893391)
Or you could write a macro?
Re:VS Code can do that? by staticassertion on Saturday April 21, 2018 @06:49PM (#16893790)
That sounds like considerably more work.
Re:VS Code can do that? by kazinator on Sunday April 22, 2018 @02:51PM (#16897815)
In Vim (on Unix), type this into the buffer

  for x in type1 type2 type3 type4 type5 type6 ; do
    cat <<!
      impl Into<$x> for ProcessNode {
        fn into(self) -> $x {
          $x {some_boilerplate(self)}
        }
      }
  !
  done
Now do a line-oriented visual select of all the lines from for to done (using V + cursor movements). Then pipe to shell with !sh[Enter].

Poof! The above is replaced with the following:

      impl Into<type1> for ProcessNode {
        fn into(self) -> type1 {
          type1 {some_boilerplate(self)}
        }
      }
      impl Into<type2> for ProcessNode {
        fn into(self) -> type2 {
          type2 {some_boilerplate(self)}
        }
      }
      impl Into<type3> for ProcessNode {
        fn into(self) -> type3 {
          type3 {some_boilerplate(self)}
        }
      }
      impl Into<type4> for ProcessNode {
        fn into(self) -> type4 {
          type4 {some_boilerplate(self)}
        }
      }
      impl Into<type5> for ProcessNode {
        fn into(self) -> type5 {
          type5 {some_boilerplate(self)}
        }
      }
      impl Into<type6> for ProcessNode {
        fn into(self) -> type6 {
          type6 {some_boilerplate(self)}
        }
      }
Don't write in some insane boilerplate-driven programming language in the first place. Anyone proliferating this kind of duplication (whether with multi-cursors or shell here docs or any other way) should be flogged.
Re:VS Code can do that? by khedoros1 on Saturday April 21, 2018 @11:08PM (#16894516)
I've got to agree. It's one of those things that could save me a minute of manual editing every once in a long while, but I feel like there have to be lower-hanging fruit, in terms of increasing my editing speed.
Re:VS Code can do that? by Groxx on Saturday April 21, 2018 @12:27PM (#16892019)
Yea, I have the mac bindings, and for me it's opt-click to add a cursor, and ctrl-g to select next duplicate of whatever I select. You can even do stuff like "move left 1 word" or "move to begin/end of line" and all cursors follow the same behavior, possibly with different movement-distances. Utterly trivial refactorings in any language (including english), and always feels far easier to understand / predict than Vim's record-and-replay (as useful as it is), especially since you can see the results in every instance as you type.

I've been using it in editors since well before VSCode existed. It's very nearly a requirement for me to use an editor now.

Re:VS Code can do that? by philsnow on Sunday April 22, 2018 @12:08AM (#16894662)
I end up doing everything like that in emacs keyboard macros.

For your example, this is the kmacro I ended up with:

    ;; Keyboard Macro Editor.  Press C-c C-c to finish; press C-x k RET to cancel.
    ;; Original keys: 2*M-f M-DEL b C-x C-k TAB C-a 2*C-k 2*C-y C-p
    
    Command: last-kbd-macro
    Key: none
    
    Macro:
    
    2*M-f                   ;; forward-word
    M-DEL                   ;; backward-kill-word
    b                       ;; self-insert-command
    C-x C-k TAB             ;; kmacro-insert-counter
    C-a                     ;; move-beginning-of-line
    2*C-k                   ;; kill-line
    2*C-y                   ;; yank
    C-p                     ;; previous-line
(after you've created a macro, you can get the above by doing M-x kmacro-edit-macro.)
Re:VS Code can do that? by kazinator on Sunday April 22, 2018 @01:29AM (#16894839)
In Vim: just write the first line. Then iterate on this key sequence:

  YP<Ctrl-A>l<Ctrl-A>
That can be recorded into a register (say "a") with

  qaYP<Ctrl-A>l<Ctrl-A>q
then replayed with @a, and further repeated with @@.

This is nothing new; I was doing this in Vim 20 years ago.

Re:VS Code can do that? by staticassertion on Sunday April 22, 2018 @12:12PM (#16896785)
I'm sure it's not new.
Re:VS Code can do that? by billfruit on Saturday April 21, 2018 @02:23PM (#16892575)
I haven't tried this feature on VS Code, but isn't the record and replay macros feature in Emacs(normally bound to F3 and F4) a simpler(and more general, since seemingly it can record arbitrary text transformations) approach to achieve a similar effect?
Re:VS Code can do that? by SiVal on Saturday April 21, 2018 @05:40PM (#16893589)
Where did you find "duplicate and increment"?
Re:VS Code can do that? by staticassertion on Saturday April 21, 2018 @06:49PM (#16893788)
It might be a plugin called String Manipulation.
Re:VS Code can do that? by matthewmacleod on Saturday April 21, 2018 @11:52AM (#16891871)
I'm not sure where this feature originated, but it's been a core feature of Sublime for as long as I've used it, and is also broadly available in other editors. Definitely not a VS Code-specific thing, though it's still great!
Re:VS Code can do that? by JonathonW on Saturday April 21, 2018 @02:25PM (#16892589)
I doubt it's where it originated, but I was using multi-select/multi-caret editing in jEdit before Sublime came around.

That and arbitrary window splitting (split any pane horizontally or vertically) are two features I really miss when they're not present. The latter's surprisingly hard to find; Sublime and VS Code don't do it (Atom does).

Re:VS Code can do that? by jobigoud on Saturday April 21, 2018 @12:08PM (#16891945)
I believe Sublime introduced the feature. I looked it up and it was already present in the first release on January 18, 2008.
Re:VS Code can do that? by dexterdog on Saturday April 21, 2018 @12:38PM (#16892086)
Column editing? That was in a shareware text editor that I wrote for Windows in the mid 90s and I didn't invent it.
Re:VS Code can do that? by ehsankia on Saturday April 21, 2018 @03:05PM (#16892808)
Editors like n++ have column editing, but Sublime takes it to a different level. It's most generalized, it's multi-cursor.

You can for example put your cursor on a word, spam ctrl+d (of press alt+f3) to select all instances of that word, and start editing them all at ones, no matter where they are. They don't have to be perfectly in a column. You can also copy X items from anywhere, and if you have X cursors again somewhere else and paste, they'll each go in their respective slot.

Re:VS Code can do that? by setr on Saturday April 21, 2018 @01:04PM (#16892206)
multi-selection; multiple cursors, arbitrary (non-contiguous) columns, arbitrary rows.
Re:VS Code can do that? by parmesan on Saturday April 21, 2018 @11:49AM (#16891855)
I believe it is the same functionality as in Sublime, IntelliJ, Eclipse and others. It's a total lifesaver when editing bulk stuff, like a lot of constants and what not! One of my must-haves in an IDE.
Re:VS Code can do that? by paol on Saturday April 21, 2018 @11:57AM (#16891902)
Not eclipse, much to my annoyance.
Re:VS Code can do that? by didibus on Saturday April 21, 2018 @12:02PM (#16891921)
It does exist on eclipse.

Alt + Shift + A

Re:VS Code can do that? by unhammer on Saturday April 21, 2018 @12:34PM (#16892058)
In Emacs this feature is called multiple-cursors: http://emacsrocks.com/e13.html – Emacs got it after Sublime. But I find I typically get things done faster with keyboard macros, or maybe I'm just too stuck in my ways :)
Re:VS Code can do that? by ben-schaaf on Saturday April 21, 2018 @12:14PM (#16891972)
Sometimes I have 2 alternatives for a piece of code, one commented out and the other not, and I'd like to toggle between the two. With multicursor I just select both and hit `ctrl-/` to toggle them on and off.

I've had issues in IDEs where they have shitty implementations of multicursor that break that example.

Re:VS Code can do that? by ccharles on Saturday April 21, 2018 @12:34PM (#16892054)
Re:VS Code can do that? by nickjj on Saturday April 21, 2018 @12:22PM (#16892001)
I recently discovered a vscode extension literally called "Insert Cursor at Beginning of Each Line Selected".

Super handy when you want multiple cursors to line up at the start of every line (by default vscode will wrap the cursor around to the end of the longer lines if you try to move around with the arrow keys).

There's about 20 other extensions I routinely use, which I documented at https://nickjanetakis.com/blog/switching-to-vscode-from-subl....

Re:VS Code can do that? by 0x6c6f6c on Saturday April 21, 2018 @12:35PM (#16892073)
What's this do differently from just pressing Home with multicursors?
Re:VS Code can do that? by kwood on Monday April 23, 2018 @03:52AM (#16901332)
It saves a whole keypress (because you need to go into multicursor mode first, then press Home / Ctrl+A to get to the beginning of the line.

Additionally, there is a case where if you don't select the last line until the end, you end up in a state where the last cursor is already at the beginning. If you press "Home" now, you end up having that last cursor at the beginning of the line - and the other ones at the indentation level (see https://github.com/Microsoft/vscode/issues/14919#issuecommen...)

(Disclaimer: Author of the mentioned plugin, so I am heavily biased against my own workflow and saving-keypresses-obsession :P)

Re:VS Code can do that? by nickjj on Saturday April 21, 2018 @12:42PM (#16892110)
> What's this do differently from just pressing Home with multicursors?

Pressing home doesn't always put you at the true beginning of the line. It will put you at the first non-whitespace character.

Re:VS Code can do that? by sellweek on Saturday April 21, 2018 @01:00PM (#16892187)
If you press it twice, it will put you at the beginning of the line.
Re:VS Code can do that? by nickjj on Saturday April 21, 2018 @01:07PM (#16892218)
Damn. Now I regret never taking a typing class.

It only took using a keyboard for about 22 years to learn pressing home more than once has benefits.

Thanks!

Re:VS Code can do that? by tenryuu on Saturday April 21, 2018 @11:53AM (#16891881)
Visual Studio gets this feature soon, otherwise I use it all the time in sublime

https://visualstudio.uservoice.com/forums/121579-visual-stud...

Re:VS Code can do that? by arbie on Sunday April 22, 2018 @02:06AM (#16894929)
VS Code certainly makes VS look really bad in some areas.
Re:VS Code can do that? by 4umfreak on Monday April 23, 2018 @10:15AM (#16903548)
VS does that all by itself.
Re:VS Code can do that? by SSLy on Saturday April 21, 2018 @03:48PM (#16893048)
V to for block-visual highlight, then do your motion, hit esc and I'll be repeated on every line
Re:VS Code can do that? by andrepd on Saturday April 21, 2018 @02:19PM (#16892549)
Sublime's multicursor works the same way, I believe.
Re:VS Code can do that? by marssaxman on Saturday April 21, 2018 @02:57PM (#16892751)
(I think you mean "caret" and not "carrot".)
Re:VS Code can do that? by conistonwater on Saturday April 21, 2018 @12:27PM (#16892021)
carrot?
Re:VS Code can do that? by humanrebar on Saturday April 21, 2018 @12:41PM (#16892105)
caret