Intermediate Vim
dn.ht
dn.ht
They may seem a bit complex but are quite simple. Basically you just record your editing movements and play it back.
How macros work: press q x to record and q to end. Then repeat with @ x, where x is the name of the macro you want to use (you can have several macros). Anything you do will be played back. That's it... Conceptually simple.
A nice trick is to record a macro for a single line and place the cursor on the start of the next line at the end of the recording. This way it's possible to operate upon large blocks of text in a similar way by just repeating the macro. The key then is to use generic movements like "go to the beginning of line" (^), "go to the end of line" ($), "skip word" (w), etc.
As a silly toy example, if you have a list of strings in a text file, you can turn it into a list of quotes with (from the top of my head):
qx<Home>i"<Esc>$i<Right>",<Down><Home><Esc>q
(Record macro, go to beginning of line, insert mode, write a quote, exit insert mode, go to end of line, insert mode, move right, write a quote and a comma, move down to next line, move to the beginning of line, exit insert mode, end macro recording)Then to process 100 lines, you'd do:
100@x
That turns these:
alpha
bravo
charlie
delta
...
into "alpha",
"bravo",
"charlie",
"delta",
...
If you have hundreds of lines of that you'll start to appreciate the easy automation.I use macros to do drive-by mass-edits without resorting to sed, scripting, regular expressions etc. since the cognitive load of those is higher for me.
Edit: formatting
Unless I use a feature regularly I find it's a higher cognative load. I use bash, sed and perl regexps on a near daily basis. Macros are very rare.
:%s/.*/"&",/
The & special character gets replaced with the whole matched pattern.:s/x/\&\/\\
But this has prompted me to look this up and it turns out that you can use @@ to do this. This will make my life a lot easier, I can just smash <Shift 2> until all my lines are updated.
:'<,'>norm @x :g/pattern/y A
you need to use an uppercase register ("A" in this case) because it executes `y` once per match. With `y a` instead of `y A`, you would end up having only the last match in the register. qq0i"<Esc>A",<Esc>jq@@
i agree this example is silly. i'd do something like :%s/.*/"&",/Macros really shine when you need to do a bit more complex stuff, there is certainly a cutoff between ease of regex "from the top of my head" vs. spending time formulating it.
edit I see someone else answered this - use @@ instead of .
Macros and other solutions are great, but for this very simple and surprisingly common task, multiple cursors are perfect.
A macro like this will just keep calling itself until it fails, which is nice for files with thousands of lines and you want to make a change that is not conducive to a regex.
As a tip that took me a while to figure out, you can search within a visual selection using `\%V`, which may make it easier to reliably go to a specific part of the current line (compared to t or f like I mention above).
Also wanted to point out that iVim for iOS is pretty good, for those rare cases you want to do some text processing on the move that makes you miss Vim: https://itunes.apple.com/us/app/ivim/id1266544660?mt=8
A decent post on recursive macros: https://jovicailic.org/2018/06/recursive-macros-in-vim/
I use it so I can run the macro in batches a couple times and check if I made any mistakes, before picking an absurdly high number to hit the whole file.
:'<,'>normal @q
to apply the macro (in q) to that region. You might want to remap the above command to some shortcut if you find it useful often.
Modern Vim[1], mentioned elsewhere in the comments, covers more advanced Vim usage.
[0]: https://pragprog.com/book/dnvim2/practical-vim-second-editio...
I got a boost in my Vim productivity after reading a similar StackOverflow answer a couple of years ago. It's a nice read for people familiar with Vim, and another good resource for new users.
"Your problem with Vim is that you don't grok vi." https://stackoverflow.com/a/1220118
Most of it is "intemediate".
I imagine it might be quite a good starting point for somebody who's used to VsCode productivity but wanted to get into vim without going full vim!
"vim.normalModeKeyBindingsNonRecursive": [
{
"before": ["u"],
"after": [],
"commands": [
{
"command": "undo",
"args": []
}
]
},
{
"before": ["<C-r>"],
"after": [],
"commands": [
{
"command": "redo",
"args": []
}
]
}
],Nevertheless, I think that VIM support for IDEs has improved a lot these past years. VSCode and Jet brains have done an awesome work integrating it to their software.
My annoyances are pretty small: not being able to do `:e ` and then tab complete the directory structure and the fact that VSCode has splits with tabs rather than tabs with splits.
The reason I wrote this is because I've been pair programming quite a bit lately and also mentoring some new vim users—so I wrote this little guide to help vim beginners level up their skills a bit.
What I noticed is that a lot of vim beginners find composing commands and text-objects to be kind of mind blowing.. and even more experienced vim users mentioned that these were the things that made them have that "aha" moment and convinced them to learn more.
Does it mean both party have to use the same editor? I wouldn't participate in a pair programmnig like that.
Pair programming is best when everyone can use one's favorite editor. When there is a pair switch there is also an editor switch if the other party prefers a different editor.
> Tip: Generally after you’ve inserted some text, you want to return to command mode so that you’re ready to enter your next command. I’d recommend pressing esc any time you’ve finished typing.
Totally agree with this. What really helped me with getting this into my muscle memory was remapping Capslock to ESC which seems to be what many (most?) vim users do.
I'm not quite convinced about this as general advice :
> If you’re like me and never learned to touch type properly, just use the arrow keys, it’s fine, don’t worry about it.
When I decided to go all-in on Vim keys some time ago (trying to find plugins for everything), it didn't take long to transition to hjkl. I think it's easier and more convenient to stay near the home row rather than moving my hand over for the arrows. I don't strictly touch type either.
:heAre touch typists rare these days? I feel like people these days forego even learning to type properly. I dont understand why they refuse to just learn it, it is such an essential skill to have.
I’m very grateful that my dad, recognizing back in the 90s how important a skill it was, made me learn to touch type when I was just six years old. I never got as good with the number row as with the letters, which was fine for a long time as I am very quick with the number pad, but nowadays I mostly use my laptop which doesn't have a number pad. It is a silly thing, because I could with a few minutes every day for a couple of weeks train myself to be as quick with the number row as with the letters, but I haven't and probably never will. There is no pressing need for it in my life and no one to force me to do it.
Later on, when I learned to program, I found that my slow typing speed frequently caused me to lose my train of thought midway through a line of code. It was frustrating enough that I spent a month teaching myself to touch type. Basically, I took online typing tests over and over after work.
Since then, my typing speed has at least doubled. I don't look at the keyboard anymore, and I'm able to get my thoughts down with _much_ less friction. I can't recommend it enough.
e.g. How fast can we type the below accurately?
function typing_test($foo = 'bar') { return $foo }
Typing App for Engineers, MVP:
* Load a canvas with a preloaded code snippet, the text is mostly greyed out
* When typing your text shows over the greyed out text, but darker, red if the character doesn't match
* Time how long it takes to type it perfectly
Gold plating:
* Users can login
* Users can upload own code snippets for community contributions
* Leaderboard shows top times and users for those who typed it perfectly, only perfect scores are registered, because code must be perfect or it won't run.
This could also allow an engineer to learn APIs while learning to type. I did this with 60 or so pages of Drupal code a few years ago. I used Compiz fusion's overlay opacity control to put a loaded code snippet into VIM,reduced the opacity of the window slightly, then laid another VIM terminal on top of it, reduced the opacity of that terminal window so I could see behind it and started typing the slightly greyed out text from the window behind.
Been meaning to put this out on my blog but this is a good start.
I like the idea of typing out code though! I think there are some sites that do that, like https://typing.io/
* It doesn't seem to have the social/competition/gamification aspect built in like Vim Golf.
I studied a few charts beforehand, but the repetition is really what made it sink in.
Also helps writing code big time, but the big win is in communication, and as someone else mentioned during pair programming sessions.
You might more than you think. That was the case for me. Rearranging the key caps on my (non-laptop) keyboard quickly cured that, and is an interesting exercise when you can afford it.
I was quite surprised to see that there are programmers that don’t do touch typing.
For me the biggest advantage I see after learning touch typing is lesser errors because you are getting proper feedback from the screen while you are typing.
Prior to that, I could type at a very decent speed and pseudo touch typed: I would put my fingers on home row but quickly I would lose that discipline and look at my hands as I typed and use the wrong fingers for various keys. I'm guessing that most people who are using VIM (or just programming on a daily basis) are close enough to count even if it isn't proper touch typing like I had in the past
That being said, I'm SOO much faster than I used to be and would encourage people to grab an ergonomic keyboard. Better for your body and it forces you to type properly :D
So I wrote a little program to download source code from Github and allow you to type along with it, and measure the number of typos. It's super old, half-finished and was one of the projects I used to learn Python, so the code is hot garbage and unmaintained, but it might be an interesting thing to look at and use if anyone wants to practice touch-typing on source code:
https://github.com/bcbrown/CodeTyper
Hell, if anyone's interested in using it, maybe I'll pick it back up and improve it.
Personally, I learned to first type in the mid to late 80s on a 286 using Mazis Beacon and an IBM selectric typewriter. I wasn't hunt and peck, but still looked at the keyboard while typing.
I didnt really no-look touch type until a took a typing class in high school where they covered your hands so you couldnt see the keyboard. That was the key to me really improving. I went from around 45 wpm to about 120 wpm. I didnt really get comfortable with the number/symbol row until I started programming heavily. Still had to look down for a while to find those symbols. Also one of the reasons I hate nonstandard keyboard layouts. I know where "{}[]/\" are, but on a laptop or compact keyboard, I'll have to gunt for those potentially.
I think this is pretty basic in terms of general vim commands. Navigation and insertion are all basic use cases that beginners should know. That said, the section about composing commands was really interesting!
You can read Pro Vim or Modern Vim as well.
Learn vim For the Last Time: A Tutorial and Primer
https://danielmiessler.com/study/vim/
Mastering the Vim Language
https://youtube.com/watch?v=wlR5gYd6um0
An Incremental Approach to Vim
Example (cursor on the last arg):
importAntigravity(usePython, crashTheUniverse)
<type daa for "delete an arg">
importAntigravity(usePython)I use it all the time! The only reason I don't use Spacemacs is the startup time/GUI lag, but if it works for you keep at it!
One thing I'd suggest for the author, based on a quote from the article - "Invest your time to learn new skills" - Try and invest some time to learn touch typing.
I worked as a developer for years thinking that it wasn't worth the effort, but after taking a couple of weeks to learn, I never looked back. My productivity, particularly with Vim has jumped leaps and bounds.
If you don't want to move, you can just hit Alt+l as the "default" escape.
https://www.eduardobautista.com/escape-key-alternatives-in-v...
You may like to also try `CTRL+[`. It's the telnet escape character and seems to be a general substitute for ESC on OSX, Windows, and I think Linux too. I find it pretty quick since my hands can work together.
Another handy tip I ran across was to remap CAPS LOCK to CTRL. Quite nice for both Vim and Emacs users once you get used to it.
Man, that'll definitely save my pinky from cramping up every so often.
Even I, an Emacs user who occasionally uses vi am familiar the material in this.
Can anybody suggest any real intermediate vi material?
I believe people with poor keyboards don't even have escape keys any more though
I'm happy with the generic HP keyboard I'm using at the moment, circa 2014, and my T410s old style thinkpad keyboard.
noremap! kj <esc>
it means when you press kj [0] that it's the same as pressing escape.. very handy (the times when I deliberately want to press k and then j are still zero)
[0] https://github.com/patrickdavey/dotfiles/blob/335254dce90d5c...
You -do- have the useless, enormous capslock key remapped to control, right? ;-)
Anyway, it's common to remap jj or jk to esc.
Perhaps it's navigating the :help window you find cumbersome?
As far as I'm concerned, the following is an non-negotiable requirement for vimrc.
source /usr/share/vim/vim80/defaults.vim
set noincsearch
set scrolloff=0
set mouse= " Workaround stupid defaults.vim behaviour
if filereadable("/usr/share/vim/vim80/defaults.vim")
source /usr/share/vim/vim80/defaults.vim
endif
let g:skip_defaults_vim = 1
" Disable stupid seeking to last position
autocmd! vimStartup BufRead
" Disable stupid mouse support
set mouse=If you are on twitter, you might want to follow MasteringVim. You'll learn one trick every day.
I think I agree now that the title isn't quite right—my intention was to help beginners get towards some intermediate topics.
BTW, even though I've used vim for a long time and consider myself fairly advanced, I had somehow forgotten about "I". You've saved me countless "^i" invocations... Wait... That's cording! It's cheating :-) (But it's OK because I'm a closet Emacs fan)
We are in the field of connecting Qualified Home/Online Tutors since 2005 with an extensive experience in dealing students and tutors in a familiar way.
• Trustworthy Home Tuition Providers • All Major Cities Of India • Over 12 Years Of Impeccable Service • Qualified And Verified Home / Online Tutors • Provided Tutors to Thousands Of Students Since 2005 • Our Students Are From Prestigious Corporate Schools and Colleges • Having more than 1,00,000 tutors in around In India • International Experience Trainer in Corporate Teaching
For more information: https://www.cityhometution.com/ WhatsApp : +91 9948464333
Shift+k on a word to enter the corresponding man-page if such one exists :)