DEV Community

Cover image for Vim A to Z - Literally

Vim A to Z - Literally

Omer Hamerman on January 06, 2020

TL;DR - ish: This post is a list of options and keystrokes in Vim, if this is the first time you're getting to know them, take your time, read som...
Collapse
 
caruso profile image
Giuseppe Caruso • Edited

Tim Pope's Vim Unimpaired has a nice shortcut (among some other very useful ones) to add empty lines above or below current one:

There are linewise mappings. [<Space> and ]<Space> add newlines before and after the cursor line. [e and ]e exchange the current line with the one above or below it.

Collapse
 
omerxx profile image
Omer Hamerman

This is fantastic. That's why I love this community. Thanks buddy

Collapse
 
caruso profile image
Giuseppe Caruso

Thanks to you for taking the time to write this article. #vim rocks.

Collapse
 
worc profile image
worc

B and E might be custom commands on your end? on a completely unmodded version of vim on my mac those commands aren't working. but the regex-ish ^ and $ definitely work to move you to the beginning or end of the line in command mode.

stackoverflow.com/questions/105721...

Collapse
 
xexyl profile image
xexzy

They're not custom, no.

Try :help B' and:help E'.

Hint: WORD versus WORDS.

And 0 also takes you to the beginning of the line if you didn't know.

Collapse
 
caruso profile image
Giuseppe Caruso

A small but important clarification: 0 brings the cursor to the beginning of the line, while ^ brings it to the first character of the line. I.E. the first character, no matter if it's at column 1 or 1+n (that's is no matter what's the indentation of the line).

Collapse
 
worc profile image
worc

weird, they don't work on mac vim out of the box. there must be a setting that gets flipped or changed that's not mentioned here.

Thread Thread
 
omerxx profile image
Omer Hamerman

Hey @worc any chance for an exact version?

Thread Thread
 
worc profile image
worc

a vim --version prints this:

VIM - Vi IMproved 8.0 (2016 Sep 12, compiled Jun 19 2019 19:08:44)
Included patches: 1-503, 505-680, 682-1283, 1365
Compiled by root@apple.com
Normal version without GUI.  Features included (+) or not (-):
+acl             +file_in_path    -mouse_sgr       +tag_old_static
-arabic          +find_in_path    -mouse_sysmouse  -tag_any_white
+autocmd         +float           -mouse_urxvt     -tcl
-balloon_eval    +folding         +mouse_xterm     -termguicolors
-browse          -footer          +multi_byte      -terminal
+builtin_terms   +fork()          +multi_lang      +terminfo
+byte_offset     -gettext         -mzscheme        +termresponse
+channel         -hangul_input    +netbeans_intg   +textobjects
+cindent         +iconv           +num64           +timers
-clientserver    +insert_expand   +packages        +title
-clipboard       +job             +path_extra      -toolbar
+cmdline_compl   +jumplist        -perl            +user_commands
+cmdline_hist    -keymap          +persistent_undo +vertsplit
+cmdline_info    +lambda          +postscript      +virtualedit
+comments        -langmap         +printer         +visual
-conceal         +libcall         -profile         +visualextra
+cryptv          +linebreak       +python/dyn      +viminfo
+cscope          +lispindent      -python3         +vreplace
+cursorbind      +listcmds        +quickfix        +wildignore
+cursorshape     +localmap        +reltime         +wildmenu
+dialog_con      -lua             -rightleft       +windows
+diff            +menu            +ruby/dyn        +writebackup
+digraphs        +mksession       +scrollbind      -X11
-dnd             +modify_fname    +signs           -xfontset
-ebcdic          +mouse           +smartindent     -xim
-emacs_tags      -mouseshape      +startuptime     -xpm
+eval            -mouse_dec       +statusline      -xsmp
+ex_extra        -mouse_gpm       -sun_workshop    -xterm_clipboard
+extra_search    -mouse_jsbterm   +syntax          -xterm_save
-farsi           -mouse_netterm   +tag_binary
   system vimrc file: "$VIM/vimrc"
     user vimrc file: "$HOME/.vimrc"
 2nd user vimrc file: "~/.vim/vimrc"
      user exrc file: "$HOME/.exrc"
       defaults file: "$VIMRUNTIME/defaults.vim"
  fall-back for $VIM: "/usr/share/vim"
Compilation: gcc -c -I. -Iproto -DHAVE_CONFIG_H   -DMACOS_X_UNIX  -g -O2 -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1
Linking: gcc   -L/usr/local/lib -o vim        -lm -lncurses  -liconv -framework Cocoa
Collapse
 
xexyl profile image
xexzy

Good list; I've used vi since the 1990s and exclusively vim since the early 2000s and there were some I never considered like `dip'. A few comments though.

First of all: look at vimtutor(1) (or just try vimtutor at the command line).

