Of vim and other text editor hackery

• I'm still somewhat nonplussed by this attitude. Did a regular expression touch you when you were a child or something?

Shuddap I DONT WANNA TALK ABOUT IT

What difference does it make whether you fix something using regexes or not?

Speed, ease of use, security.

When advanced editor features - be it smart multi-cursor or vim macros or Visual Studio refactoring - fails to solve the problem, regex is the last, desperate solution. The fact that you keep reaching for it tells me you're either a regex wizard (less likely) or have a very inefficient workflow.

But whatever, I'm fine with that. Just don't tell me vim is da shit, and then reveal you do most of your editing through regexes, which any text tool (including fucking sed) has.

• you're either a regex wizard

Thanks. I guess we'll just have to agree to disagree on this. I still can't shake the feeling this is like an electrician saying "screwdrivers, bullshit, we don't need no fucking screwdrivers", but I guess if you have a workflow which is working for you, that's fine.

sed is the bomb though—there is literally nothing it cannot do...

• sed is the bomb though—there is literally nothing it cannot do...

Pedantically speaking, there's one thing that sed definitively can't do: write its results back to the file they came from. You have to write to another file and then move it back over the old file. This is because it keeps as much of the data directly in the file (or the pipe/preceding program) as possible, rather than loading masses into itself.

But it does mean it can work on multi-gigabyte files, no problem at all.

• This is because it keeps as much of the data directly in the file (or the pipe/preceding program) as possible, rather than loading masses into itself.

It is, after all, a stream editor....

• Thanks. I guess we'll just have to agree to disagree on this. I still can't shake the feeling this is like an electrician saying "screwdrivers, bullshit, we don't need no fucking screwdrivers", but I guess if you have a workflow which is working for you, that's fine.
I think it's more like a carpenter saying "When I cut wood, I use a saw; when I nail wood together, I use a hammer. I only reach for the Giant Boulder of Doom in dire emergencies." If you find yourself reaching for the Giant Boulder a lot, maybe it's because your hammer is a glass bottle and your saw is a frayed shoestring.

• Emacs. Come over to the dark side

Zawinski Emacs or Stallman Emacs?
Emo Philips - Golden gate bridge (1987, official sub ita) – 03:22
— vazcomics

• Alright then, in the interests of sharpening the saw. Here's a real llife example of how I'd use a regex. I'm genuinely interested to hear about alternative solutions to this, so I can get some idea of what I'm missing out on.

Let's say I have this (highly imaginative) enum:

enum SomeEnum {

ENUM_VALUE_0,
ENUM_VALUE_1,
ENUM_VALUE_2,
ENUM_VALUE_3,
ENUM_VALUE_4,
};


(Aside: fucking hell, now indentation in code blocks is fucking broken you fucking discoidiots. For the love of God, HIRE SOME FUCKING QA!)

And I want to write a function which switches on it. So I would probably write a function outline and paste the enum body into it.


void some_function(SomeEnum se) {

switch(se) {
ENUM_VALUE_0,
ENUM_VALUE_1,
ENUM_VALUE_2,
ENUM_VALUE_3,
ENUM_VALUE_4,
}
}


