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.
Navigate in a file
ctrl- y or emove one linectrl- u or dmove half pagectrl- b or fmove one pagezz/zt/zbmove current line to middle/top/bottom{jumps entire paragraphs downwards}similarly but upwards0: Moves to the first character of a line^: Moves to the first non-blank character of a line$: Moves to the end of a lineg_: Moves to the non-blank character at the end of a linegg: to go to the top of the file{line}gg: to go to a specific lineG: 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.
Navigate between tabs
:tabnew xxx.txtOpen new tab or usenvim -p 1.txt 2.txt ... n.txt:tabcloseto close the current tab.gt or gTmove forward or backward one tab or#number gtto a specific tab.CTRL-PgUpandCTRL-PgDncan be used to switch tabs just like in browser. Fortunately, they are both mapped toMod-wandMod-rin UHK.
Navigate between panes
:splitand:vsplitto split the current window vertically or horizontallyCTRL-w h/j/k/lmove to adjacent pane in the same tabCTRL-w t/bmove to top or bottom in the windowCTRL-w Tmove current window to a new tabCTRL-w =Resize the windows equallyCTRL-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 sessionsCtrl-b dDetach from the current sessionCtrl-b a/at/attach -t <SESSION_NAME>Attach to the specified session or the last session if not specified.
Windows
Ctrl-b cCreate a new window (with shell)Ctrl-b pSwitch to the previous windowCtrl-b nSwitch to the next windowCtrl-b wChoose window from a listCtrl-b 0Switch to window 0 (by number )Ctrl-b ,Rename the current window
Panes
Ctrl-b %Split current pane horizontally into two panesCtrl-b "Split current pane vertically into two panesCtrl-b oGo to the next paneCtrl-b ;Toggle between the current and previous paneCtrl-b xClose the current paneCtrl-b :Enter some commands like resizing the panes.:resize-pane -D -U -L -R 20