Vim

Vim is the muscle that keeps growing if you train it

Some common usages beyond basics

Keep expanding my knowledge on vim commands and they are amazingly helpful but only until you are willing to learn and discover new tricks. Vim needs you to have some muscle memories and eventually you will look as strong as Arnold Schwarzenegger at least mentally when you are editing code like a guru.

  • ctrl- y or e move one line
  • ctrl- u or d move half page
  • ctrl- b or f move one page
  • zz/zt/zb move current line to middle/top/bottom
  • { jumps entire paragraphs downwards
  • } similarly but upwards
  • 0: Moves to the first character of a line
  • ^: Moves to the first non-blank character of a line
  • $: Moves to the end of a line
  • g_: Moves to the non-blank character at the end of a line
  • gg: to go to the top of the file
  • {line}gg: to go to a specific line
  • G: to go to the end of the file
  • %: jump to matching ({[]})

ctrl-z to switch the current window to the background and use fg to restore. Use bg and jobs to check jobs running in the background.

vscode: ctrl-shift-l Select all of the selected word in the file and edit at same time.

  • :tabnew xxx.txt Open new tab or use nvim -p 1.txt 2.txt ... n.txt
  • :tabclose to close the current tab.
  • gt or gT move forward or backward one tab or #number gt to a specific tab. CTRL-PgUp and CTRL-PgDn can be used to switch tabs just like in browser. Fortunately, they are both mapped to Mod-w and Mod-r in UHK.
  • :split and :vsplit to split the current window vertically or horizontally

  • CTRL-w h/j/k/l move to adjacent pane in the same tab

  • CTRL-w t/b move to top or bottom in the window

  • CTRL-w T move current window to a new tab

  • CTRL-w = Resize the windows equally

  • CTRL-w >/</-/+ Incrementally increase the window to the right/left/bottom/top. Takes a parameter, e.g. CTRL-w 20 >/</-/+ to resize multiple times.

Note that on my UHK keyboard, CTRL-w is mapped to Mod-c.

Some useful vim mappings

Resize the pane can never be so easy..

1
2
3
4
5
" Use alt + hjkl to resize windows
nnoremap <M-j> :resize -2<CR>
nnoremap <M-k> :resize +2<CR>
nnoremap <M-h> :vertical resize -2<CR>
nnoremap <M-l> :vertical resize +2<CR>

Better way to save and quit files without typing commands

1
2
3
4
" Alternate way to save
nnoremap <C-s> :w<CR>
" Alternate way to quit
nnoremap <C-Q> :wq!<CR>

Some tricks

I don’t know…but I’ll update after I’m familiar enough with the basics and want to explore more.

Tmux

Tmux is a useful tool to manage multiple terminal windows in only one window. One session contains several windows and one window can have several panes vertically or horizontally. A cheatsheet can be found here: https://tmuxcheatsheet.com/

First, we need to create a session tmux new -s <SESSION_NAME> with specified name or a default name if parameters are left out. Second, Create a new window in tmux type Ctrl-b c and list all existing windows with Ctrl-b w, the first available number from the range 0…9 will be assigned to it. Use Ctrl-b q to see the assigned number for each pane and Ctrl-b q <NUM> to switch to the pane. Third, Create new panes in one window and you may not have to if you are using panes in Vim.

A list of all windows is shown on the status line at the bottom of the screen.

Below are some most common commands for managing Tmux sessions, windows and panes:

Sessions

  • Ctrl-b ( or ) Switch between sessions
  • Ctrl-b d Detach from the current session
  • Ctrl-b a/at/attach -t <SESSION_NAME> Attach to the specified session or the last session if not specified.

Windows

  • Ctrl-b c Create a new window (with shell)
  • Ctrl-b p Switch to the previous window
  • Ctrl-b n Switch to the next window
  • Ctrl-b w Choose window from a list
  • Ctrl-b 0 Switch to window 0 (by number )
  • Ctrl-b , Rename the current window

Panes

  • Ctrl-b % Split current pane horizontally into two panes
  • Ctrl-b " Split current pane vertically into two panes
  • Ctrl-b o Go to the next pane
  • Ctrl-b ; Toggle between the current and previous pane
  • Ctrl-b x Close the current pane
  • Ctrl-b : Enter some commands like resizing the panes.
  • :resize-pane -D -U -L -R 20