TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Vim Anti-Patterns

458 pointsby microsageover 8 years ago

33 comments

pjungwirover 8 years ago
I never got into the habit of using { and }. I just use H M L (high&#x2F;medium&#x2F;low) to get approximately in the right part of the screen, then go line-by-line. You can also do 5H or 10L to get &quot;5 lines from the top&quot; or &quot;10 lines from the bottom&quot;. I make pretty good use of vim features, but I like to mix some sloppiness with the precision. I don&#x27;t often count things before typing commands, because that breaks the second-nature quality of navigation. If something is more than 2 or 3 objects away, I approximate. I do use counting with t, T, f, and F a lot to hop around, including things like c2f). Very frequently that combines well with spamming j.j.j.j.j. I use . with trivial movement (like j or n) at least 10x more than @a. Another way to move fast sloppily is W and B. I guess I&#x27;m saying: learn all the cool stuff you can do, but don&#x27;t feel like you have to find the most precise way to do every little thing. If you&#x27;re just starting out, relax and don&#x27;t try too hard. You can always just pick one new thing every couple weeks and try to add it to your habits.<p>Oh also: he mentions O to insert above the current line. I use that a lot, but on my systems (going back 15 years or so I think) it has always required a pause, like vim is waiting to see if I&#x27;m typing O or some longer command. If I type O and immediately start entering text, strange things happen. This doesn&#x27;t happen with o. Does anyone else experience this? Maybe it&#x27;s just something weird in my own setup.<p>EDIT: Some more &quot;moving fast sloppily&quot;: 1G goes to the top of the file. G goes to the bottom. Also you can not-move, but scroll the visible area so that your cursor is on the top line (zENTER), middle line (z.), or bottom line (z-). I use that a lot when I am Ctrl-Fing through a file, so I can see more context.
评论 #12647609 未加载
评论 #12645960 未加载
评论 #12647281 未加载
评论 #12645691 未加载
评论 #12646537 未加载
评论 #12645987 未加载
评论 #12648485 未加载
评论 #12648667 未加载
评论 #12645563 未加载
评论 #12647938 未加载
评论 #12646868 未加载
评论 #12647316 未加载
评论 #12650029 未加载
foobover 8 years ago
These are all good tips but most of the anti-patterns seem to skew towards beginners (<i>e.g.</i> don&#x27;t use the arrow keys, don&#x27;t navigate in insert mode). One that I think is more common among intermediate, and even advanced, users is the misuse of tabs, windows, and buffers.<p>A lot of people have a tendency to think of each tab as corresponding to a single open file. This is very understandable because it closely matches the paradigm of most IDEs but it&#x27;s actually an anti-pattern in VIM. Tabs are really meant to be more like workspaces where you arrange multiple windows into a desired layouts. You then have one buffer for each file that you&#x27;re dealing with and view them in your windows. It&#x27;s perfectly fine for multiple windows to share a single buffer or to switch out the buffer that is being viewed in any given window. This StackOverflow answer [1] and this blogpost [2] both go into a fair bit more detail.<p>If you&#x27;re trying out this approach for the first time then you probably want to add `set hidden` to your configuration in order to avoid automatically closing buffers that aren&#x27;t currently being viewed in a window. Coupling this approach with fzf.vim [3] makes managing very large numbers of files a breeze compared to using one tab per file.<p>[1] - <a href="http:&#x2F;&#x2F;stackoverflow.com&#x2F;a&#x2F;26710166" rel="nofollow">http:&#x2F;&#x2F;stackoverflow.com&#x2F;a&#x2F;26710166</a><p>[2] - <a href="http:&#x2F;&#x2F;joshldavis.com&#x2F;2014&#x2F;04&#x2F;05&#x2F;vim-tab-madness-buffers-vs-tabs&#x2F;" rel="nofollow">http:&#x2F;&#x2F;joshldavis.com&#x2F;2014&#x2F;04&#x2F;05&#x2F;vim-tab-madness-buffers-vs-...</a><p>[3] - <a href="https:&#x2F;&#x2F;github.com&#x2F;junegunn&#x2F;fzf.vim" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;junegunn&#x2F;fzf.vim</a>
评论 #12645614 未加载
评论 #12648765 未加载
评论 #12644724 未加载
评论 #12648665 未加载
评论 #12653319 未加载
评论 #12653912 未加载
评论 #12646640 未加载
lunchboxsushiover 8 years ago
I was originally a die hard Emacs user, but I started to feel a bit of strain on the whole holding Ctrl+p, Ctrl+n etc. I was trying to find an editor&#x2F;plugin that can reduce keystrokes or at least the strain. I ran across EVIL for Emacs which is just a Vim emulation and keybindings for Emacs users. Unfortunately I did not like it at first the entire hjkl felt awkward and all those modes - OMG. But after toying around with it a bit and playing the online interactive vim learning game I started to see the benefits of Modal editing.<p>I can easily without leaving home row move a few letter to the right or back with hl or f and F. but things started to get a lot more fun when you realize that Vim is a programming editing language and it&#x27;s beauty is in the commands. This leads to amazing things that I hated before, such as deleting 2 words back from my current position would simple be d3b. instead of shift+ctrl+left arrow x 2 + delete.<p>Overall it&#x27;s been about 2 months since I&#x27;ve started using EVIL mode for Emacs and I love it. I&#x27;ll stand by the saying that Emacs is a great OS and vim is a great for editing text in it&#x27;s modal editing.<p>lastly modal mode really felt powerful only after I had re-mapped my CAPS key to ESC. I mean throughout the past decade I don&#x27;t think I&#x27;ve even used Caps for anything. so I&#x27;ve remapped the machines I work on the have caps as esc.<p>- for those that think it&#x27;s not reasonable to do so and the whole point of using vim is so that you can edit machines via ssh then use vim on that machine, my suggestion is to use tramp in Emacs with ssh or plink to get to the server and edit (you will still have the local caps to esc key mapped)<p>TL;DR - Vim modal editing is amazing and feels has straining than other editing layouts - IMO.
评论 #12644813 未加载
评论 #12644588 未加载
评论 #12645606 未加载
评论 #12645823 未加载
评论 #12645797 未加载
评论 #12645466 未加载
评论 #12644657 未加载
bpierreover 8 years ago
Shameless plug: I wrote a small guide about how I switched to Vim, after having “tried” for years. It’s not a universal approach, but maybe it can be useful for people here: <a href="https:&#x2F;&#x2F;github.com&#x2F;bpierre&#x2F;switch-to-vim-for-good&#x2F;blob&#x2F;master&#x2F;README.md#switch-to-vim-for-good" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;bpierre&#x2F;switch-to-vim-for-good&#x2F;blob&#x2F;maste...</a>
yes_or_gnomeover 8 years ago
This is a great resource, but the article is pretty strict on the arrow keys. I would recommend `nnoremap`, over `noremap`, because it only disabled the arrow keys in Normal Mode.<p>The author&#x27;s explains his rationale in the next section, it&#x27;s to prevent users from living in Insert Mode. Fair enough. But, when making several relatively close edits, the ability to tap a few arrow keys in Insert Mode is far easier and less mentally demanding than any key combination that requires the user to bounce around different modes.
评论 #12644381 未加载
评论 #12646186 未加载
评论 #12645504 未加载
评论 #12648788 未加载
taneqover 8 years ago
While it&#x27;s unarguably fun, I can&#x27;t believe the effort required to become actually good at vim (as opposed to just using a few of the easier features) will ever pay itself off. (These days, at least - if you spend your days editing code in a terminal over a dialup connection, then it&#x27;s absolutely worth it!)<p>Maybe there are scenarios where the busywork of text editing really is on your critical path, but even as a fluent coder who uses some verbose languages at times (my current project is C++ and IEC Structured Text, does it get any more blabby?) I still spend far more of my time looking at, and thinking about, code than I do actually typing. Any extraneous cognitive load just takes focus away from what I&#x27;m actually meant to be doing.
评论 #12650168 未加载
评论 #12649957 未加载
评论 #12650144 未加载
评论 #12650456 未加载
sdegutisover 8 years ago
&gt; <i>&quot;If you have to move more than a couple of lines, moving one line at a time by holding down j or k is inefficient.&quot;</i><p>This very point is actually why I moved from Vim to Emacs years ago. After mastering Vim, I realized that Vim strongly encourages you to think a little too much about exactly how to get there or do that thing in the fewest keystrokes, and that it&#x27;s incompatible with muscle memory. Even years later, I still had to think too much about it. Whereas in Emacs I can just use basic character&#x2F;word&#x2F;line-based movements and let my muscle memory do its thing while I let my brain focus on the code itself instead of how to use the text editor.
评论 #12644306 未加载
评论 #12644287 未加载
评论 #12644265 未加载
评论 #12645222 未加载
Philipp__over 8 years ago
Not trying to start a flame war here, but I in recent few years I am seeing Vim being more popular, and I feel it adoption in terms of younger developers (those who weren&#x27;t programming when original Vi was around) grows. (Ok, please forget about GUI editors, they have their audience, and I am not comparing any of those facts to them) I used Vim for 3 years, and found philosophy and implementation of modal editing amazing. Writing and manipulating code in it went like bread and butter.<p>But then I told myself, hey, why not try Emacs? So I&#x27;ve been using Emacs for ~6months now and cannot but notice that community is much more niche and humble compared to Vim. Just look at the sheer number of color schemes available for both editors. And I had to agree, Vim was far superior text editor, but that wasn&#x27;t enough to keep me away from Emacs, since I gave the advantage to other things (everything else) that Emacs does better.<p>I tried EVIL mode, and it is amazing, but something just felt wrong using it inside Emacs. I wasn&#x27;t using either Emacs or Vim. I would often shuffle and mix commands, sometimes I would do :w, sometimes C-x C-s. So I decided to ditch Evil until I get more comfortable with Emacs key bindings. I came to Emacs because of Lisp (and general FP audience is much, much, more based around Emacs, makes sense), amazing tools and plugins which I found more stable, better designed, and it is weird to say this but things just worked with Emacs, things like auto-completion and code navigation (ivy, ace-jump-mode) were really fast, hustle free experiences. Disclaimer, I have never touched older versions of Emacs, spent my time in 24, and now in 25, so many of <i>myths</i> and problems that Emacs got known for over the time, I think, aren&#x27;t there anymore.<p>And to sum things up, what is really weird to me is that functional programming is on the rise and every year I see it more and more being adopted, but that doesn&#x27;t help Emacs audience grow. (Maybe because I am young, and I am nerd often found in nerd communities where things like FP are often praised, but in the real world considered a bit awkward or esoteric.) I showed up at the FP workshop few weeks before in local dev&#x2F;hacker community place, everybody rocking Sublime Text&#x2F;Vim, but nobody used Emacs, people were looking at me like I was an Alien. Spacemacs is doing good job at attracting people, but maybe Emacs will stay that programmers&#x2F;nerd phenomenon, the all-mighty lisp OS, that in the end people often reject or overlook. And why is it like so? I do not know. If somebody can sink into and learn Vim, I don&#x27;t see a reason why it is different story with Emacs.
评论 #12651990 未加载
评论 #12644940 未加载
评论 #12648478 未加载
评论 #12645172 未加载
评论 #12647290 未加载
评论 #12646664 未加载
glormphover 8 years ago
Useful stuff. I&#x27;d add typing J to move the next line to the end of the current line. A move I normally do by being on the next line and then type 0dwiBackspace.<p>Only just figured that one out by mistakingly having the capslock on when moving around.
评论 #12644304 未加载
dopeboyover 8 years ago
&gt; It’s always useful to jump back to where you were, as well, which is easily enough done with two backticks, or gi to go to the last place you inserted text. I<p>And here I was undoing and redoing the entire time.
评论 #12645419 未加载
评论 #12649830 未加载
JackMorganover 8 years ago
For anyone who wants to build muscle memory in Vim I&#x27;m writing 10 Minute Vim, a book of pre-made exercises for practising advanced macros, RegEx, and navigation. It&#x27;s already helped me learn a number of new commands in just a short time.<p><a href="https:&#x2F;&#x2F;leanpub.com&#x2F;deliberatevim" rel="nofollow">https:&#x2F;&#x2F;leanpub.com&#x2F;deliberatevim</a>
sevensorover 8 years ago
This is good solid advice -- I have a problem with vim advice that golfs more unusual circumstances to save a keystroke or extensively remaps the keys. (Generally the only thing I remap is F1 to ESC, because I use laptops that place them inconveniently close together.) Devoting too many cognitive resources to text entry is also an anti-pattern!
评论 #12644474 未加载
tremonover 8 years ago
Note that d2wi and c2w are not exactly equivalent: d2w will consume trailing whitespace after the second word, while c2w will not. FAFAIK, there is no exact equivalent of d2wi using c. I usually end up using c2aw, but that will also consume any word characters preceding the cursor (not a problem for me since I usually navigate using b and w).
评论 #12644473 未加载
nocmanover 8 years ago
&quot;Hitting Escape is usually unnecessary; Ctrl+[ is a lot closer, and more comfortable.&quot;<p>I agree with a lot of the things in this article, but wow, I could not possibly disagree with this quote more. Ctrl+[ is <i>WAY</i> more uncomfortable than using the Escape key.<p>Granted, I have <i>many</i> years of vim usage that have made hitting Escape a habit, and that probably plays a big part in it, but Ctrl+[ is downright painful for me (and yes, I sat and tried it for a while in vim to see what it would be like).<p>I can get from the home row to Escape and back with very little effort, though I understand that is not the case for many people. Perhaps it is due to the fact that I use my ring finger to hit Escape rather than my pinky (which would be a lot more work, I think).
评论 #12646082 未加载
评论 #12646105 未加载
评论 #12649835 未加载
评论 #12646451 未加载
评论 #12646702 未加载
评论 #12646758 未加载
yumaikasover 8 years ago
I&#x27;ll just drop my 2 cents here as a vim user. I&#x27;ve tried emacs a number of times, but I&#x27;ve gotten too familiar with what vim has on offer.<p>I think where vim often wins over emacs is the 110 vi modes that every IDE eventually gets. Vi is an idea that can prosper in many environments.<p>Emacs is kinda like smalltalk. To get much benefit from it, you have to buy into it whole hog, or not at all. I can write C# in VS with vim keybindings, go in sublime text, and then just hack on a Lua snippet in vim itself. Emacs has ways to work with all those, but that requires a new skill set that I don&#x27;t need at the moment. Maybe after I graduate from college, but right now isn&#x27;t the time for me.
stewbrewover 8 years ago
While these tips are good per se, in retrospect I don&#x27;t think it&#x27;s that a good idea to tie your muscle memory that much to a single piece of software. I&#x27;d say enjoy the cursor keys. Moving around in insert mode is ok too.
评论 #12648564 未加载
评论 #12649865 未加载
评论 #12645858 未加载
oolongCatover 8 years ago
Instead of<p><pre><code> c2w</code></pre> I prefer<p><pre><code> v2ec </code></pre> selecting with v helps me avoid making mistakes.
评论 #12645787 未加载
评论 #12645285 未加载
deathgrindfreakover 8 years ago
I find that marking my place with &quot;ma&quot; and returning to it with &quot;&#x27;a&quot; is a huge productivity boost. Of course the &quot;a&quot; is just a label, you can use any character (this allows multiple marks as well).
评论 #12645605 未加载
SCdFover 8 years ago
So I have used ST3 for years now, and I realise that I&#x27;m slowly moving toward Vim. I started using Vim shortcuts in Chrome, and now I use Vintageous in ST3. Vintageous is good, but it is incomplete. It can&#x27;t even do the first examples on this article (ctrl+f &#x2F; ctrl+b) correctly.<p>Thinking I should just bite the bullet. Would mean I could work on a server using Mosh + Tmux as well, which should be rather nice.<p>What would the current canonical guide be for getting up and running with Vim, with plugins, auto-complete[1], inline linting, multiple carets etc?<p>[1] Just the ST3-style of parsing out tokens in the same file
评论 #12644782 未加载
评论 #12645795 未加载
评论 #12644723 未加载
dvcrnover 8 years ago
I can recommend vim-hardtime [0] for anyone wanting to force themselves to learn the &quot;vim-way&quot; of navigating. It disables n subsequent presses of hjkl where n is any number you want.<p>For me I set it up to not allow more than 2 steps in any given direction to remind me to use jumps instead.<p>[0]: <a href="https:&#x2F;&#x2F;github.com&#x2F;takac&#x2F;vim-hardtime" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;takac&#x2F;vim-hardtime</a>
bcheungover 8 years ago
&lt;C-c&gt; works likes escape almost all the time. Mapping the caps lock key to Ctrl makes hitting &lt;C-c&gt; super easy. I usually use that instead of escape.<p>In general I would agree 1 character at a time is an anti-pattern but it needs to be balanced with the cognitive load of counting how many words or deciding what is or is not a boundary when there are symbol characters.
评论 #12647444 未加载
godelskiover 8 years ago
Movement is an important part of vim, but so is auto-competing. Probably the biggest thing that helped me greatly improve speed in vim was learning the C-x keys and that they are context aware. Having to repeat an entire or similar line&#x2F;s becomes quick without having to go back to that place. Why move in the file when vim can find the text for you?
评论 #12647058 未加载
mastaziover 8 years ago
&gt; If you happen to know precisely where you want to go, navigating by searching is the way to go, searching forward with &#x2F; and backward with ?.<p>It&#x27;s worth noting that, after searching with ?, you can still move back and forth through occurrences using N and n, in the same way you would after using &#x2F; for a search.
tomc1985over 8 years ago
While I prefer vim for text-mode editing, it is so frustrating that it just has to use its own damn keyboard shortcuts and that the ones burned into muscle memory from 20+ years of using pretty much every text editor under the Windows&#x2F;DOS sun don&#x27;t work without significant hacking.
bagolover 8 years ago
My favorite is mapping &#x27;()&#x27; to &#x27;vi)&#x27;, &#x27;[]&#x27; to &#x27;vi]&#x27;, &#x27;{}&#x27; to &#x27;vi}&#x27;. So I can read is as &quot;grab parentheses&quot; instead of &quot;visual inside parentheses&quot;
vlunkrover 8 years ago
&gt; don’t forget you can yank, delete or change forward or backward to a search result.<p>Did not know this! (although I&#x27;ve been on vim for ~a month) That&#x27;s a great trick. Much easier than trying to count words.
评论 #12645399 未加载
lucb1eover 8 years ago
Page loads very slow and uses Wordpress. In case it goes down: <a href="http:&#x2F;&#x2F;archive.is&#x2F;JaOXd" rel="nofollow">http:&#x2F;&#x2F;archive.is&#x2F;JaOXd</a>
choicewordsover 8 years ago
My eyes. Good content, but very hard to read.
rhinoceraptorover 8 years ago
I have a programmable keyboard, so I just map escape to be right above tab, where tilde usually is.
评论 #12644173 未加载
评论 #12644331 未加载
评论 #12644996 未加载
评论 #12645905 未加载
lathiatover 8 years ago
This is a really good guide.
jnordwickover 8 years ago
I started off using Emacs a lot (like tons and tons of Emacs with tons and tons of custom modes, functions, and craziness - network pong anybody?). For almost a summer, my xinitrc at the school labs literally opeed up a full screen Emacs; it was my window manager too.<p>The simplicity of vim (and pretty colors) drew me in. Plus as I learned more sed&#x2F;ed, I understood vi more. That, and a slow connection from off campus really sucks. I learned that too pretty well. Well enough to hack together some vim scripts, but nothing near my Emacs level. I feel like vim mode hacking is a beast you need to be specially equipped to handle (and I can write APL in any language so it isn&#x27;t the syntax).<p>Then Eclipse and IntelliJ came around and I only really used vim for quick one off stuff (if I didn&#x27;t use printf, echo, or cat). The only time I used vim was for C&#x2F;C++ or something esoteric, like KDB+&#x2F;Q&#x2F;K, that didn&#x27;t have their own dev environment (unlike say VHDL or Matlab where I could sit in their ugly cocoons).<p>Now I&#x27;m growing tired of the very buggy, slow Java environments that seem to require a gaming rig to compile HelloWorld.java (or sorry, HelloWorld.xml with SpringBoot). And I see all these poor Emacs clones playing catchup with 2000. You have editors written in JavaScript inside a web browser with modules transpiled to JavaScript (for some reason JavaScript is too low-level to write text editing packages in now -- God help us all). That, and they don&#x27;t support a tenth (I&#x27;m being generous) of the functionality that Emacs does and they probably never will.<p>What is so hard about an extensible text editor? Just getting the basic hooks down for self-insert and movement without having to go to swap?<p>I remember when Emacs was called &quot;Eight Megabytes and Constantly Swapping&quot;. I now see Atom routinely take up over 800 MB. And it still can&#x27;t play Pong.<p>Now with Rust and other languages, I&#x27;m back home in Emacs, but the keystrokes do tend to bother me a little. I liked the HJKL movement keys in vim - I just hated the modality and think I spent more time trying to figure out &quot;my cursor is here, but I need move it over there - so first I need to jump on that log, shoot the rocket to knock down flowerpot then run quickly while the line is falling to catch it and K it to the next line&quot; -- like some sort of weird text editing puzzle (I wonder if you could make a vim code golf puzzle set).<p>Emacs has these bindings that feel like finger yoga, even when I&#x27;ve remapped Caps, control, half my F-keys, etc. What I really need to do is remap my toes to my hands, I think.<p>It would have been really nice to see C-[hjkl] style movement (with maybe CS-[HJKL] be the distance mods or something). It&#x27;s too late now. You of course you can remap those keys, but too much of that behavior is baked in to people).<p>Maybe one day when I&#x27;m old and gray I&#x27;ll do the Knuth thing and start a new text editor, but before that I&#x27;ll probably need to redo the monitor, mouse, and keyboard and that is just too much right now.
评论 #12650015 未加载
评论 #12644988 未加载
foo101over 8 years ago
Moving to the far left-corner of the keyboard to reach Escape has been a major annoyance for me. On the other hand, the Caps Lock key on my keyboard is almost useless. I never use that key. Is there a way to map Caps Lock to Escape in Vim?
评论 #12647418 未加载
评论 #12652012 未加载
评论 #12647263 未加载
clifanaticover 8 years ago
&gt; Vim isn’t the best tool for every task<p>Begone with your heresy!