(Again, seriously, why the fuck can't we have nice things? FFFFUUUUUU)

So, go into Visual mode, highlight the enum values, run something like this:
:'<,'>s/$$[A-Z_0-9]\+$$,/ case \1: {\r break;\r }\r/gc
And boom, there's my function outline. Now I just need too fill in the interesting bits:

void some_function(SomeEnum se) {

switch(se) {
case ENUM_VALUE_0: {
break;
}

case ENUM_VALUE_1: {
break;
}

case ENUM_VALUE_2: {
break;
}

case ENUM_VALUE_3: {
break;
}

case ENUM_VALUE_4: {
break;
}

}

}


(OK, so now indenting sort-of partially works, but still, not well enough to avoid completely fucking up my example. You fucking clowns!)

I guess you'd need to check Raw if you want to see the indentation I intended. Anyway, there's a real world example of saving myself some boring typing—anyone have some non-regex solutions to this?

• Block cursor before the first column. type case . Block select the commas, type : { break; }. Hit CtrlK + CtrlD.

Done. And it's all visually intuitive and doesn't require bizarre line noise commands.

• I am clearly Doing it Wrong.

Presumably I need to use Ctrl+V (or Ctrl+Q if you have Windows-mode enabled) to get into block select.

So I'm in VISUAL BLOCK mode, if I then type case it deletes the first E, and inserts ase. I'm pretty sure I've missed something, but it's not clear what I missed...

Edit: I figured it out! Ctrl+Q I case Esc. Inserts case at the start of every line. Onto the next part...

Block select, s: { break; }Esc also works for the end of the line.

CtrlK + CtrlD does nothing for me though, it's possible something in my .vimrc is preventing it though. :h ^K^D gives me nothing, :h ^K tells me about digraphs, which is unlikely to be relevant...

On balance, I'm not 100% there yet with it, but this is probably a legitimate approach, if I can figure out the last part...

It's probably going to take me a lot of reprogramming to make use of it rather than doing it the way I've been doing it the last 15 years or so.

• I'm pretty sure I've missed something, but it's not clear what I missed...
What you missed was that I was using Visual Studio. Or MonoDevelop, or WebMatrix, or Sublime, or XCode, or any other Windows or Mac IDE.

The next part for Vim is CtrlQ, C, : { break; }. Looks like you got it covered.

The final part is language-sensitive prettification ("Format Document" in the editors mentioned). I don't know how to do that in Vim, but I think there's a way to set a macro to pipe the current buffer through an external app and then back into the current buffer. You could probably assign it to CtrlK since digraphs and trigraphs haven't been relevant since the late 80's.

• @TwelveBaud said:
What you missed was that I was using Visual Studio. Or MonoDevelop, or WebMatrix, or Sublime, or XCode, or any other Windows or Mac IDE.

<s>The next part for Vim is <kbd>Ctrl</kbd><kbd>Q</kbd>, <kbd>C</kbd>, <tt>: { break; }</tt>.</s> Looks like you got it covered.

The final part is language-sensitive prettification ("Format Document" in the editors mentioned). I don't know how to do that in Vim, but I think there's a way to set a macro to pipe the current buffer through an external app and then back into the current buffer. You could probably assign it to <kbd>Ctrl</kbd><kbd>K</kbd> since digraphs and trigraphs haven't been relevant since the late 80's.

What you missed was that I was using Visual Studio. Or MonoDevelop, or WebMatrix, or Sublime, or XCode, or any other Windows or Mac IDE.

<s>The next part for Vim is <kbd>Ctrl</kbd><kbd>Q</kbd>, <kbd>C</kbd>, <tt>: { break; }</tt>.</s> Looks like you got it covered.

The final part is language-sensitive prettification ("Format Document" in the editors mentioned). I don't know how to do that in Vim, but I think there's a way to set a macro to pipe the current buffer through an external app and then back into the current buffer. You could probably assign it to <kbd>Ctrl</kbd><kbd>K</kbd> since digraphs and trigraphs haven't been relevant since the late 80's.

Umm. OK Discourse, if you want to nest a quote inside itself, who am I to stop you...

I actually dont mind Ctrl + K, it lets you enter characters such as these into your source code:

à:   Ctrl+K a 

Ö:   Ctrl+K O :

¼:   Ctrl+K 1 4

»:   Ctrl+K > >


It's not particularly useful, but it's fun...

• Ahh, a Compose key. As an American I don't really care about those, but for certain European countries and a lot of Asian countries it's quite useful. Which is why it's a part of their keyboards already.

• Pedantically speaking, there's one thing that sed definitively can't do: write its results back to the file they came from.
sed s/foo/bar/ -i thing.txt?

• Still creates a new file.

• It does. Do you want it to avoid creating a new file just when the replacement is the same length? Otherwise there's no choice.

sed s/foo/barrrrrr/ -i thing.txt almost certainly has to rewrite the whole file... with any tool.

• Oh, I use sed plenty, often enough with that flag. It's just something to be aware of: Ownership and permissions may change, and any other hardlinks to that file will not be updated.

If that's a problem then you do need to use a second command, rather than sed's -i flag, to write the data back to the original file.

• Good points. All around.

• Still creates a new file.

On Windows, unxutils sed -i (or is it gnuwin32 sed? I honestly can't remember which ports collection my usual sed got pilfered from) copies the original file to a randomly-named temporary in the current directory, overwrites the original file with sed's output, then fails to clean up the temporary.

• Otherwise there's no choice.

Well, you could shorten the file too (all major operating systems support truncation) but it's awkward. It's easier to teach programming noobs that the way to actually do text editing is to rewrite the file from scratch when you save; it's one heck of a lot easier to get right and make reliable.

• Pedantically speaking, there's one thing that sed definitively can't do: write its results back to the file they came from.

$echo foo > bar$ cat bar
foo
$sed -i s/foo/bar/ bar$ cat bar
bar


Insert rant about how it's the same damn file to me.

• It appears to be. But it isn't. That's what we call a timebomb, as explained before.

• What shit-grade server host do you use that doesn't allow ssh/scp?

If you have access to the Lounge, just read it in there. Meanwhile all I will say is... Windows dedicated servers that we run. The problem is not the hosting company but a "people problem".

• It appears to be. But it isn't.

Yeah, that's what I said. It's good enough that it is. You're just arguing at a lower level that I'm not terribly interested in.

• Num Lock is one of those keys that exists merely so that when it's pressed by accident it makes your computer look broken. There's a surprisingly large amount of those keys.

Num Lock is really an 83/84 key compatibility holdover...I'd drop it, but that'd probably break something in some crappy piece of BIOS code from the 80s or w/e...

• You're just arguing at a lower level that I'm not terribly interested in.

Hmm. I may have to "borrow" this...

• Num Lock is one of those keys that exists merely so that when it's pressed by accident it makes your computer look broken. There's a surprisingly large amount of those keys.

Unless you have mousekeys enabled in the Accessibility section of the Control Panel, then Num Lock toggles whether or not you can use the keypad to move the mouse cursor.

• But regular expressions are more like a magnifying lens than a giant boulder.

• But regular expressions are more like a magnifying lens than a giant boulder.

A magnifying boulder?

• I solve this by not using any plugins in the first place. [...]
But I'd gently suggest that 400MB+ files are into Doing It Wrong territory...

Standard shitty software solution for its 'impossible problems': don't do it.

Huh?
[...]
Oh wait you said regex mode:

Maybe I wasn't clear: I don't care anymore. They had their chance, they blew it, case closed.

• If the editor is set to spaces, you typo one character (tab), then you have to hit backspace 4 times to delete it.

At least in ViM, no, you don't. When tab inserts spaces (expandtab), the backspace () dedents, i.e. when used in place where there are only spaces from start of line to cursor it removes one indent (softtabstop) worth of spaces.

Ahh, a Compose key. As an American I don't really care about those, but for certain European countries and a lot of Asian countries it's quite useful. Which is why it's a part of their keyboards already.

As European I obviously have keyboard layout such that I can type the graphemes of my native language without needing compose. But I've used ViM's compose when I wanted mathematical expressions in comments using unicode math (Doxygen supports MathJax, but with TeX escapes it's mostly unreadable in the raw form, so I wanted to use unicode where possible).

• For example, Windows Vista/7 command prompt doesn't have tab-completion.

Huh??? I use it every single day.
(If I remember, I think it was turned off by default in Vista. Or maybe that was XP. A registry bit had to be toggled.)

• Implemented in Windows NT 3.5, on by default until Windows 2000 and from Windows Vista onward. Off by default on Windows 2000, Windows XP, and Windows Server 2003 (non-R2). Notably, on by default in Vista and 7 (silly @Gaska, and you thought you could fool us...)

• Huh??? I use it every single day.

http://i.imgur.com/tyTc1Nl.jpg

• The way I feel today, the Point should be on top. And the rocket in a crater.

• At least in ViM, no, you don't. When tab inserts spaces (expandtab), the backspace (←) dedents, i.e. when used in place where there are only spaces from start of line to cursor it removes one indent (softtabstop) worth of spaces.

The setting of (no)expandtab means that Vim behaves effectively indentically whether you decide to use tabs or spaces. (I'm fairly sure there's a setting which means "just use whatever (spaces/tabs) the current document is using", but my :h-fu is failing me.) So the whole spaces/tabs thing really ought to be ...

• Also, I've just realized most of my problems with cmd tab-completion come from the fact / is easier to reach than \. Still, not recognizing path executables sucks (I cringe every time I need to write "mingw32-make" or "mingw32-gcc").

• It's just something to be aware of: Ownership and permissions may change, and any other hardlinks to that file will not be updated.

If that's a problem then you do need to use a second command, rather than sed's -i flag, to write the data back to the original file.

-i[SUFFIX], --in-place[=SUFFIX]
edit files in place (makes backup if extension supplied).
The default operation mode is to break symbolic and hard links.
This can be changed with --follow-symlinks and --copy.

So maybe you can use additional flags rather than a different command, at least as far as the hardlinks go (haven't tried it myself). Doesn't help for ownership and permissions though, I guess. None of that has been an issue in the places I've needed to use it.

• Also, I've just realized most of my problems with cmd tab-completion come from the fact / is easier to reach than .
(another fine example of Discourse quoting)

Oh how I wish. My / is hidden behind <nobr>shift+7</nobr>. Which makes all shortcuts involving <nobr>shift+/</nobr> useless to me. \ has its own dedicated key under Esc.

But regarding cmd: does it still only show one option at a time while overriding everything you had to the end of the line?

• Oh how I wish. My / is hidden behind <nobr>shift+7</nobr>. Which makes all shortcuts involving <nobr>shift+/</nobr> useless to me. \ has its own dedicated key under Esc.

You don't know how lucky you are having the \ there. Mine is on AltGr+Q.

Well, it would be, but I'm using US now.

• Oh how I wish. My / is hidden behind shift+7. Which makes all shortcuts involving shift+/ useless to me. \ has its own dedicated key under Esc.

Why won't you remap your keys or something? Also, I know it's extremely uncomfortable, but don't you have another / on your numpad?

• You don't know how lucky you are having the \ there. Mine is on AltGr+Q.

Oh, I do. I'm very thankful for that and the dedicated key for < and >`.

• Why won't you remap your keys or something?

Hmm... I guess I don't use « that much. But switching to US layout is probably easier.

Also, I know it's extremely uncomfortable, but don't you have another / on your numpad?

I forget my laptop has a "virtual numpad". I'll try to get into the habit of using Fn+0. Thanks for calling my attention to that.

On external keyboards I occasionally use the numpad slash, but shift+7 ends up being less disruptive because of not having to move my hands.

• Lenovo keyboards (the ones on my laptop and desktop, at least) have a ÷ symbol instead of / on the numpad, which I forget about until I actually look at the numpad and get confused about where the / is

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.