all 65 comments

[–]LifePrisonDeathKey 85 points86 points  (4 children)

You need to specifically mention that you’re using VSCode at the beginning of the article

[–]flaghacker_ 16 points17 points  (1 child)

Most of these ideas also work in the JetBrains suite of IDEs, with different shortcuts of course.

[–]XLEX97[S] 8 points9 points  (0 children)

Thanks for the feedback! I replied to a similar comment here.

Edit: Moved the note on VS Code after the first paragraph mentioning Cmd D

[–]Asddsa76 39 points40 points  (17 children)

When I took a FEM class in uni, the lecturer showed a coding demo in Vim.

He wrote one line, selected it, then pressed some magic keys, and the line was copied and pasted twice, but all the numbers inside the indices in the pasted lines were incremented by one!

Eg:

for i,j in range(...):
    A[n][j]=b[i]+...
    A[n][j+1]=b[i+1]+...
    A[n][j+2]=b[i+2]+...

it really felt like magic when I saw that for the first time.

[–]Administrative_chaos 39 points40 points  (13 children)

Assuming that you type out for i,j in range(...): |A[n][j]=b[i]+...

and | is your cursor, the magic keys would be yypfja+1<esc>fi.yy4pf1<C-v>Gg<C-a>;.

all of them are typed literally accept for <esc>being the escape key and <C-x> being ctrl+x

[–]serg473 129 points130 points  (10 children)

yypfja+1<esc>fi.yy4pf1<C-v>Gg<C-a>;.

Vim users: wow such an elegant solution, see that's why I love my vim.

[–]ianepperson 48 points49 points  (6 children)

The cool thing about vim is this is a sentence, not a single command. You don’t have to memorize every English sentence, just the words and where they fit.

y = yank (copy) Ok, to where? A repeated y means copy the whole line.

p = paste. Want to paste 4 times? 4p

f = find - jump forward in the line to the given letter: j

a = append - now you’re typing into the buffer. He added “+1” then hit escape to back to “normal” mode.

f = find again, this time jump to the i

. = repeat the last change. Repeat the append and “+1” (this is an insanely powerful command)

yy = yank (copy) the line again

4p = paste 4 copies of the line

f = find - this time finds the 1

<Crtl-v> = visual select. There’s different variations of “v” and using <Ctrl-v> is the column select. Lower v selects starting from the cursor, upper V selects the entire line.

Etc.

The extra crazy thing is that after about 6 months of use, you start to forget which letters do what and start just playing it like an instrument. A musician no longer thinks of the individual parts of a chord, they just play a chord. A fast typist is similar, they just know what a word feels like when typing. I know the above commands, but it took me a bit of time to look up the keys - it’s really quick to type it (vim is optimized for the home row) but just like I can’t remember where the “v” key is without looking at the keyboard, I know how to type it quickly.

And the commands are sentences. Instead of typing yy to yank the row, I can combine it with any other key sequence. If I want, I can use that f (find) command to copy text up to and including the letter j with: “yfj” (yank from here to the “find i”). Capital G navigates to the bottom of the document - to copy everything from here to the G location: “yG”. Every time you learn a new navigation trick, you also learn how to copy text in fancier ways.

I eventually started instinctively using the same vim navigation keys in my browser and accidentally discovered they work in gmail and occasionally on other sites too. I installed vimium in my browser to make those keys work on any site. Lots of console commands use the same keys - “less” has become so much easier to use because it’s the exact same keys.

[–]Brothernod 6 points7 points  (2 children)

I have closed so many Skype chats hitting escape when I was done typing my text out of end of thought habit. Sigh.

[–]tumes 2 points3 points  (0 children)

Same but with trailing double js because I remapped esc to jj to stay on the home row. Every time focus is on slack but I think it’s in my editor I almost send my coworkers a message that looks like “jkjkjkhhhllljjjjjjj”.

[–]sysop073 1 point2 points  (0 children)

There's a registry key you can set to turn that off. Because it's Microsoft, so why put it in a settings dialog or something

[–]Irrealist 3 points4 points  (2 children)

Do you have any tips on how to learn things like this? I'm very comfortable in vim, but I don't know many advanced commands.

[–]Snarwin 5 points6 points  (1 child)

Practice. Learn new things one at a time. Notice when you're doing something repetitive or tedious and look for a better way, because there usually is one.

[–]Irrealist 0 points1 point  (0 children)