Rather than use what would be in some contexts an anchor to get to beginning of line (i.e. '') you can just use `0' instead. No need to use shift that way.

G: You can also just do G to go to line .

Arrows: Do people actually use those? Interesting... I knew they worked but I've never used them. Started out with h j l k and never looked back. Maybe it wasn't even possible in vi but I've always felt it far more natural to use the same area as if you were typing than to have to move your hand away. As for the h j l k you can also specify a count (never tried with the arrow keys for the above reasons) e.g. 5j goes down five lines.

I: Actually I often enough do insert to the beginning of the line but I tend to use 0i instead. It's just as quick if not faster and it keeps in the theme of go here and do this.

Marks: just like in the pager less (and maybe more - I've not used that in decades now...)

N: But if you're searching backwards (?) it is the reverse: N goes forward, n goes back.

Opening a line without insert: If there is I'm unaware of it but I can say that's not what the O/`o' commands are designed to do - it's as you say to go into insert mode.

:qw: Another way is just ':x'

s: But surely you have however used :s? I rely on it very often. The fact it supports regular expressions (so too does searching for that matter) makes it immensely valuable.

:ZZ: Just like :x (I didn't know :ZZ)

Another you didn't mention (well - not as examples): de or 5de (etc.)

Collapse
 
a5537 profile image
A5537

Maybe it's just a typo, but since : denotes VIM command mode, the "save and quit" shortcut is ZZ in normal mode and not :ZZ. By the way, there's another normal mode combination ZQ which acts like :q! or "force quit without saving".

Collapse
 
omerxx profile image
Omer Hamerman

Thank you!
I'm honored to read such a comment from such an experienced use.

Re arrows: you'd be surprised.. people use custom settings to disable their kb arrows just for that.

Re "I" personally I use it when it comes up, and I always prefer least keystrokes, that's me..

Re Marks: I use it quite often, honestly, it's always ma and mb, rarely something else

Re: Correct! Will fix

Re s: nope, but that's why I do these posts, to learn or to be taught by the community ;)

With your permission, I'd like to add your corrections with the appropriate credit

Collapse
 
caruso profile image
Giuseppe Caruso

Finally, z doesn't take care only of folding, but also of the line position in the buffer by scrolling:

  • zz places the line in the middle of the buffer
  • zt to the top
  • zb to the bottom

Thanks again for the article! Keep on spreading the world about #vim! 🤓

Collapse
 
omerxx profile image
Omer Hamerman • Edited

Important clarification and additions, with your permission I'd like to add your comment with a credit :)

Collapse
 
caruso profile image
Giuseppe Caruso • Edited

Then, you can also add H for moving cursor, without scrolling the page, to the Head, T to the Tail and M (I am not sure by hearth, check this last out) to the Middle. 😉

Collapse
 
caruso profile image
Giuseppe Caruso

My pleasure.

Collapse
 
aminnairi profile image
Amin

Hey there, thanks for your article. A good refresher!

By the way, I may be wrong but I think that you can open a new line above by just pressing O instead of o. But maybe I didn't quite catch what you wanted to do.

Collapse
 
xexyl profile image
xexzy

They mean without going into insert mode. What is being said is both O' ando` take you into insert mode and often a new line without insert mode would be useful.

Probably it isn't that way because by adding a line you're technically inserting into the file itself.

Collapse
 
simonced profile image
simonced

Nice article.
Continuing with z, zf will create a fold on what is selected.
za will toggle folds. I use those all the time.

Also, zl will scroll right (good for long lines).
and zh goes left.
You can also scroll more than 1 character at a time like so:
z20l.

Happy Viming.

Collapse
 
omerxx profile image
Omer Hamerman

Thanks for the additions! Some stuff, like folds (which I also use from time to time) I left out to try and focus on things that matter and make phonetic logic. I linked to my Vim notebook on Github where it goes further to explain.

Collapse
 
winstoncodes profile image
winstonCodes

Great list! One correction: B and E move by word blocks (like W) and not to the beginning or end of the line.

Can't wait to see more of your Vim-related content. Learning Vim has been one of the funnest sagas in my development journey.

Collapse
 
omerxx profile image
Omer Hamerman

Thanks!
You are 100% correct, I think most / some Vim "flavors" (non-vanilla Vim configurations people start with) are already preconfigured with B / E actually targetting the end / beginning of the line. I'll double-check myself and fix, but as far as the docs go you are totally right!

Regarding the next posts: I have one waiting to be completed discussing macros. It would be much shorter though :)
If you've got any ideas / things you'd like me to cover let me know! I'm getting to learn so much myself doing these it's incredible

Collapse
 
argon2008aiti profile image
argon2008-aiti

J: Join line below cursor to current line.

I find myself using this often when refactoring code.

Collapse
 
omerxx profile image
Omer Hamerman

You're right! Using it myself quite often. Adding to the list.

Cheers

Collapse
 
jacobherrington profile image
Jacob Herrington (he/him)

This is awesome! Thanks 😁

Collapse
 
omerxx profile image
Omer Hamerman

You're welcome! :)

Collapse
 
hamidrezakp profile image
Kaveh

Hi. Very good list but you have a mistake:
dg - "delete from here up" must be dgg.

Collapse
 
omerxx profile image
Omer Hamerman

Thanks man!
Fixed

Collapse
 
j0ssue profile image
Josue Diaz

F for search is one I never use but will start to use thanks to this post

Collapse
 
mattthecuber profile image
MattTheCuber

"the [j] key on a keyboard happens to be quite far considering its extensive use"
It's literally the right hand pointer finger...