Wizardly Tips Vim (full version on Github)

As promised here are the much wanted Vim tips. This is just a SNIPPET.

Full tutorial freely available on github (which will be updated regularly, this post not so much)

Wizardly Tips Vim

A fast way to benefit from vim’s power. Ideally you should read a good book and the :h section. But if you’re in a hurry this should serve you well.
This is a list of somewhat unknown but highly useful Vim tips and tricks. I try and keeep the list succint by not showing well known or less useful commands.

HOW TO READ SHORTCUTS:

<Esc> – Escape. Usually you should press the key, not type it. When you see ii<Esc> you type ii and then Escape, not ‘ii` literally.

<C-g> – means Ctrl+G

g<C-g> – means press g then press Ctrl+g

<S-g> – means press SHift+g

<C-Z> – means Ctrl+Shift+Z (Shift is not shown but implied since we have an uppercase Z)

<zZ> – z followed by Shift+Z

<C-m><CR> – type Ctrl+M followed by Enter. <CR> is Carriage Return (another word for Enter key)

Find files and send them to vim.

find ~ -size +10M | vim -
  • find files and open the results in vim. While in vim you can press gf to open a file (from the results provided by find) in vim.
  • HINT: if your file has spaces vim will complain that the file is not in the path. This happens because it truncates the file when a space appears, thus trying to reach the file at an erroneous path. So instead use <S-v> to select the hole line and then press gf
  • HINT2 – you can use any other command that outputs to stdout and send it to vim.
  • HINT3 – you can open the file under cursor in a new window with <C-w>

Encrypt

  • You decide to write your secret journal using vim. Trouble is – anyone can read your unencrypted text files. Or can they? By using vim -x mysecretfile.txt you use encryption on file. You’ll be asked for a key (password) to encrypt/decrypt your file. Warning – if you forget your key bye bye file.
  • HINT – you can also encrypt a file after you open it by typing :X

  • I’m still amazed by how many vim users totally ignore the power of register playback. That is – you record your actions into a register and play those actions back. It’s a bit weird to wrap your head around it but once you do – you’ll be amazed.
    Here’s how it works
  • press qa to start recording your actions into register a
  • Perform some actions that you want repeated. Here’s a very useful example for coders – custom line by line editing. Let’s say that you need to go line by line, search for the word “foo” and place it in tags () and place those tags again on the same line at the end of the line.
  • This is a pretty complex scenario. Doing it by hand for thousands of lines it’s PITA (Pain In The A*s). What can you do? Write an awk script? A Python script? NOOOOO. You use vim register playback.
  • So you start your action by implementing a movement that will go to the next line or the next pattern. This is very important because it will make the playback “smart”, as it will go on the next line or pattern and do the work.
  • In your case you would start by typing /foo and pressing <CR> (Enter). This takes care of always finding the next pattern and working on it.
  • No do your desired operations. i<<Esc>ea> which will insert the bracket, then to normal mode, go te end of the word, append the closing bracket.
  • next you copy the whole bracketed foo by pressing ya> (yank all in between brackets) followed by $p (go to end of the line and paste)
  • Your work is done. You press q to stop recording.
  • time for PAYBACK …ah, I meant PLAYBACK.
  • you press @a to playback the contents of register a (into which you recorded your actions). You watch amazed how vim repeats “smartly” the operations you just performed previously.
  • Now you can press @@ to repeat the previous playback as many times as you want. Or you could press 10@@ to repeat the playback for 10 times. Or if you want this applied to your whole file type :% norm @a – which means – apply the macro to all lines.

Open files in own tab/win

  • Your girlfriend’s father comes to visit you. You want to impress him by opening a bunch of text files, each in it’s own window in vim (and show him that you’re very smart and tech savvy)
  • You write vim -o f1 f2 f3 f4 to open the specified files with windows split horizontally. use vim -O f1 f2 f3 to split them vertically. If you have lots of files and you dont want a hundred narrow windows use vim -o5 *.txt to open a max of 5 windows (horizontally stacked).
  • You could instead open them in tabs by using the same syntax vim -p f1 f2 or vim -p5 *.txt

Info

  • The <C-g> magic – it displays file name and position. g<C-g> will display number of words, num of lines and other useful stats.

Enter special chars

  • Enter digraphs (speciar chars). Use <C-k> in Insert mode and type the digraph code. For example you can enter the pound sign using ‘Pd’ (£). Type :h digraph-table for more info. To just view the codes type :dig

Quickly write buffer to disk

  • ZZ – write the buffer to disk if modified and exit. I bet you didn’t knew that – but if you did BRAVO, you know how to quit Vim!

Sort

  • :%!sort -u – sort all lines and remove duplicates. % refers to the range and it means all the lines. You could use 1,5 for example and it would only sort lines 1 to 5. The ! in this context means – execute an external shell command (in our case sort) with the range as stdin, when you’re done put back in place the stdout from that command.

Paste register

  • While in insert mode press <C-r> followed by a registry, such as a or b or @ or : or +. eg: <C-r>a. This will insert the contents of the registry at cursor.

Insert mode navig

  • While in insert mode press <C-h> to backspace a char, <C-w> to delete a word backwards, <C-u> to delete until the beginning of the line (all without leaving Insert mode)

Autocomplete

  • Did you knew you also have autocomplete by default? While in insert mode press <C-n> or <C-p> to cycle through possible autocompletions.

How to quit Vim

  • There are lots of famous memes about people not knowing how to quit Vim. Here’s a little secret – you can quit the current buffer(if you opened just a single file) without writting to disk by pressing ZQ.

Repeat Insert

  • Everyone knows that you can insert a character repeatedly using a command such as 20i-<Esc> – which will insert 20 ‘-‘. Or you could do something like 10aHello World Of Vim.<Esc>. This will do 10 appends of the phrase ‘Hello World Of Vim’

THIS IS JUST A SNIPPET. Full Tutorial on github, freely available and regularly updated.

All Hail VIM Supreme.

You know the meme about Vim? The one where people are stuck for an eternity in Vim because they don’t know how to quit?

How to Exit Vim Editor - Stack Overflow Blog
source

My question is: why would you ever want to quit Vim? 😀

I’ve heard some people say that you have to be autistic if you want to know well how Vim works. Why would you ever need anything else aside from Notepad++?

(this reminds of another famous prediction – probably not based in reality but still nice – involving 640k of RAM that ought to be enough).

Ha!

I’ll tell you why. Because 90% of time doing devops/dev you’ll be spending it inside a text editor or terminal (better yet – a text editor with a terminal win inside it – which Vim can do).

It’s all about text in development. Everything is text. Everything is text manipulation. Especially for a devops engineer, sysadmin or even software developer.

Scripts, config files, documentation, logs, infrastructure. Yeah, even the damn infrastructure is text (for the purposes of management and provisioning data centers and hardware).

Knowing this how can one claim that a text editor is not important? Or ever worse – that it’s alright to be ignorant of the power of your text editor of choice?

I’d say that the text editor is the most important piece of software that you will ever learn. Libraries come and go. A good text editor though? Will be with you for decades. You’ll use it daily and it’ll occupy a huge chunk of your work hours.

“But, but I can use an IDE these days. They have nice GUI’s, shiny graphics, mouse controls and a very easy learning curve.”

Try to use an IDE over ssh (in theory you could with ssh -x, in practice, not so much). See how easy that goes.

(VsCode has remote editing capabilities using a ssh plugin. Quite good and easy to setup. It might be what you need in certain situations when you have to use an IDE)

Far from me to say that IDE are useless. They have their established place in the hierarchy of software development and should be used accordingly (VsCode is very good).

If you have to use an IDE then use an IDE. But whatever you use – text editor or IDE – know it well! Know your tools my friend and your tools will reward you. Be ignorant of your tools and most often than not they’ll bite you in the a*s.

“But even if I have to ssh and work remotely I can still use nano. I don’t want to bother with vim. It’s too complex for me”

Complex it may be but the more powerful for it’s complexity. It’s true that it will take a brave and stout heart to learn vim well. But the rewards are commensurate. And don’t forget that you are actually making an investment when you learn Vim.

Let me put this way. Let’s say you decide to be a carpenter for the next 20 years. How well should you learn to use carpentry tools? Will the hours spent learning the tools be worth the next 2-4 decades of usage?

If you are serious about your profession you should be serious about your tools.

I’ll publish soon a compiled list of fairly unknown (but powerful) Vim tips and tricks for you, my reader. This way I hope to entice you to what is one the best text editors in the solar system.