Skip to main content
  1. Posts/

Vim Tips

·714 words·4 mins
Author
Yang Hu

Search and work with many files
#

Searching
#

For searching words / symbols within files, see search-in-vim

To quick find and open files by filename, I use fzf and coc-list, and the follwing key bindings.

General search

To open files in the working directory ( not current file/buffer dir), use :Files or ,lc ( list current)

I also created a quick command ,lf (list file) to start from current buffer’s folder, and you can type the path before start the search.

Lastly, there’s :Buffers / ,b to list opened buffers list to quickly jump between buffers. It’s very useful if lots of files are opened. However, using quickfix lists or markers, or :Lines / ,l* to precisly jump to a line may often be more efficient.

Git related

To open files in current git repository, use :GFiles or ,gc ( git content ). This uses the files list of git ls-files which respects .gitignore.

To only check files that are modified (in git status results), use GFiles? or ,gs (git status).

Jumping between buffers
#

Move between buffers

Buffers are numbered and you can use n <C-^> to quickly jump to the buffer with number n. To jump to previous buffer, simply use <C-^>.

Sometimes, <C-o> / <C-i> to move back/forward in jumplist can also be useful for moving between buffers, but with less precision.

If there’s only a few buffers and the ones you want to move to is next to each other, I mapped <C-p> / <C-n> to move to previous/next buffer.

Moving with search

When you need to work with some symbols, usually a search is done and a quickfix list would be available ( see search-in-vim for more details). With that, you can use the quickfix list to help move between search result locations, which is even faster. See how to work with quickfix list for more details

Moving within a large buffer
#

Markers

If there’re a few “hot” spots in a buffer that you visit frequently, using marks would make jumping to those locations a breeze. When jumping, 'a jumps to start of the line, `a jumps to the exact location. (:h mark-motions)

  • ‘a - ‘z lowercase marks, valid within one file
  • ‘A - ‘Z uppercase marks, also called file marks, valid between files

motions

  • g; / g,: Go to previous/next change list location.
  • [ and ] jumps: Square brackets followed by a character jumps in code blocks.
    • { } ( ) Brackets: Jump to previous/next unmatched bracket. This is useful to jump to start/end of the current code block in some languages.
    • m / M Functions: Jump to previous/next function start/end. m means start. M means end. This works with files in Java’s structure, with a class and methods defined inside it.
    • #: #if #else #endif macros. Jump to previous/next if/else/endif macro lines.
    • *: C comment blockes. Jump to the start/end of C comment block /* or */.
  • ( / ): Jump a sentence. Jumping words (w/b) and paragraphs ({/}) is used a lot in coding. While jumping by sentence is useful when working with large text, like writing documents or long comments.

Quickfix List

Quickfix list isn’t only used for serach results: it is also used by make command to list errors/warnings with locations of this issues. Jumping withing the file using the quickfix list can quickly locate and address errors reported from those sources.

Outline

Git Merge
#

Buffers
#

  • {count}Ctrl-^: Jump to buffer number count. Very useful with tabline where buffer’s numbers are shown on top.

Insert mode
#

Motions

  • Ctrl-c: Enter normal mode.
  • Ctrl``O: Temporarily switch to normal mode for a single motion.
  • C-x + C-e/C-y: Scroll windows up/down. Ctrl-x enters scroll mode, and susequent c-e or c-y scrolls the screen without moving the cursor.

Deletions

  • Ctrl``U: Delete all entered characteres in the current line. If there’re no newly entered chars, delete to beginning of cursor.
  • C-w: Delete a word backwards.
  • C-h: Delete a character

Insertions

  • C-i or Tab: insert a tab
  • C-r: Insert contents of a register. Some special registers
  • C-t/C-d: Insert/delete indent. 0 C-d delete all indentations.

Completion

C-x enters the completion mode, and subsequent input triggers completion for different types

  • C-L: Whole lines
  • C-F: file names
  • C-N: keywords in the buffer
  • C-K: Dictionary
  • C-]: Tags
  • C-D: Definitions
  • C-O: omnifunc