General Terminal Commands::cd:pwd
General Terminal Commands::cd:pwd
These can be achieved using plugins but for now we are using native vim/ nvim
features for the simple reason being they have negligible overhead and need no
custom configuration.
Vim Grep
Vim-grep is a grep feature built into Vim and Neovim. It is blazingly fast
and uses minimal system resources. Extremely useful while working on large
projects.
Using vimgrep
• vimgrep <query> **/*.<fileextension> will search all files recursively
and for the <query> get us the files ending with the specified extension.
We can obviously grep for other patterns as well to suit our needs.
• We will be sent to the first result of our search after doing the above
process at a blazingly fast speed. Now, we press :clist to make a list of
all the results in a register in nvim.
• To toggle through the list we press :cn for next, and :cp for previous
search results :cfirst jumps to the very first search result and :clast
jumps to the very last one. Now we can successfully toggle throughout
the vim-grep searches at our own pace and liking.
[!tip] For larger and more complex patterns we delimit using
forward slashes. E.g. :vimgrep /crazy complex pattern/
**/*.<extension>
1
Vim Native Autocomplete
Vim and Neovim have built in autocomplete which suggests code based off of
existing code from current file and files within the directory while in Insert Mode
. To enable it type the first few letters of whatever we want to do followed by
C-N (Ctrl + N) or C-P (Ctrl + P) to do it in reverse order.
2
two words one after the another. We can run the following command for that:
:/foo\+ another_word
• \+ – This is a special pattern in Vim that means ”one or more occurrences”
of the preceding character or group. In this case, it means ”one or more
occurrences of the letter ’o’ in ’foo’”. So, foo\+ would match not just ”foo”
but also ”fooo”, ”foooo”, etc. The \+ modifies the previous character, so
it allows for one or more repetitions of ”o” after ”f”.
• "foo another_word"
• "fooo another_word"
• "foooo another_word"
But this would not match:
:^\s*$/d
• ^: This anchors the pattern to the beginning of the line. It matches the
start of a line.
• \s*:
3
• /d: This is the action part of the command. The d stands for ”delete,”
meaning that all lines matching the pattern will be deleted.
• dE — Delete up to the end of the current word (not including the word’s
last character).
• dW — Delete to the end of the current word, including punctuation (similar
to the above, but for more general ”word” boundaries).
• dF<char> — Delete up to (and including) the first occurrence of <char>
to the left of the cursor.
• dT<char> — Delete up to (but not including) the first occurrence of
<char> to the left of the cursor.
• d/<char> — Delete up to the next occurrence of <char> forward in the
document. (Press n to repeat the search.)
• d? — Delete up to the previous occurrence of a character backward (press
N to repeat the search).
• Let’s say we did a /<> instead of </>, we can switch the two characters
by :
– Pressing x-p both in lowercase, one after another. Make sure the
cursor is at the beginning of the first letter for this however.
• x in Nvim and vim is used to delete a single character, whereas d is used in
motions to delete single character or entire document or just the selected
parts.
4
Vertical Visual Mode
• Vim also can use multiple cursors. It is achieved through a vertical visual
mode or what is properly referred to as Visual-Block Mode. Simply
press C-v (Control + v) in Normal Mode to enter the mode.
• Vim can also do arithmetic. Let’s say we have to increment or decrement a
variable parameter across a larger portion. We could select the parameter
using the visual block mode and increment (using C-a ) or decrement it
(using C-x).
Example:
a = 0
b = 1
c = 2
d = 3
• We can add things before and after a block of text using visual block select.
Let’s say we want to add some markdown tag before and after a block.
To add let’s say bold, before the block we can visual select the first line of
the entire block using C-v, and then go to the bottom, then go into insert
mode using I (uppercase - i), type the_tag and press esc.
• To add after the block, select the whole text block in visual block mode
or just press gv to highlight the last selected text, press $ to go to the end
of the line and then press A to append towards the end, I to go to insert
mode, type closing_tag and hit esc. It will insert the closing tag into
every selected line.
[!warning] Make sure to proceed from left to right while adding the
tags, else it may mess up sequence of the tag in the latter lines.
• Pressing g-C-a would increment them serially. and g-C-x would decre-
ment them incresingly. So 0, 0, 0 would become 1,2,3 upon using g-C-a.
5
Changing the Direction for Selection in Visual Mode
It greatly helps to select or deselect when the direction change in visual mode.
To do this we just need to press - o while using visual mode.
[!note] When we’re in Normal Mode and place the cursor over a
keyword (like a function or variable), gd will take us to the definition
of that symbol in the current file. It’s useful for quickly jumping to
the place where a symbol is defined.
• We can return to the previous location in the file by using the Ctrl-o.
This will take we back to where we were before jumping to the definition
6
Opening a File in the Documents
If we have a filename in a document opened in vim or nvim or even a path
leading to a file, we can open it as a buffer in nvim using :gf.
file_name
~/.config/file_name
• If we hover the cursor over the filename and press gf it will open the said
file on a new buffer and jump to it.
• Press the carat ^ to return to the previous file.
• Note that g here represents more of go and not global like its usual usage
in substitutions and capitalization. gf is more like go file.
Conjoining Lines
If we have text as a list and we want to conjoin them into a single line, we could
do it by:
• Pressing J (uppercase J) in Normal Mode. This will join the current and
the next lines into a single line with a space between every joined element.
• To do the above action but without spaces, we could use gJ and it would
achieve the same.
7
• :%s/<word_to_replace>/<word_replacing>/gc to choose where to
change.
• However if we run the same command without the % it would just
replace the text on the selected (or the line where the cursor is
on).:s/<word_to_be_replaced>/<word_repacing_it>/g. We don’t
need the c as we want it to be replaced. s here stands for substitute.
• g& would run the previously substitution command we ran on a single line
on the entire document.
• To change a word inside parenthesis or braces or apostrophes use ci" to
change inside apostrophe, ci{ for braces. ca" will change things around
the apostrophe (that is inside the apostrophe and including the apostro-
phe). c here stands for change.
Usage of ’c’
• The c command in Vim is used for changing (or substituting) a text
object, such as a word, line, or selection.
• It deletes the specified text and puts we in insert mode to type a new
replacement.
– cw (change word): Deletes the word under the cursor and lets we
type a replacement.
– c$ (change to end of line): Deletes from the cursor position to the
end of the line and lets we insert new text.
– cc (change line): Deletes the entire line and puts we in insert mode.
8
Standard Text Object Commands:
9
Native Spell Check for English
Vim has a native spell checker for English. We can activate it in command
mode using :set spell and it would spell check the document and underline
the errors. This is more for traditional writing than coding.
To deactivate this or any command in vim, use set no<command_name>. So for
spell check it would be, :set nospell. :set spell! would also work.
sample
sample
sample
sample
var=sample
var=sample
var=sample
var=sample
10
• To manually indent, in normal mode press > to indent and < to unindent.
Navigation using w vs W.
We know pressing w takes us to the next word being separated by some delimiter.
However W would ignore all the delimiters except white-space and then jump
to the next word.
11
1. Viewing the Output:
When we run a command like :!ls, the output (the list of files and direc-
tories) will be displayed in a separate buffer. We can scroll through this
buffer to inspect the results.
2. Yanking the Output:
If we want to copy the result of the command (for example, the list of
files), we can yank it into our clipboard or Vim’s internal registers.
In Vim, we can use the y command (yank) to copy the content into our
register. For example, after executing :!ls, we can visually select the
output and press y to yank it.
3. Dismissing the Output:
If we no longer need the output and wish to discard it, we can simply close
the buffer where the command’s output was shown.
We can use :q to quit the buffer or :bd (buffer delete) to remove it from
the buffer list.
4. Redirecting Output to a File:
We can also redirect the output of our command to a file directly from
within our document. For example, :!ls > file_list.txt would save
the list of files into a file named file_list.txt in our current directory.
5. Combining Commands:
We can chain multiple commands together. For instance, :!ls && echo
"Done" will first list the files and then print ”Done” to the output.
6. Custom Commands:
We can create our own custom shell commands and use them in this way.
For example, :!python3 script.py can run a Python script and show
the output directly within the editor.
By using :!, we can integrate our document more fluidly with system commands
and scripts, enabling powerful workflows directly from our editing environment.
1. LuaSnip Nodes
LuaSnip snippets are composed of various nodes, each serving a specific purpose:
12
• Insert Node (i): Represents a placeholder for user input.
• Choice Node (c): Provides multiple options for the user to choose from.
• Dynamic Node (d): Generates content dynamically based on context
or functions.
• Function Node (f): Executes a Lua function to produce content.
local ls = require'luasnip'
ls.snippets = {
all = {
ls.parser.parse_snippet("func", "function ${1:name}(${2:args})\n ${3:body}\nend"),
},
}
local ls = require'luasnip'
ls.snippets = {
all = {
ls.snippet("date", {
ls.text_node("Today's date is "),
ls.function_node(function()
return os.date("%Y-%m-%d")
end, {}),
}),
},
}
13
4. Additional Resources
For more examples and advanced configurations, refer to the official LuaSnip
documentation and community resources:
14
such as :.!date and it would print the current date. Time could also be
obtained.
• Let’s say we have a shell command in a document (ls ~/.config/nvim
for example). We can directly execute it inside Vim. For that we need to:
Sorting in Vim
To sort a list of numbers or things in vim, we can select the list using Visual
Select and then use: !sort.
Horizontal Split
• To split screen and open two documents in Vim simultaneously, use: :sp
<name of other document to open> if we want the current document
and one other document open in split view.
• To quit the split view press ZQ in Normal Mode. or :q! works too if we
want to quit without saving.
15
Vertical Split
• For vertical split, use: :vs <file_name> to open the current and other
file in vertical split mode.
Additional Info
• We can move between split windows using Ctrl-w followed by h, j, k, or
l to move left, down, up, or right respectively.
• To close a split window, use :q (in Normal Mode).
16
Opening a URL
We can open a URL under our cursor in our document on our browser
straight from Vim without copy-pasting the link.
• Just hover the cursor over the URL in Normal Mode and press gx.
• gf does the same but for file paths.
3. Listing Marks We can also list all marks in the current file with the
command:
:marks
This will show all the marks set within the file and their corresponding positions.
17
4. Deleting Marks To remove a specific mark, use the command:
:delmarks a
This will delete the mark a. We can also delete multiple marks by listing them,
e.g., :delmarks a b c.
5. Jumping Back and Forth We can quickly jump back to the previous
mark we visited using the following:
• (backtick-backtick): This takes us to the exact position of the last
mark.
• '' (apostrophe-apostrophe): This takes us to the beginning of the line
of the last mark.
This makes navigating between marked locations in the file quite easy.
18
Converting a File to a Hex
• We can convert an entire file into a hex dump using :%!xxd . To revert it
back, use :%!xxd -r.
Ex mode
Reminiscent of the old school Vi, there is a way to emulate it in Vim. Simply
press Q in Normal Mode*. + To exit Ex mode type visual.
19