How I Loved Vim, Emacs, and the Keyboard

To some extent, this article is the answer - or rather, an addition - to the publication of “Why a 21st Century Programmer's Vi-Ax” . I saw that in the comments people were still wondering: what is the point in these editors when there are full-fledged IDEs; The article gave some real examples and, realizing that I have something to say, I decided to share my own experience. It is written in an artistic style, because I think if people wanted a dry squeeze, they would just go read the manuals. I’ll also warn you that in the manuals by Emax, the Alt key is referred to as “Meta”. I will say “Alt”, as for many this name is more familiar.

Looking ahead, I’ll say that at the moment I am using Emax for coding with the Evil plugin (a plugin emulating VIM’s functionality)and combinations disabled for insert mode in this plugin (ie, I have to press “Escape” and Emacs transforms into vim, but in insert mode it is still the same regular Emacs. Just imagine: you’re super hero X at night, but nobody does not know your true alter ego, because during the day you are an ordinary super hero Y.) .

"What is your typing speed?" - the question asked when putting into practice just six months ago caused me an embarrassed smile, made my eyes go down and mumbled “Well, I didn’t measure the exact speed, but half-blind ...” Oh, yes, I often saw in the comments on Habré dedicated to simulators blind set, a kind of proud answers in the comments, "half-blind ...". Unfortunately, if one of the readers has a “half-blind set” and you want more, then reconcile: my humble opinion says that you are contraindicated not to use Vim or Emax. Because when you write code (aka a lot of text)in these editors, without having this kind of set, you will have only two ways: to score and use a regular editor or learn to type blindly. Because when instead of arrows you use “Ctrl + n / Ctrl + p” and “Ctrl / Alt + b / f”, etc., to move around the text, you simply can’t lower your eyes every second to see where you press . At first, however, I was very perplexed - "Why is the button" n "in place of the button" b "? And what the hell did I just press the "p" button, and the effect, as if I pressed the "["? But believe me, this will pass.

Relatively recently, I was an ordinary student with a rich imagination, a bunch of superficial knowledge and without any serious programming experience - an average enthusiast. Quite by accident, this student was lucky enough to get into the branch of one large company that writes software for controllers running GNU / Linux on them and, of course, under the “most popular OS”. Initially, I got there as a practice from the institute, began to write some small software in the form of a server under GNU / Linux and a client for Windows, then I was accepted as a programmer and added it already as a working person.

Like the “average enthusiast,” I have GNU / Linux (Kubuntu) at home , so when I was given a task and a bare computer with inconvenient WinXP (compared to KDE)for work - I, realizing that in any case I would have to keep one system under the virtual for development, decided to assign XP'yusha this role and install Kubunta on the office PC.

“What does this have to do with our article?” You ask. Oh, the most direct: as a result of my decision, I was auto-deprived of Visual Studio. Then I did not know about its counterpart for Linux aka QtCreator, I knew more precisely, but I thought that it can only be used for development on Qt. Therefore, the choice fell on CodeBlocks. In part, this choice was also due to the fact that this IDE is widely used in our office for Linux software.

CodeBlocks did not last long as an editor for me, I quickly slipped into editing the code in something third-party and launching this IDE exclusively for compilation. I had little experience using SublimeText - after it it seemed extremely inconvenient to edit the code in CodeBloaks. Over time, I can’t recall anything specific that CB didn’t like, except for the complete lack of semantics of auto-brackets, which made it easier to turn them off, the best color scheme in ST and, of course, the killer feature of the latest “mini-map” ".

Unfortunately, the moment I switched from SublimeText to Emacs completely fell out of my head. I can only guess how it happened. I can assume that I wanted something new. Or maybe I was inspired by numerous articles on how cool it is to code on Emax, written by independent people with a fairly long programming experience. I remember trying to switch to Emax a couple of times, and then complained to a friend from another city, which, according to him, is actively using. I remember how I called this editor bad words, poured mud and humiliated in every possible way in the hope that someone would say “Guy, you are an idiot, you just don’t understand this, and this, and that’s why you say that; stop talking nonsense! ”, but the friend was the only person who could answer something sensible, and for some reason he was silent. And then, as if after an accident at high speed and a non-racing track, I suddenly found myself in a chair, tcpdump digits were running nearby in the terminal, and I quickly typed into the editor Emax. "Woke up - a plaster."

People who first take Emax in their hands have a fundamentally wrong expectation from this editor, which I stumbled on more than once, and because of which I did not immediately begin to use it. I see how cool uncles with programming experience of 30+ years, doctors of science, use it instead of IDEs and then write posts about it, how cool everything is in their world. But in my world, when I asked for a welding machine for a couple of tens of kilowatts, they gave me a rub, a lot of firewood, a huge folio, and said that tomorrow it would be hot here. And they also advised not to confuse the volume with wood.

You must understand that before it works for you, you will have to spend a lot of time setting it up. And, do not even hope that you will be able to avoid learning elisp (the language used by this editor for scripts)- Unless, of course, you want to seek help just because the piece of code found in some social network like Github from someone’s config doesn’t work exactly as it was written in the comment.

But this is all the lyrics - I'm not a pro in Lisp at all, and the article, as we recall, is not about the nuances of the configuration of Emax, so I’ll try to leave this topic aside.

So, since we have not reached Vim by the time interval, we only have combinations known for their inconvenience for moving through Alt / Ctrl plus a letter. Mini-memo for those who are not in the know or do not remember:

Forward char ⇒ Ctrl+f
Back char    ⇒ Ctrl+b
Forward word ⇒ Alt+f
Back word    ⇒ Alt+b
Line up      ⇒ Ctrl+p
Line down    ⇒ Ctrl+n

Believe me, their inconvenience is caused only by a habit! I know there are people who swap Caps-Lock and Ctrl, I didn’t do this, and I used to move to the keys mentioned instead of the arrows in just half a week. How convenient is this compared to arrows, you ask. Oh, it's very, very convenient! First, you don’t have to move your hand to the arrows. This is very cool, because if your blind set is not based on an excellent orientation in space - when you can hold your fingers anywhere, get caught without looking at any key - but on the location of the index fingers on the serifs of the “f, j” buttons, then you every time you have to print to look for these recesses; and it doesn’t matter at random or with a glance. Secondly, arrows can be on [no / laptop] books, and keyboards of not very adequate manufacturers can be located at rather different distances,(as on my laptop) - i.e. if you don’t have a perfect feeling of space and have not used this keyboard for many years, you will have to either peep where you moved your fingers each time, or often make mistakes by accidentally pressing the wrong button. To be honest, the Ctrl button on some keys may also be where the hell knows, and then vim combinations will come to the rescue, but more on that later.

Another irreplaceable combination is “Ctrl + xb”, it moves the cursor to the field for entering the name of the buffer (aka open file) that you want to go to. Auto completion on a taboo. And, if you have “ido-mode” turned on, like mine, then this field will also have a list of buffer names that fit, you just need to type part of the buffer name and this list will be filtered out accordingly. You can add a name or simply select the appropriate name on “Ctrl + s”, “Ctrl + r”. How convenient is it? I will say this: I recently coded in Visual Studio, and to go to the tab, I had to use VsVim combinations like “Prev. tab ”,“ Next tab ”,“ Tab No.n ”. And this is ok, until you have 100500 tabs open, a couple of split screens and how to switch between all this wealth becomes completely incomprehensible. It remains only to pick up the mouse. Often pick up a mouse. Probably, for the uninitiated, this will seem like a strange argument: so what, why not use the cursor? Let's just say - when you know that there is a way to do something much faster, and almost reflexively other paths that require switching attention take longer and do not introduce any advantages, they start to annoy.

To give you the idea of ​​a mouse, I’ll give an example that is very close to IDE’s hearts: imagine that you need to rename a local variable under the cursor. So, you can α) Display the context menu and enter a new name β) Write a simple regular form like “\ bmy_var \ b”, and make sure that the replacements are only within the function. If you do not know about option α, you will be completely comfortable with the case β, because this is an ordinary situation, and it is clearly better than renaming manually. But, when you have tasted the sharp cries of a variable that dissolves into nothingness just a couple of your clicks, you will try your best to avoid the option β: it is bad, it is evil, it is the enemy, it annoys, it makes you make too many movements, it makes you switch , one wrong move - and you cut yourself.

