- When using the Alacritty terminal, NeoVim seems to respect the terminal's color setting out of the box.
Saves all the windows and open files (and buffers?) after closing vim so that you can recover the same setup again next time Source
- Make session:
mks ~/.vim/sessions/mysession.vim - Open a session
- In Vim,
:source ~/.vim/sessions/mysession.vim - In a terminal,
vim -S ~/.vim/sessions/mysession.vim
- In Vim,
Reference: Text-object selection
- Select a word:
viw - Select contents inside braces:
vib - Select contents inside quotes:
vi<QUOTE CHARACTER>vi"
- Move cursor to previous or next empty line:
}or{ - Indent multiple lines quickly:
>>(shift + . + .) - Insert # in front of selected lines. Stackoverflow answer:
Control + vto enter columnwise visual mode- Select desired lines
Shift + ito enter insert mode on those lines and insert #- Escape to apply changes to the selected lines
- Navigate to across location in the jump list (previous and next cursor location):
- Previous:
Ctrl + o(o for old) - Next:
Ctrl + i
- Previous:
- Recenter the line that the cursor is on:
zz - Scroll up or down:
Control + uorControl + d- To scroll one line at a time, use
eoryinstead
- To scroll one line at a time, use
- Enter visual mode:
v - Visually select entire line:
Shift +v - Start visual mode with the same area selected in previous operation:
gv
- To enter command mode, do
:in normal mode - To see your command history, do
q:- To run it, place your cursor on the command and hit enter
- To quit it,
:q
- To run a bash command in command mode, use
!- Say you want to align a visually selected group of text using the
columncommand in bash. Visually select the text then do:'<,'>!column -t
- Say you want to align a visually selected group of text using the
- Bring cursor to a character:
f<CHAR> - Bring cursor before a character:
t<CHAR>- This is useful for deleting up to a charcter (say deleting the remaining contents of a list without deleting the final brace with
dt], where ==])
- This is useful for deleting up to a charcter (say deleting the remaining contents of a list without deleting the final brace with
Sorccto delete current line and enter insert mode.3Sdeletes the next 3 linesset autoindentto start insert mode at the end of indentation
- D -> delete from cursor to end of line (characterwise)
dd will do a linewise deletion, which following a put command, P or p, will either insert the deleted line before or after the line in which the cursor is located. To insert the deleted line at the cursor position, do a characterwise deletion, 0D (0 will move cursor to the begining of the line and D will do a characerwise deletion from the cursor to the end of the line). See https://vimhelp.org/motion.txt.html#linewise
- Explore file system and open a file in current viewport split:
:Ex- To return to the previous file that you were on when you're done, do
:bp - Doing
:qwill close the viewport and lose the view into the file you were previously working on
- To return to the previous file that you were on when you're done, do
- Create a split first then explore the file system
:Sex,:Vex,Texfor horizontal, vertical, and tab splits- To exit, simply do
:q, which will close the viewport that was opened by theS, V, Tcommand prefix
Source: https://stackoverflow.com/questions/1445992/vim-file-navigation
Tabs are useful for working on a seperate part of the project without messing with your current view. For example, you can have files from folder foo/ opened across multiple windows in one tab and files from folder bar/ opened in an another tab.
- Create a new window in a new tab,
:tabnew - Previously accessed tab,
g<Tab> - Cycle through tabs,
gt - Access the tab in position [count],
[count]gt - List all windows within each tab,
:tabs - Close current tab (and all windows in it),
:tabc - For more information,
:help tab-page-commands
vims way of editing multiple files within the same vim session.
- To open a file in the same vim session, do
:e <FILENAME> - To list files in the buffer, do
:ls - To toggle across files in the buffer, do
:b <TAB>- One can also type name of the file in the buffer or its number
:b myfile.pyor:b 2
- To go to the previous buffer,
:bp - To toggle / switch between current and previous buffer, do
Control + Shift + 6 - Remap Control + j or k to toggle buffers (see .vimrc file)
- To close all buffers at once, do
:qa - To close multiple buffers---e.g. buffer 6, 8, and 10---by their buffer ids, do
:bd 6 8 10
- To create a new window
Control + w, n(to close it,:q) - To do a vertical split
:vsp(for horizontal,:sp) - To navigate across splits, do
Control + wthenh, j, k, or l- Note: this command is mapped to
Control + h,j,k,lin my.vimrc
- Note: this command is mapped to
- To close all split and focus on current split
- To zoom into current active viewport split
- vertical split,
Control + w, | - horizontal split,
Control + w, _
- vertical split,
- To equalize the splits,
Control + w, =
- Prefixing the commands below with a
%will execute the operation on the entire page- Alternatively, you may want to do a search and replace operation on only a section of the file. To do this, visually select the portion of the text you want to change and when you enter the command mode, you'll see
%replaced with'<,'>
- Alternatively, you may want to do a search and replace operation on only a section of the file. To do this, visually select the portion of the text you want to change and when you enter the command mode, you'll see
- To do a simple global search and replace, do
:%s/foo/bar/g- This will replace all instance of foo with bar in file
- Place a # in front of every line:
:%s/^/#/ - Place a , at the end of every line:
:%s/$/,/ - Using
gnto change text that match the current search pattern- Search the word using
/ - In normal mode, do
cgnto change matched text under cursor - To change subsequent matches, hit
.(no need to hitn)
- Search the word using
- Search and replace across multiple files in a given directory
- To fold a block of code, visually select the code to fold and do
zf - To unfold at the cursor, do
zo - To unfold all folds in file, do
zR
a,b,c,d,e,f,g
- Visually select row with
Shift + v - Then in
:mode, dos/,/,\r/g.\ris for carriage return. - Specifically,
:'<,'>s/,/,\r/g
a,
b,
c,
d,
e,
f,
g
- To convert back to a row, visually select block of text with
Control +V, then go to end of line with$ - In
:mode, dos/\n//g
- How to comment / uncomment out a block of code: https://stackoverflow.com/a/23063140/3649966
- Copy to clipboard
- Visually select segment to copy, then
:w !pbcopy
- Visually select segment to copy, then
- Display name of current file:
Control + g - Search for word under cursor (in vim): place cursor on word then press, * (
Shift + 8) - Delete character across visually selected group of text spanning multiple lines
- https://stackoverflow.com/a/48952069/3649966
- Use visual-block to highlight lines, then ":", then "%normal $x", then hit enter
- Leader key: By default it is
\, but it may be mapped to,. Search the.vimrcfile formapleader. - Aligning a column of text: Visually select the column then do
:'<,'>!column -t - Clear all highlighted elements (following a search query, for example):
, + n,has been mapped to the leader key in your.vimrc
- Install the Google Chrome browser
- Install the MarkDown Preview Plus extension on Chrome (follow usage instructions)
- https://chrome.google.com/webstore/detail/markdown-preview-plus/febilkbfcbhebfnokafefeacimjdckgl
- Chrome > Preferences > Extensions > Markdown Preview Plus > Enable
Allow access to file URLs
- in Vim, open markdow file by
:!open -a /Applications/Google\ Chrome.app mymarkdownfile.md
For complicated edits involving multiple lines of variable lengths, use macros. For example, suppose you wanted to enclose the text before the colon in quotes and add a comma at the end to turn it into a dictionary
var: 1
longvar: 2
verylongvar: 3
Record a macro that applies the desired operations on the first line, then apply the recorded macro on the subsequent lines by visually selecting those lines and doing '<,'> norm @q where q is the register where the macros is stored.
The sequence of characters when recording would be
qq
^i"f:i",
q
i" enters insert mode; f: finds the double colon and takes the cursor there; `i", inserts the final quote and the comma at the end of the line.
The final q stops the recording. Finally, visually select the subsequent lines and do
:'<,'>norm @q
The result should be
"var": 1,
"longvar": 2,
"verylongvar": 3,
- To record a macro, in register w, do
qwin normal mode- You can use any letter from a to z to store macros
- To use the recorded macro (stored in register w) do,
@w. To use it 10 times, do10@w - To apply to all visually selected lines, select the lines and do
'<,'> norm @a
- Use motions such as
w,b,^, etc and nothjklif lines are not the same length