Navigation in Vim

Navigation in Vim

I have seen that many people use the Arrow Key in Vim…
emmm…
Therefore, I will show my way in this article.
Of course, Your way is also welcome in the comment area.
( <C-x> means <Ctrl>x
<M-x> means <Alt>x)

Quick Move Like Mouse

Don’t move your fingers!

  • j or k or h or l: You can move the cursor without moving you fingers!
    Vim can help you to get rid of the mouse, but NOT by the Arrow Key!
    Unplug the Arrow Key in your keyboard or…buy a keyboard without Arrow keys! (Explain below)

  • The <Leader> key is one of the most important key in Vim.
    Let it just be <Space> please.
    So you don’t have to move your fingers.

  • The <Ctrl> key is also one of the most important key in almost all software.
    But…wait a minute…the most useless key <Caps Lock> sleep in the base line of the keyboard?
    Please…swap them!
    (By the Gnome Tweak Tools or the setxkbmap command.)
    But…how can I switch the case of letters?
    Besides <Shift>, there are many efficient ways in Vim.

  • Buy a 60% keyboard with Unix Layout.
    Unix Layout means… <Ctrl> is close to A’s left.
    If you have a little money, just buy a HHKB!
    Or…buy a ‘GH60’ is awesome because you can add a vim mode to your keyboard!

Build-in Keys

  • <C-u> or <C-d>: Move half page up or down.
  • gg or G: Move to the first line or the last line.
  • ^ or $: Move to the ‘first’ column or the ‘last’ column in the current line.
  • 66G or 6j: Move to the specific line by your eye’s quick calculation. (hhh)
  • <C-o> or <C-i>: Move to the last or the next position.
  • * or /word: Move to the next or the last specific word.
    The n and N will navigate you among those same words.
    (Could be enhanced by incsearch.vim.)

  • fa or Fa: Move to the next or the last a in the current line. ( fb, fc, …)
    The ; and , will navigate you among those same characters.
    No kidding you, f is my most frequently key. (Not the ‘Fxxx’ word.)

EasyMotion

OK. Now I assume you have fall in love with the f key (not the ‘f’ word).
The vim-easymotion plugin will let you two get married, trust me.

  • <Leader>fa: Move to any ‘a’ in the current page (NOT only the current line!).
    All ‘a’ will be highlighted with different red characters, try to press one of them.

  • <Leader>j: Move to any line below in the current page.
    Likes 6j, but…without your eye’s quick calculation. (hhh)

  • <Leader>k: Move to any line above in the current page.

Files Navigation

Buffers

Just use the beautifully vim-airline and the handy vim-unimpaired and the powerful vim-ctrlspace and a little plugin vim-bbye.

  • Buffer is just a buffer of the file’s content.
  • Window is just a window which displays the buffer.
  • Tab is just a container of windows.
    Each tab has it’s own windows layout, which is similar to Workspace.
    But all tabs share one buffers list, which is not similar to Workspace.
    That is why we need vim-ctrlspace.

  • [b or ]b: Move to the last or the next buffer of the current tab.
  • <Leader>q: Close the current buffer without close the window.
  • <C-s>: Open the buffer list of the current tab. (Followed by f``j``k)

Directories

I also use NERDTree until I read this article which drive me to vim-dirvish

  • -: Open the current directory in a new buffer.
  • t: Open the selected file in a new tab.
  • :!mkdir %foo: Create directories.
  • :e %foo.txt: Create files.
  • :'<,'>!xargs du -hs: Pipe to :! to see inline results.

More powerful quick jump

Keywords

CtrlSF

YES! The ‘f’ key again!

  • <C-f>n: Find the word under the cursor.
  • <C-f>w: Find the word you type.
  • <C-f>o: Toggle the quick search window.

EasyGrep

  • <Leader>vv: Grep for the word under the cursor.
  • <Leader>vr: Perform a global search and replace on the word under the cursor.
  • :Grep [arg]: Grep for the specified arg, like <Leader>vv.
  • :Replace [target] [replacement]: Perform a global search and replace, like <Leader>vr.

Tags

Generate ctags and gtags

Replace Exuberant Ctags by Universal Ctags.

Install Pygments in Python.

Expand Universal Ctags by Gtags.

Generate tag files automatically by Gutentags.

Navigate with ctags

Build-in Keys and vim-unimpaired

  • <C-]> or <C-w>] or g<C-]>: Jump to tags with the name under the cursor.
  • <C-t>: Return after a tag jump.
  • [t or ]t: Jump to the previous or the next tag with the same name.
  • :tag <tagname> or :tag /<pattern>: Jump to a tag with the specific name.
  • :tag: Jump to a previously jumped tag location, which is stored in the tag stack.
  • :tags: List the contents of the tag stack.

LeaderF

OH NO! The ‘f’ key again!

  • <C-f>f: Find functions.
  • <C-f>t: Find tags.
  • <C-f>l: Find lines.
  • <C-f>b: Find buffers.
  • <C-f>p: Find files.
  • <C-f>u: Find the MRU files.
  • <C-f>c: Find cmd history.
  • <C-f>s: Find search history.
  • <C-f>h: Find helps.

Navigate with gtags

Build-in Keys

Gutentags Plus

  • <Leader>gc: Get functions calling this function.
  • <Leader>gd: Get functions called by this function.
  • <Leader>gs: Get this C symbol.
  • <Leader>gg: Get this definition
  • <Leader>gf: Get this file.
  • <Leader>gi: Get files #including this file.
  • <Leader>ge: Get this egrep pattern.

Preview

vim-preview

  • p or P: Open or close the preview window of the current quickfix-list item (tags).
  • <M-u> or <M-d>: Scroll the preview window up or down remotely.

Semantic

Welcome to LSP!

Backend

Install cquery.

Frontend

LanguageClient-neovim

  • <Leader>td: To Definition of the symbol under the cursor.
  • <Leader>gt: Get Type of the symbol under the cursor.
  • <F2>: Rename the symbol under the cursor.

One thought on “Navigation in Vim

Leave a Reply

Your email address will not be published. Required fields are marked *