There is also a buffer * scratch * in Emax. This is a draft, automatically created every time you run it. There is a warning about its purpose. The bottom line is that it is not saved by default. It can be used to write brief thoughts, for example, about how your architecture will look - just to be sure that nothing is missing. Or to test the regulars, or maybe for a brief note, what to do, how do you get back from the weekend (this, if you, like me, do not turn off the PC - I just throw it into hibernation). Yes, a bunch of uses, I'm sure you have something like that. While I used SublimeText, I always had this kind of file open, though the problem was that this file was always saved, although I did not need it. What kind of IDE is there such a thing? Yes, at least in what other text editor is such a thing?

When I started using Visual Studio, I was just amazed at how dependent it was on intellisense. Once the slightest error appears in the code, the indentation stops working. Imagine that you have a function of only about fifty lines with a bunch of fields of visibility, try-catch, and other stuff. And it suddenly dawns on you that, in general, there will definitely be no exception, yes, and, there you can redo everything. You start to delete some pieces of code, transfer others, and here - achtung! - Intelligence detects errors, and you can no longer press the button to change the indentation by function, it quickly plunges into chaos! But this situation can be quite rare, so another example - before I tried Visual Studio, I had a frequent habit of writing out a function with pseudo-code, and then writing real code on top of the draft. Based on sad experience, when the logic of some function seems obvious, and you write immediately the code, a bunch of checks for errors, etc., and then you discover that you did not take into account a real trifle, and now you need to shovel everything. So, in Emax it’s enough to select a piece of code and click on the tab - and it will be equalized regardless of what errors the flycheck found in the code. The same goes for vim("=" key for alignment) .