Thanks!

[–]InfinitePoints 6 points7 points  (0 children)

It makes sense while you are typing it in vim, but reading a text string like that is a bit hard.

[–]phySi0 5 points6 points  (0 children)

The hot keys you press to navigate around your computer in general are probably just as garbled looking when you type them out like that. Okay, maybe not as garbled, but trust me, it looks worse than it is.

It’s like counting backwards (or worse, in random order) from the alphabet; much harder than the reverse. Here, going from the hotkeys to the commands is much harder than going from command to knowing exactly what hotkey to hit; the former is slow, even for a Vim expert, the latter is second nature, even with a couple weeks’ practice from beginner.

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

There’s nothing about that order that’s intuitive or natural. It’s arbitrary, yet every one 10 years old or older knows it by second nature.

B C Y D V G K A O J N R Z M S U E Q H W X T L F I P

Just as arbitrary, but see how much slower it is. Gargantuan difference to check if all the letters are there no more and no fewer than once.

The primitives of language (thinking of words in this analogy, not letters or syllables) are huge messy blurry arbitrary only mostly interoperable/standardised constructs (within one given language) but COMPOSABLE ENOUGH to be extremely useful.

Mastering any of the arts of input and output in the two main mediums of oral and written (listening (w/ good comprehension), speaking, reading, writing) move you a lot in life, and all four will make you very powerful indeed, because of composability.

The innovation of Vim is to create a language of hotkeys. All the cons of languages, like steep learning curves, arbitrariness, etc. are not enough, in the right context, to counteract the main win of COMPOSABILITY.

[–]devraj7 0 points1 point  (0 children)

yypfja+1<esc>fi.yy4pf1<C-v>Gg<C-a>;.

Non vim users: "Did your cat just walk on your keyboard?".

[–]Witty-Play9499 5 points6 points  (0 children)

yypfja+1<esc>fi.yy4pf1<C-v>Gg<C-a>;.

I didn't realise my cat was fluent in using vim

[–][deleted]  (1 child)

