
Vim tricks - fanf2
https://www.hillelwayne.com/post/intermediate-vim/
======
godelski
There's a few commands I LOVE but find frequently unmentioned.

In insert mode, <C-x> is a contextual completion. So like the normal <C-n> or
<C-p> you just do <C-x><C-n> and you can keep hammering the <C-n> to fill in
multiple "words". You can also complete lines, file paths, dictionary words,
thesaurus words, tags, spelling, etc. See `help ins-completion`. Seriously, I
can't do without this command. <C-x> needs to be in everyone's daily use.

Also in insert mode: <C-w> deletes a word back. You'll commonly find this is a
lot of editors and stuff where it doesn't close a window. Similar to
<C-backspace>, though I many times accidentally hit <C-w> in my browser
instead of <C-backspace> because I use this so much (bad speller).

<C-[> much easier than pressing Esc

0 much easier than pressing ^ (honestly I have anything that is $,%,^,&. They
aren't easy to reach)

Honestly, vim has some of the best written documentation I've read. If you
really want to master vim, just read the docs on commands you already use. I
guarantee you'll find something you didn't know.

~~~
Exuma
can you give more examples of <C-x><C-n> in code editing? I have tested that
out in vim and its cool but i cant think of times I want to repeat a series in
another part of the document, perhaps I'm just very uncreative right now

Thanks

~~~
godelski
Totally. But I will tell you, <C-x> is a habit built around the other options
too. So I just use <C-x> on any completion (which I frequently use
<C-x><C-f>).

Here's a dumb example (dumb because there's other ways to do it that are
better)

Let's say I'm overloading a function.

void foo(int a, int b) {}

I could do v<C-x><C-p> until I fill in the b, then type ", int c)". Now it'd
be totally easier to do "v<C-x><C-l> backspace , int c)", but that's a
different point.

Maybe this one is a little more useful

"def _<C-x><C-p><C-x><C-p>" may expand to "def __init__(self" Note that here
I'm holding down ctrl and just hitting "xpxp"

Honestly I end up finding a lot of patterns in my code just because I started
thinking about it this way. It is more one of those things where once you
start thinking about it with it in mind you will find patterns.

It is also an easy habit to develop, if you already use <C-n>, because when
you press ctrl you just also hit x and keep your pinky down on ctrl. I feel
like there is just no downside to using <C-x> because in reality it is just
one more keystroke, which is done with the other hand (so even less increase
in work).

~~~
Exuma
Dude! That is awesome... it makes sense now. I totally didn't even know bout
<C-x><C-l> either!

Do you rebind your caps to control as well? That was another thing I did which
is 10/10

~~~
godelski
Yeah I rebind keys and write my own functions. I have a preference for
distilling things that I use out of plugins[0] and into just vanilla vim (I
still use plugins). I'll share my configurations[1]. I don't document it well
btw and there's frequently changes. But I find that looking at others rc files
many times gives me ideas that I hadn't seen before. Also, if you have any
suggestions I'd love to hear them.

[0]see my git rebindings instead of using tpope's plugin

[1][https://github.com/stevenwalton/.dotfiles](https://github.com/stevenwalton/.dotfiles)

~~~
Exuma
Awesome! Here's mine. I am busy atm but I will circle back and tell you my
favorite tricks.

Read my README for most of my favorite tricks.

[https://github.com/9mm/dotfiles](https://github.com/9mm/dotfiles)

Things that come to mind

\- ZSH-autocomplete is probably by far my favorite thing ever \- iterm3 full
overlay with <C-i> means that i have a full overlay terminal immediately
available no matter what im doing. No messing with stupid windows \- i made my
own custom theme etc you can check out

Vim favorite things

\- `gx` opens the current URL under the cursor in your browser \- `gq` for
word rewrapping comments on the fly \- `Bda!` look in my .vimrc for Bda! which
deletes all open buffers, useful when youre working and have 40 open buffers
you forgot about and want to start fresh

I'll think of more later!

------
binaryapparatus
"Exobrains, on the other hand, stuff Vim full of plugins, functions, and
homebrew mappings in a vain attempt to pretend they’re using Emacs."

This is _disturbingly_ accurate from my point of view. I keep wondering if I
should finally jump ship and stop tweaking vim with questionable results.
Spacemacs? Pure emacs + evil? Suggestions welcome.

~~~
m45t3r
I particularly prefer doom-emacs [1]. I migrated from Vim to Spacemacs,
however doom-emacs is leaner, faster and has better evil integration. For
example, Spacemacs has no mapping for evil-numbers, however doom-emacs maps it
to Ctrl+A (increase number) and Ctrl+Shift+A (decrease number) [2].

However, you will probably need to do some customizations in doom-emacs.
Spacemacs works mostly only using its internal layers, however when you try to
do something that isn't included already... Well, it is less than optimal.
doom-emacs assumes that you will want to customize things.

[1]: [https://github.com/hlissner/doom-
emacs/](https://github.com/hlissner/doom-emacs/)

[2]: And as for why it isn't Ctrl+X to decrease number like in Vim, it is
because Ctrl+X is probably the most important keymapping in Emacs itself.
However I find Ctrl+Shift+A quite good because it follows the Vim convention
of the reverse of some operation is on Shift.

~~~
theCodeStig
I recently moved from Spacemacs to Doom. I second, that it’s so much better,
in that it’s faster, leaner, and easier to comprehend. Some of the keybindings
could be more thorough. I find myself M-x’ing a lot. I take this as an
acceptable trade off, as keybindings are easy to set on one’s own.

~~~
m45t3r
I like it because it is easier to add than remove. Spacemacs has so many
language specifics keybindings that, while taking less time to be productive,
also makes customization much harder thanks to the multiple conflicts that you
will probably get when trying to map something.

However this is the trade-off. You have to spend more time in doom-emacs,
however there is less friction to customize.

------
periya
I’ve discovered q: when trying to quit vim in a hurry. You always keep finding
new quirky things to do in vim but I guess the same hold true for other text
editors like Emacs

~~~
godelski
That's how I found `K`. I was surprised the first time I got a man page open.
Now I find it pretty useful and wish it extended to more than man pages.

~~~
ronmrdechai
Take a look at `:help 'keywordprg'`.

~~~
godelski
Can I do things like get `K` to work on python commands? Including modules? Or
with other languages?

I see that I can do `set keywordprg=pydoc` but I want modules too.

~~~
matthewn
Check out vim-dokumentary: [https://github.com/gastonsimone/vim-
dokumentary](https://github.com/gastonsimone/vim-dokumentary)

------
tasty_freeze
I use gf (mnemonic: go file) all the time -- it will grab the string under the
cursor (the cursor doesn't need to be at the start of it), then attempt to
open a file with that name. You can look up the details but it uses a path
type mechanism so if the name of the file isn't absolute, it will check the
path.

~~~
AlkurahCepheus
Don't forget about its twin gx. For example on MacOS gx uses the open command
to open a file in the default application. Really helpful when you want to
open up a website quickly in your browser or you have the url to an image in a
markdown file and want to see it. Helpful trick for relativ paths:
[https://www.reddit.com/r/vim/comments/7xs8x3/opening_local_r...](https://www.reddit.com/r/vim/comments/7xs8x3/opening_local_relative_paths_with_gx/)

------
CalChris
_" :, @: ": is the register storing the last executed command. You can write
":p to print it to the buffer. @: reruns the last command._

That seems like a mouthful. I use :<up arrow> to run the last command and <up
arrow> some more to run the ones before that.

~~~
tartrate
And if you wanted to repeat that 10 times you would...?

~~~
mises
Press up ten times. That's still what a lot of us do in terminal (particularly
when we're on a machine without fancy shell history fuzzy-searching etc.).

I guess I might press and hold at ten.

~~~
grimgrin
Your machine probably has ^r for working through bash history

But that aside, if you’re into arrow pressing, a few lines in your .inputrc
can go a long ways

Test this out sometime:

    
    
      # Incremental searching with Up and Down and Left and Right
      "\e[A": history-search-backward
      "\e[B": history-search-forward
    

Now you can type “find” and press up/down arrow to just cycle through history
that starts with find

------
michaericalribo
> U is the same as u except it does the undo as a new change, which is
> functionally useless.

Unless I’m mistaken, this is the default undo behavior for emacs...What horror
to only have vim’s forward and backward undo history! Said the emacser on the
vimmer’s post.

~~~
mden
Maybe misunderstood what you meant, but vim's undo is not linear but rather
tree-like. You can undo, make a change, and still get back to the initial
state.
[https://vim.fandom.com/wiki/Using_undo_branches](https://vim.fandom.com/wiki/Using_undo_branches)

Edit: this also seems to be in the original post

~~~
michaericalribo
You are definitely right, and I still find the emacs undo confusing...but I
did think it was funny that one dev’s trash is another’s treasure—now that I
know what to expect, I even kind of like the emacs way, but when I first
switched I _hated_ it.

------
srik
Text objects are crucial to composing vim commands. Vim offers quite a few but
honestly, there’s plenty more. I’d recommend using the vim-textobj-user as
base to add appealing text objects. Personally, I can’t live without the
“entire” text object which allows one to act upon the entire file. Like
“indent entire file” etc.

[https://github.com/kana/vim-textobj-user](https://github.com/kana/vim-
textobj-user)

[https://github.com/kana/vim-textobj-entire](https://github.com/kana/vim-
textobj-entire)

------
chestervonwinch
One I learned recently transferring some LaTeX over to rST is visual highlight
=> "gq". This breaks up long lines to multiple lines based on your stored text
width. It can also be coupled with motions, e.g., "gq}".

~~~
xelxebar
Yeah. That's a really nice one. I use 'gqip' a lot when writing prose in
latex, md, etc. One really cool thing is that it respects comments, so it will
split long comment lines into multiline comments as necessary.

------
sullyj3
> If you’re constantly tweaking your vimrc, do yourself a favor and add a
> command for that:

> command! Vimrc :vs $MYVIMRC

I just use marks for this. Capital letter marks work across files, I use 'I
(for init.vim, I use neovim).

------
pugworthy
Is it bad that I've used Vi/m since the early 80's, and I still don't know (or
really feel like I need to know) a lot of tricks?

Sometimes efficiency is not about "tricks"

------
m0zg
The problem is, how do you remember all of this stuff? Tabdo/bufdo is as
advanced as it ever gets for me.

~~~
Exuma
You have to make a deliberate attempt to learn it. What i would do is when
coding I would focus on a single command and do it 5 or 6 times until it was
engrained, then I would go to the next one. I spent about 15 days straight
fairly intensively learning vim .. taht was 8 years ago. Best decision I have
ever made in my entire programmign career. Now I pick up new vim commands like
I am multilingual in 20 languages. All the commands I can usually remember
without even typing them, they just stick. It definitely has the rolling
snowball momentum effect

~~~
m0zg
At my billing rate "15 days straight" is approximately equal to a low end BMW.
So I think tabdo/bufdo will have to do for now. At least until I get a
salaried job and can learn the rest at my employer's expense.

I do think there's Pareto principle at play with all this obscure stuff.
Knowing 20% gets you 80% of the benefit, every additional percent gets you
exponentially smaller fraction of the remaining 20%. That's how I play it
anyway. I don't feel like I'm any less productive with my 20%.

~~~
tambourine_man
>At my billing rate "15 days straight" is approximately equal to a low end
BMW.

So, you’re saying you are making too much money to learn something new?

~~~
m0zg
Oh no, I learn "something new" every day. It's just not related to text
editing. Don't get me wrong, I'm pretty good at editing in vim. After all,
it's been my editor of choice for the last 15 years or so, and I see no reason
why it would not remain in that role indefinitely. I'm just not convincing
that learning the more obscure stuff is valuable.

------
dredmorbius
Clickbait title conceals a decent article.

~~~
dang
Changed now.