Another simply irreplaceable key is “Super-i”. No, don’t look, this is a custom combination, it launches the “ido-menu” function, and I just can’t imagine how people using the IDE live without it. It works in a similar way, as the buffer selection I mentioned earlier only displays a list of functions in the current file. It is incredibly convenient: you pressed mountains. key, entered part of the function, which you suddenly came to mind, and pressed enter. You can return to the previous place on "Ctrl-u Ctrl-space".

But the coolest thing in Emax is that you can find / write a function that performs any kind of action, and bind to absolutely any event in the editor. And you don’t even have to restart the editor, everything happens on the fly. For example: no one likes hanging spaces at the end of lines - why are they? Wouldn't it be nice if they were deleted before being saved? Well, we find the function “(delete-trailing-whitespace)”, we find that the hook that executes before saving the file is called “before-save-hook”, and add the function there. Or maybe it struck you as it would be cool if in C ++ all the inclusions were sorted alphabetically before saving the file? Sumptuously! We write to the same “before-save-hook” lambda, which checks that the current C / C ++ mode is on, and searches for inclusions, then sorts them.

Another thing that was missing later in Visual Studio - “Alt + q”, it aligns the current paragraph with a given maximum number of characters per line. Those. if the number of letters is too large, the words are transferred to the next line. An irreplaceable thing when editing code comments, and I don’t know that it should be in at least Visual Studio or CodeBlocks.

To be honest, I have no idea what else to mention so as not to make the article a boring enumeration of the features of Emax. There are an incredible amount of functions in Emacs for all occasions, I’m not saying that without leaving the editor, there is support for the terminal — serial and regular — debugger, version control (though I didn’t figure it out, but I just didn’t get it right), keyboard macros, and hell knows what. Those who say that a full-fledged IDE are cooler just do not know that all of their features in Emax are either there or can be installed. For example, for C # there is a so-called. omnisharp-server, which runs separately, and then you can connect Emax to it to get all the features, such as “go to definition”, “renaming variables and references to them”, auto-completion, etc. Problems usually start from ignorance of elisp, and reluctance at the slightest question, just see how this code works. I say because I myself was like that - I didn’t really know the Lisp, and I couldn’t set up omnisharp-mode. The tutorials seemed to me insufficiently detailed. The questions disappeared after being desperate, but still not wanting to ask a question on the forum, I decided to look into the plugin code, and found that even a configuration example was lying on github.

By the way, the first sign of interest in Vim woke up precisely while I was using Emax. Absolutely by accident, I found that there is a cool function on the “o” button in “normal mode” that works as if you would press “End” and “Enter” in the regular editor - it starts a new line without breaking the current line. And the big "O" starts a new line above the current line. It’s very convenient, I zayindil a similar thing on a separate key, but I was curious: what else is there in Vim, what is not in Emax?

So let's move on to VIM. My acquaintance with this editor took place in sad circumstances: I started working in Visual Studio with C # code, there was no time to deal with the work of the Omnisharp server, because I was stuck at work for 12+ hours due to problems with the deadlines for the project and tried to get as much experience as possible, in case I, as an inexperienced student, was simply fired. Therefore, I decided to simply configure Visual Studio so that I could move around comfortably. And then terrible disappointment awaited me: the plug-in found with the Emax keybindings completely refused to work; and even when I decided to somehow simply reconfigure, for example, “Ctrl- →” to “Alt-f”, the studio worked with combinations just disgustingly. Firstly, if you press Alt and release, it happens that I wanted to jump one word ahead, and then changed his mind - when you try to continue writing code, you can close the file, run the debugger, call an apple monster from the abyss of hell, or accidentally become the first person killed by a spontaneous generation. Because when you release the alt without pressing any other key, the cursor jumps to the studio menu(where File, Edit, etc.) , where each letter corresponds to a submenu. This is a very big problem, and I could not find how to turn it off. Based on a search on the Internet, I concluded that this is a "feature" of the window manager under Windows and it cannot be disabled.