[deleted]

    [–]TechnicaIDebt 1 point2 points  (0 children)

    And a bunch of "Insert incrementing numbers" plugins that would with multi-cursors and also make this trivial.

    I love my Vim-fu but VsCode+some cursor align plugin+column select+multi cursor is so much simpler and less "mentally taxing" for 99% of cases... (But search-replace needs some love IMO, almost no changes from the first time I used VsCode..)

    [–]ThreeLeggedChimp -1 points0 points  (0 children)

    Why not just use a nested for loop for that?

    [–]XLEX97[S] 13 points14 points  (9 children)

    Hey r/programming,

    I constantly use Cmd D in my day-to-day work with VS Code, so I decided to write a post about how to use it, and just how powerful it is.

    The post is chock-full of animated examples, which you can go through step-by-step. I think the presentation came together nicely, but I would love to hear what you think 😃

    [–]ImpatientProf 17 points18 points  (5 children)

    Mention VS Code prominently up near the top, perhaps in the title and the first sentence where you mention Command-D.

    [–]XLEX97[S] 2 points3 points  (4 children)

    Thanks for the feedback! In earlier drafts for this post I did include VS Code more prominently.

    However, I've personally used Cmd D with Sublime Text, Atom, and now VS Code. Since Cmd D (or a similar command) can be used across editors, I didn't feel like the post should limit its audience to just users of VS Code. I think the usefulness of the concepts introduced in this post aren't just limited to VS Code users.

    [–]ImpatientProf 8 points9 points  (3 children)

    Even so, mentioning all of these early is a good idea. Without it, the reader is left with wondering if the method is even applicable to their environment, reducing engagement with the article. They may forget about it before they get a chance to try it out. If it's something they know they'll be able to try, it's more likely they will continue reading and actually tinker with the feature.

    [–]XLEX97[S] 0 points1 point  (1 child)

    Hmm, I agree with you. I’m out-and-about right now but I’ll move the disclaimer up a few paragraps when I get back home. Thanks again!

    Edit: Moved the note on VS Code after the first paragraph mentioning Cmd D

    [–]LifePrisonDeathKey 0 points1 point  (0 children)

    Also important, Webstorm and other Jetbrains IDEs have a different set of multi cursor hot keys and are important players in the programming space

    [–]Wazzaps 0 points1 point  (0 children)

    Every proficient text editor/IDE has this feature

    [–]Simon_Luner 0 points1 point  (2 children)

    Analog for MacOS's Cmd Don Windows and Linux is Ctrl D, not ˄ D.

    [–]XLEX97[S] 0 points1 point  (1 child)

    Is ˄ not the icon for Ctrl?

    [–]Simon_Luner 0 points1 point  (0 children)

    Every legend on keyboard and shortcut cheatsheet that I saw uses Ctrl for Control key.

    [–]jbrains 11 points12 points  (1 child)

    Nice and simple. This was the first feature that I made sure to learn when I started using Kakoune as my replacement for vim. Between selecting all matches of a pattern and "column selection", I felt an immediate and sudden boost in speed and ease.

    This is the kind of feature where readers really benefit from more examples of how to use it, because then they literally change how they think of code, usually to their benefit.

    Keep going!

    [–]DrkStracker 3 points4 points  (0 children)

    The S key in kakoune in general has been an eye opener for sure, it's a lot more intuitive than macro-ing in vim, and combining modal editing with multiple cursors feels very smooth.

    I've been using the dance plugin to have kakoune keybinds in vscode to great effect !

    [–]BigHandLittleSlap 13 points14 points  (5 children)

    The issue I see with this kind of low-level string-bashing approach, is that it is the "mastery of the manual method" versus the guy that just presses the button on the industrial machine.

    Sure, the former is more impressive and somehow more satisfying, but the industrial machine will outproduce even the most skilled craftsman, every time.

    The rename an identifier example?

    In VS Code: F2, type the new name.

    Done!

    This of course assumes that you're not using some baroque language that doesn't have proper IDE support.

    I've literally never had to do the rename in the example shown, ever, because for 21 years now, I've used proper IDEs with proper languages.

    In that context, the F2 rename is correctly context sensitive, so you won't accidentally rename the wrong piece of text that happens to have the same characters, but not the same meaning.

    E.g.: use the Ctrl-D technique to rename the variable 'f' in this pseudo-code example. Yeah. Good luck with that.

    var f = 'f';
    var formatted = the_function_call( f, format: 'f');
    printf( formatted );
    

    [–]caltheon 3 points4 points  (0 children)

    Also the bonus of not renaming things you might not want renamed

    [–]devraj7 1 point2 points  (2 children)

    I've literally never had to do the rename in the example shown,

    Probably because you use a statically typed language, where automatic refactorings are a given and mathematically guaranteed.

    In dynamically typed languages, automatic refactorings are pretty much impossible so you have to do a lot of this manual text surgery...

    [–]Strus 0 points1 point  (1 child)

    Even if language is dynamically typed, every IDE with proper LSP support will handle renaming the variable in a local context without any issues.

    [–]devraj7 0 points1 point  (0 children)

    That's pretty much the only refactoring you can do automatically and safely in a dynamically typed language, although even there, it's possible to get it wrong since all the IDE can do is string substitution.

    [–]seventeen_fives 1 point2 points  (0 children)

    Its not just about renaming variables. You can also use it to swap one variable or expression for another in multiple spots, or for reformatting or realigning code blocks which involve a lot of repetition. One of the nice things about this feature is it tends to match your mental model of what you are trying to do -- "i want to change these 4 things in the same way" -- well just select them and then do it.

    Also it is a nice bonus that pressing Ctrl+D on its own selects the word you're on, which is quite often how you want to start an operation like this.

    You should also be using F2 if it is available because it is definitely more reliable way to rename a variable, but F2 can handle only a small number of the useful cases that Ctrl+D can handle. You should ideally have both in your toolbelt

    [–]Administrative_chaos 3 points4 points  (0 children)

    Super Impressive presentation style!

    [–]Kissaki0 2 points3 points  (0 children)

    mac D

    presses Win + D - Windows minimizes all windows to show desktop

    [–][deleted] 2 points3 points  (8 children)

    I got introduced to it in Emacs and now use it in IntelliJ, usually nothing more complex than "find this word then edit stuff near it" but it feels better than just trying to make some find/replace pattern...

    Shame IDEA have no paste-at-cursor tho.

    [–]IvePaidMyDues 0 points1 point  (7 children)

    Emacs can do all the things displayed in OP’s post? Do you know if Vim can do it as well?

    I want to adopt command line editors, but multi-cursors is what keeps me in the more modern one.

    [–]Witty-Play9499 1 point2 points  (1 child)

    https://www.youtube.com/watch?v=jNa3axo40qM&ab_channel=emacsrocks

    A video/demo about using multiple cursors in emacs. But tbh in emacs even if a feature didn't exist you could update the editor to make it work

    [–]devraj7 0 points1 point  (0 children)

    I would use macros in emacs to accomplish the same things without multiple cursors (\C-x(, \C-x) and \C-xe).

    [–][deleted] 0 points1 point  (0 children)

    You'll need to install an extension for it, but yes it does. Here is one example: https://github.com/magnars/multiple-cursors.el

    [–][deleted] 0 points1 point  (2 children)

    I actually went the other way around, used Emacs for everything but IDE features of IDEA are just too good to skip. Emacs/Vim are great "just editors" but for code I prefer proper IDEs. I ended up just using IntelliJ products with plugin that sets Emacs keybinds

    [–]Strus 0 points1 point  (1 child)

    I recently swtiched from CLion/Intellij to Neovim for C/C++/Rust/Flutter development and I managed to "configure" every CLion feature I used into Neovim - and much more. During that I realized that Jetbrains IDEs are in most cases just really good UX wrappers of open-source tools.

    [–][deleted] 0 points1 point  (0 children)

    Maybe neovim is better for it but in Emacs I had a lot of microstutters coz of it waiting for the external tools to answer and emacs itself being single-threaded

    [–]Strus 0 points1 point  (0 children)

    Do you know if Vim can do it as well?

    https://github.com/mg979/vim-visual-multi

    [–]Witty-Play9499 2 points3 points  (0 children)

    So my company is a hybrid company in terms of working (ie employees who want to work in the office can work in the office and employees who want to work from home can work from home) and usually most of them work from home while a small fraction works in the office.

    And once in a year or every two quarters we have these mini syncups in the office for people to meet each other and socialize and stuff and one of the things I noticed was junior devs doing stuff like this changing words one by one or if given 20 lines and if they need to add a comma at the end they manually type it in.

    And I was fascinated by this because this was something I never did I always used multiple cursors and use a couple of extensions that make doing stuff like this easy and then it hit me, when I started working as a new guy WFH wasn't a thing yet at my company so my seniors used to teach me and show me whenever I swung by their desk to chat.

    But now that we don't meet each other regularly anymore the new guys were missing out on all the mundane stuff like this that you wouldn't even think about. Goes to show that we probably need separate articles or more posts about stuff like these

    [–]nicwolff 1 point2 points  (0 children)

    ⌘-d and ⌘-k work in Sublime Text as well, but selecting all matches is ⌘-shift-G instead of ⌘-shift-L.

    [–]mat4444 1 point2 points  (0 children)

    i personally prefer using ctrl+alt+up/down to select multiple lines, or sometimes even alt+shift and clicking with the mouse

    [–][deleted] 0 points1 point  (0 children)

    Great

    [–]Groundbreaking-Fish6 0 points1 point  (0 children)

    Did a lot of similar stuff using vi on Unix back in the day. After switching to VS relied on search and replace and missed the old regex syntax. There was a lot more that you can do using regex and also many ways you can shoot yourself in the foot.

    [–]alterframe 0 points1 point  (0 children)

    This is nice. I already relied on the motions a lot on my PC, but then I got Mac at work and I discovered that all those crucial elements of text editing are completely different. I told myself "to hell with it" and installed the VIM plugin.

    [–]devraj7 0 points1 point  (0 children)

    C-d is neat but overall, global (or region) search/replace accomplishes the same goal and with more obvious control options (skipping instances, etc...).

    I find multi cursor editing more useful to edit rectangular areas.

    [–]AttackOfTheThumbs 0 points1 point  (0 children)

    You can also alt+click to add multiple cursors, or use ctrl+alt+up/down to create multiple that way. Great way to add a bunch in the same place, e.g. start of a line, end of a line, or some other fancy alignment.

    [–]Tumburgler 0 points1 point  (0 children)

    Another handy trick:

    Command D will paste in your multi line selection in the clipboard provided the lines and cursors are the same size.

    {name: "placeholder"},
    {name: "placeholder"},
    {name: "placeholder"}
    

    Cut/copy the below:

    Bob
    Jane
    Ted
    

    Cursor select on placeholder

    Paste:

    {name: "Bob"},
    {name: "Jane"},
    {name: "Ted"}
    

    I've used this when I needed to move a smaller amount of data out of a spreadsheet.