And secondly, in Visual Studio (achtung!) There is no “jump to the end of a word” command! Those. shortcut “Ctrl- →”, working everywhere exactly this way, in the IDE sent you to the beginning of the next word! It was just awful, I tormented myself like this for a couple of weeks and realized that in 80% of cases after I jumped to the next word that interests me, I have to press the key again to return to the character back.

Then I remembered that I read about a certain plug-in for Visual Studio, emulating VIM functionality. It is called VsVim, and it is magnificent. And yes, it does have the command “go to the end of the word”! This was a new round of evolution: knowing that something should be automated by pressing a single key, I just entered Google, as it was done in VIM, and almost always, it was implemented in VsVim, with the same keys, or teams. I got used to traveling on hjkl for maybe half a week, as a maximum.

"Why Vim?" Remember, I told you how cool it is to use the “Ctrl / Alt-key” combinations instead of the arrows. So: here you didn’t even have to press Ctrl. At first it seems that tapping Escape every time loses, but believe me, this is also just a matter of habit. Except, perhaps, in extreme cases when you need to move only a few letters to the right / left.

Navigation in Vim perfected breathtakingly. All code within a few keys. Move to the center, beginning, or end of the screen? M (iddle) , H (ihg) , L (ow)at your disposal. Scroll half the screen? “Ctrl + d” / “Ctrl + u”. Go to opening / closing bracket? [<brace>, and] <brace>, respectively. Go five lines above? Just hit 5k. Go three words to the left? "3b". Jump to a proclamation of function? "Gd". Leap to the next comma in a line? "F," Return to the previous, next place in the code? “Ctrl-o” and “Ctrl-i” respectively.

In addition to the best code navigation, this finally gave me a complete blind set and taught me how to evaluate the number of various objects. The reason for the latter is that almost any vim command accepts a digit, which usually means the number of times that the command needs to be executed. Of course, this was also in Emacs, but there it was necessary to press Alt or Ctrl additionally with the number, which, in the absence of the ability to press the number blindly, did not add enthusiasm. And therefore, the numbers there were used by me only in extreme cases, but not in everyday operations, such as switching to a few words sideways. Here, adding the prefix to the command was as simple as possible, and therefore I very soon remembered what numbers (and characters that are also in active use there)are on the digital row, and with which fingers it is better to press them.

Well, in the end, the vim crown feature is text objects. An irreplaceable thing during coding. The fact is that many commands (in particular, deleting, aligning, copying) accept postfixes, and they are quite intuitive. For example, do we need to align code inside square brackets? Click "= i {" Want to remove everything from inside the angle brackets, including themselves? Click "da <". The rule of postfixes is quite simple: an action, then i (nside) or a (think of an indefinite article) , then the object on which we want to carry out the action. Besides brackets, it can also be w (ord), and this is perhaps the most commonly used object. The postfix “a” works in this case on a word with a space at the end, if any. “I” is obviously above the word itself.

If I compared code navigation in vim and Emacs, I would say that vim definitely wins. But not for that people love Emax, but for its gigantic extensibility! Hmm ... And what if ... If someone even wrote a plug-in for Emax, which emulates the functionality of vim'a? And yes, there is such a plugin, it is called "Evil"! Emacs doesn’t have anything particularly useful on the Escape key, so why not turn on vim normal mode on this key? And leave the rest of the keys unchanged? No sooner said than done:

;; remove all keybindings from insert-state keymap
(setcdr evil-insert-state-map nil)
;; but [escape] should switch back to normal state
(define-key evil-insert-state-map [escape] 'evil-normal-state)

Also, in addition to blind typing, quick text editing, and the ability to figure out the number of objects, it allowed me to get acquainted with the wonderful LISP languages , which I also wish you.

I have not said that IDEs are forested compared to Emacs? Ah, well, he said.

Well, and according to the good tradition established by the author of the last post, I will mention that I use FireFox and the Pentadactyl extension to it. This is a fork from Vimperator, which, unfortunately, is not developing now. In general, I must warn you that something strange is happening to Pentadactyl. It seems to be like a living project, but in working condition for new versions of firefox there were only night builds. Recently, they stopped appearing. When I updated my browser and encountered a similar problem, I foundon the Mozilla Pentadactyl page among the reviews, one from someone sympathetic who promised to post builds . I was glad I installed it at home, and what was my surprise when the next time I went to work from reviews to use the link, the review was not found by me. Then I was not too lazy to register, duplicated the review and the next day it was also deleted. Anyway, since someone removes reviews, then the project is alive, which cannot but rejoice.

And currently at the office I use the Awesome window manager, which is very convenient for work, but that's another story.

Also popular now: