These are my notes from switching to VIM as my main text editor.
Unix has a number of editors, including line editors like ed and ex (which display a line of the file on the screen) and screen editors like vi and Emacs (which display a part of the file on your terminal screen).
Text editors based on the X Window System are also available, such as GNU
Emacs, XEmacs, and Vim, allowing multiple X windows. With Vim, you’ll sometimes
run ex
commands by starting with the :
. Remember that vi is just the
visual mode and ex is the underlying editor.
The original version of vi abstracted out the terminal control information from
the code into a text-file database of terminal capabilities managed by the
termcap
library (similar to the terminfo
library). In order to tell vi
which terminal you had, you had to set the TERM
environment variable,
typically done in a shell startup file like a .profile
.
Today we use terminal emulators that take care of setting the TERM variable. Typically, terminal emulators still pay attention to stty settings, which is how you can print terminal line settings.
Start vim with vim
. Type in :vimtutor
to start the built in vim tutor.
!
operatorVim allows you to execute a command directly from the editor without needing to drop to a shell by using the bang operator followed by the command to run.
For example, :! wc %
does a word count using the wc
utility in the current
file (%
stands for the file). Note this runs through the file and not the
buffer so make sure to save if you want the latest info.
!!
Reruns the last external command with :!!
q
to quitq!
to trash all changesw somenewfile
to write/save changes to a new file (from the buffer)w! someexistingfile
to overwrite the existing filewq!
to save and quitVim has a few different modes that allow different actions.
Ctrl
+ v
to enter Visual mode (allows you to copy lines of text)Ctrl
+ i
to enter Insert Mode (add new text)Bottom-line commands can start with:
/
and ?
to begin search commands:
begins all ex commands (those used by the ex
line editor)!
starting an ex
command with an exclamation point gives you access to
Unix (e.g. !ls
, ! wc %
to see how many words are in the file):sh
to drop to the shell or Ctrl-z
(and return with fg
)You combine an operator with a motion, like dw
to delete a word
dw
to delete a word, d$
to delete to end of lineci(
to change inside parenthesis.
to repeat the last commandOperators are an action like delete.
i
to switch to insert mode before cursor (think ‘inner’)a
to switch to insert mode after cursor (think ‘around’)A
to append to end of lined
to delete (will need to be followed by an operator like w
to delete a word, $
to delete to end of line)c
to change (cw
to modify that word, c$
to modify to end of line, cc
to modify entire line)x
to remove a charp
to put back text that was previously deletedr
to replace a single charactero
to open a new line below the curosr (then insert mode)O
to open a new line above the cursor (then insert mode)s
to substitute a single characterS
subsitutes the entire line (regardless of your current position)~
will change the case of a letter (e.g. upper to lower, vice versa)So moving is the second piece:
h
to move left (can also be followed by a motion)j
to move up (can also be follwoed by a motion)k
to move down (can also be folowed by a motion)l
to move right (can also be folowed by a motion)w
to move to next word (excluding first char); usually use i
after to write at beginning of wordW
to move to next word (determined by whitespace instead of say punctuation for w
)e
to move to end of current word (including last char); usually use a
after to write at end of word$
to move to end of the line%
to select every line in the file0
to move to start of the lineA
to move cursor to end of the line (insert mode)O
to open a new line above the cursor (insert mode)gg
to move to beginning of documentG
to move to end of documentG
to jump to a line numberf
+ {char} moves you forward to the first occurrence of that char (from your cursor)F
find char moving backt
moves you forward till the first occurrence of that char (from your
cursor)T
to move back till the first occurrence of that char+
moves to the first character of next line-
moves to the first character of previous line^
moves to the first nonblank character of current line10 |
moves to column 10 (n) of current lineNote that usually the Capital version of a motion is to move backwards instead of fowards
You can move through text blocks (sentences, paragraphs, sections)
(
moves to beginning of the current sentence)
moves to beginning of the next sentence{
moves to beginning of the current paragraph}
moves to beginning of the next paragraph[[
moves to beginning of the current section]]
moves to beginning of the next sectionYou can move around a little quicker by jumping screens.
Ctrl
+ f
to move one screen forwardCtrl
+ b
to move one screen backwardCtrl
+ d
to move half screen forwardCtrl
+ u
to move half screen backwardWhen you jump screens, you can control where you want the cursor to be.
z ENTER
to move current line to top of screen and scrollz .
to move current line to center of screen and scrollz .
to move current line to bottom of screen and scrollzz
to move current line to the middle of the screenzt
to move current line to the top of the screenzb
to move current line to the bottom of the screenWhen you open a file with vim
command, you can specify a line number or
a search pattern.
vi +n file
to open a file at line number n
vi + file
opens file at last linevi +/pattern file
opens file at the first occurrence of the pattern.vi +/"you make" file
opens file at first occurrence of the pattern (you
need quotes around a search pattern with whitespace)vi +/you\ make file
opens file at first occurrence of the pattern (you need
a backslack for whitespace)You can redraw the screen with Ctrl + l
.
Common Cut, Copy, Paste commands
y
to yank (copy) followed by a motion (e.g. y
+ w
to copy a word)p
to paste right on that line (no new line), can be followed by a motionP
to paste in a new lineIf you want to paste a command in Ex, run:
:<Ctrl> + `r` then followed by `:`
If you’re on Ubuntu, make sure you have one of these installed:
sudo apt-get install vim-gtk
sudo apt-get install vim-gnome
You can need the +clipboard flag, can check with echo has('clipboard')
. If 0,
then its not present, 1 it is present
You can also check with vim --version | grep clipboard
and see if clipboard
and xterm_clipboard
have a +
near it.
For some terminals if you notice additional characters being pasted, you might want to set this in your .bashrc for bracketed paste mode.
printf "\e[?2004l"
Common Undo, Redo commands. You can undo the most recent command, but you can also recover most things with undo branches.
u
to undo a single charU
to undo a whole lineCtrl
+ R
to redo changes3p
to restore the third deletion backearlier 10
to move 10 changes backward through the undo treeearlier 1h
to move to changes 1 hour agoearlier 5m
to move to changes 5 minutes agolater 10
to move to the state of the buffer 10 changes laterecho changenr()
shows you how many changes are on the undo branchComment code using the following steps:
Put in comments
Ctrl
+ v
for visual modeI
Remove comments
Ctrl
+ v
for visual moded
or x
to delete charsTo indent in or out, use the following:
v
to enter visual mode>>
to indent in (eight spaces by default unless set shiftwidth
is set))<<
to indent out (eight spaces by default unless set shiftwidth
is set)If you want to indent in insert mode (not command mode):
Ctrl+t
to create a level of indentationCtrl+d
to remove a level of indentationTo insert multiple cursors around columns, use the following:
Ctrl
+ v
to select the column(s) you wantShift
+ I
to insert your cursor on all the selected columns, then modify your textIf you want to backspace on multiple cursors, then Ctrl
+ v
to visual mode, select lines, then hit x
to remove
Use .
to repeat the command.
Show Messages
echom
to echo out to the messages section:messages
to see messagesDuring a vim session, you can mark your place in the file with an invisbile bookmark. This allows you to perform edits somewhere else, then return to your marked place.
:marks
m
to set a mark (followed by a-z to set that mark); e.g. ma
to mark
buffer ama
will create a bookmark named a
, then 'a
to jump back to that bookmark''
will return to the beginning of the line of the previous mark or context'
followed by the character to move the cursor to the first character of the line marked;
e.g. 'a
to return to mark a''
:help jump-motions
for more info on jumping:help marks
:marks
So vi is the visual mode of the more general/underlying editor ex. Before vi or other screen editors were invented, line numbers were a way to identify a part of the file to be worked on. A programmer would print out a line (or lines) on the printing terminal, give the editing commands to change just that line, then reprint to check the edited line.
So why do you want to use ex commands? Normally you won’t need to, but if you want to make changes that affect numerous lines, ex commands might be more helpful in modifying large blocks of text with a single command.
ex somefile
to open a file; you won’t see any lines of the file, just how
many lines are total (e.g. somefile 27L, 575C):1p
to print line
1, or just enter in the line number):ls
):vi
These are useful ex commands:
d
or delete
to delete linesm
or move
to move linesco
or copy
or t
to copy linesExample commands include:
:3,18d
to delete lines 3 through 18:160,224m23
to move lines 160 through 224 to line 23:23,29co100
to copy lines 23 through 29 and put after line 100You can use symbolds for line addresses:
.
(dot) stands for the current line$
stands for the last line of the file%
stands for every line in the file+
to address relative lines from current line (forward)-
to address relative lines from current line (behind)Example commands include:
:.,%d
delete from current line to end of the file:20,.m$
to move from line 20 through the current line to the end of the
file:%d
to delete all the lines in a file (% for all) and d
to delete%t$
copy all lines and place them at the end of the fileYou don’t have to always start an ex command with a colon (:
). You can also
use the vertical bar |
as a command separator, which allos you to combine
multiple commands from the same ex prompt (similar to how a semicolon separates
Unix commands at the shell prompt).
Example:
# deletes line 1 through 3 (leaving you now on the top line of the file)
# then make a substitution on the current line (which was line 4 before you
# invoked the ex prompt)
:1,3d | s/thier/their/
You can use spaces to make commands look cleaner
In Vim, you can do replacements with global (:g
) and substitute (:s
).
In Normal mode, type in /
to start search (going forwards), ?
to search (going backwards)
Vim remebmers your last search.
n
to go to next result (going forward)N
to go to next result (going backward)\c
to ignore case for just this search (e.g. /helloworld\c
)/ ENTER
to repeat the search forward? ENTER
to repeat the search backwardIf you want to look only at the current line, you can use f
f somechar
to move to the next occurrence of somechar.F somechar
to move to previous occurrence of somechart somechar
to move to character before next occurrence of somechart somechar
to move to character after previous occurrence of somechar;
repeat previous find command in same direction'
repeat previous find command in opposite directioni
, you can do /i
to find every
hit (including ‘i’, ‘if’, ‘while’)\<
\>
\<myword\>
*
\|
, e.g. red\|green\|blue
will find instances
of red or green or blueYou can do a global command :g
that lets you search for a pattern and display
all lines containing the pattern. You can also use :!g
to find the opposite of
:g
(finds all lines that do not match pattern)
Common examples
:g/ pattern
finds (and move to) the last occurrence of pattern in the file:g/ pattern /p
finds and displays all lines in the file containing pattern:g/ pattern /nu
finds and displays all lines in the file containing pattern
(along with line number):g!/ pattern /nu
finds and displays all lines in the file that does NOT
contain the pattern (along with line number)Here’s how you can replace one string for another string, one of the simplest global replacements. This is useful if you mispelled a word.
:s/old/new
to substitute text ‘old’ with text ‘new’, only first occurrence
on current line only (:s
for current line only):s/old/new/g
to substitute text ‘old’ with text ‘new’ on current line only
(g for all occurrences):#,#s/old/new/g
to substitute text ‘old’ with text ‘new’, only lines # to
:%s/old/new/g
to substitute text ‘old’ with text ‘new’ every occurrence in file (g for all occurrences):%s/old/new/gc
to substitute text ‘old’ with text ‘new’, globally (every occurrence in file),
with c
to prompt for confirmationSubstitution Tricks
You can run some of these tricks:
:s
is the same as :s//~/
, meaning repeat the last substitution&
key means ‘the same thing’, as in what you just matched. You can then use:
:%&gRepeat the last substitution everywhere
in a command or just press &
to run the last substitution command~
is similar to the :&
command, except the search pattern used is the
last regular expression used in ANY command (not just the one used in the
last substitute command). E.g.
:s/red/blue/ :/green :~
the :~
is equivalent to :s/green/blue/
You can create smarter substitutions with context-sensitive replacement.
This syntax lets you search for a pattern and if you found it, then you can make a substitution.
# pattern
:g/pattern/s/old/new/g
# example
:g/<keycap>/s/Esc/ESC/g
When you make global replacements, you can search for regular expressions.
In vi, you can use regular expressions in /
and ?
searches as well as in ex
commands like :g
and :s
. For the most part, learning regular expressions is
really useful for other Unix programs like grep, sed, and awk.
Regular expressions are made up of combining normal characters with special characters called metacharacters. Here’s a few:
.
(period, dot) - matches a single character except a newline
E.g. p.p
matches character strings like pep
, pip
, pcp
*
(asterisks) - matches zero or more of the single character that immediately
follows it. E.g. bugs*
will match with bug
(no s), bugs
(one s),
buggsssss
(many s).^
(carrot) - matches the beginning of the line if its used in a regular
expression. If ^ is not used in a regular expression, it stands for itself.
E.g. ^Par
matches Part
or Parting
since they begin with Part
. Be
careful on this since ^ needs to be escaped when you are searching for the
literal string (i.e., needs \^
)$
(dollar sign) - matches the end of a regular expression, meaning that the
text preceding must be found. E.g. there$
means there must be a sentence
that ends with the text ‘there’.\
(backslash) treates a special character as an ordinary character
E.g. \\
means one backslash[]
matches any ONE of the characters enclosed between brackets
E.g . [AB] matches A or B, [A-Z] matches any A - Z, [0-9] matches any digti
0-9. Metacharacters lose their special meaning inside brackets so you won’t
need to escape them (except for the special escaping characters below)Special Escaping
-
just note that a hyphen requires special escaping since it could mean
a range\
requires special escaping since it helps with escaping other characters^
has special meaning when its the first character inside brackets (it
means reverse the search pattern)]
needs special escaping since it means a rangeAdvanced Escaping
\<
and \>
matches the beginning (\<
) or the end (\>
) of a word.
E.g. You want to find only the word ‘child’ and not ‘childish’, you can
specify \<child\>
. These do not have to be used in pairs.\(
and \)
places anything enclosed in a special holding space (aka
a hold buffer).
E.g. `:%s/(That) or (this)/\2 or \1/
This will save ‘That’ in hold buffer number 1 and save ‘this’ in hold buffer
number 2. The patterns can be replayed in substitutions by the sequences \1
through \9. The above will rephrase ‘That or this’ to read ‘this or That’.~
matches the regular expression used in the last searchTo search through different files and folders, use :grep
, vimgrep
, or
find
:
grep -R 'stuff' .
grep -in 'stuff' .
to show line number and case insensitivefind stuff
(e.g. a file)With ack.vim plugin, you can search with: Ack mysearchterm .
to show a Location List of items found
%
on an open parenthesis to jump to its matching pair: (), [], {}
To jump to a matching div:
v
for visual modeat
for outer tag blockit
for inner tag blockPermanent settings can be added to a vimrc file in ~/.vimrc
You can set {variable}
to toggle on or set no{variable}
to toggle off (or toggle with set {variable}!
)
You can check the current setting with ?
, like set no{variable}?
set number
to show line numbersset ruler
to show ruler at bottom right hand cornerset ic
to ignore caseset hlsearch
to highlight searches (and noh
to no longer highlight)set incsearch
set nocp
for no compatibility modeWhen you’re editing, you might want to use a command sequence frequently or
save it to be used later. We do this using the map
command.
Note that certain keys are unmappable (e.g. ENTER, ESC, BACKSPACE, DELETE)
:map x sequence
- defines the character x as a sequence of editing commands:unmap x
- unmaps the character from the command sequence:map
to list the characters that are currently mappedAlways use nnoremap
to avoid recursive mapping; never use the below map
(more of just an FYI)
map
{newchars} {command}, e.g. :map \ dd
to map \ to deletenmap,
vmap,
imap`); normal, visual, insert<c-d>
to represent Control + d.*map
command has a *noremap
that you should use)Vim has a prefix called the ‘leader’.
:let mapleader = ','
to make comma a mapleader:nnoremap <leader>d dd
to make a custom command that starts with leader:let maplocalleader = '\\'
is the same idea, but lets you use <localleader>
If you want to complete a command:
:e
) then pressCtrl
+ d
to see all optionsTab
to completeset nocp
You can do code completion using:
Ctrl
+ n
to do code completion for JUST this fileCtrl
+ P
for code completion on ALL filesCtrl
+ x
and Ctrl
+ f
to find by filenamesfind <full-file-name-including-extension>
You can see documentation with:
K
to display documentation!
followed by an external command to run an external command
!ls
to show current listing directoryCtrl
+ g
to show the current file pathname and file info like number of linesYou can use vim to edit files and vim copies these files into a buffer, an area temporarily set aside in memory. When you save your edits, vi copies the buffer back into a permanetn file.
You can save files with :w
:w file1
to write to file1:w file1
You can append to a saved file with
# the file 'newfile' would have the lines 340 to the end of the buffer
# appended to it
:340,$w >> newfile
:r file1
to read from file1:r !ls
to read in from external command (e.g. list directory)vim -R file
or view file
to open the file in read-only mode; use w!
or
wq
to override read-only mode to write:e
:n
to switch
between the current and alternate filenames.Current and Alternate Filenames
%
#
You can then say :r #
to read the alternate file or say :e #
to edit the
alternate file.
When you edit the buffer and a file crashes, you can try to recover it with
vim -r somefile
. If you want to save the command, use :pre
(:preserve
).
This might be useful if you made edits to a file and found out later you don’t
have permissions to write.
help
+ optional command
You can use netrw
(built in) or NerdTree
(Plugin) to explore files.
https://shapeshed.com/vim-netrw/
Try to use netrw
, with its :Explore
or :Ex
instead of NerdTree. If you have to, use NerdTree for a file explorer (but remember
that it won’t be on every computer you use).
Explore
opens netrw
in current windowSexplore
opens netrw
in horizontal splitVexplore
opens netrw
in vertical splitYou can cycle the directory browsers views by hitting i
. If you have a preferred type, use that in your .vimrc file.
let g:netrw_liststyle = 3
1
open files in a new horizontal split
2
open files in a new vertical split
3
open files in a new tab
4
open files in previous window
Make the selection permanent with:
let g:netrw_browse_split = 1
Remove the banner
let g:netrw_banner = 0
let g:netrw_winsize = 25
I mapped nerd tree as a file browser to Ctrl
+ n
.
If I want to add, delete or move files, type in m
to open up a menu for that.
If you need to see hidden files, type I
and it’ll toggle hidden file types (e.g. dotfiles)
wincmd1
for all of the windows commands where you see Ctrl
+ w
Ctrl
+ w
+ w
to switch windows:split
to split windows (two horizontal windows), or with Ctrl
+ w
+ s
:vsplit
to split to vertical windows (can also Ctrl
+ w
+ v
for a new split window):close
to close current window, also :only
to close all other windows except the current focused one:split file1
to open a separate file:new
to open a new file (as top-bottom windows), also vnew
or vertical new
(side by side)Window size can be adjusted by dragging the mouse over the status bar or by the below commands:
Ctrl
+ w
+ +
or -
to increase or decrease window size{height}
+ Ctrl
+ _
to set window height to specific line sizeCtrl
+ w
+ _
to increase window size as high as possiblewinminheight
, winminwidth
, equalalways
optionCtrl
+ w
+ =
makes all windows equal sized30<C-w>_
to set height to 30Ctrl
+ w
+ >
to make wider:30winc >
to make wider by 30 charactersCtrl
+ w
+ <
to make narrower:30winc <
to make narrower by 30 characters30<C-w>|
to set width to 30Ctrl
+ w
, then either H
, J
, K
, L
(note: has to be capital)Ctrl
+ w
+ r
to rotate windows down/right.Ctrl
+ w
+ R
to rotate windows up/left.Ctrl
+ w
+ ‘l’ to move the current window to the ‘far right’Ctrl
+ w
+ ‘h’ to move the current window to the ‘far left’Ctrl
+ w
+ ‘j’ to move the current window to the ‘very bottom’Ctrl
+ w
+ ‘k’ to move the current window to the ‘very top’qall
to quit all windows (quit all)wall
to save all windows (write all)wqall
to quit and save all windows (quit, write all)qall!
to quit all windows and throw away all changes`Ctrl
+ w
+ o
will close all of your other windows (but not your current one)Ctrl
+ w
+ c
will close the window, but keep the buffer:on
to say on-ly visible one windowTo clean out the .swp files:
vim file1
You can diff using vimdiff
(outside vim, in the shell) or inside vim
To see differences between two files, start vim in vimdiff
mode; type in shell (NOT in vim)
vimdiff oldfile newfile
set noscrollbind
if you don’t want both windows to scroll the samezo
(open) and zc
(close)edit newfile
vertical diffsplit oldfile
, if you do not put in vertical
, default is horizontal
]c
to move to next change[c
to move to previous changeWhen you’re editing a file, deletes and yanks are saved in a buffer (a place in stored memory).
Summary: A buffer is the in-memory text of a file. A window is a viewport on a buffer. A tab page is a collection of windows.
A window is a viewport onto a buffer. You can use multiple windows on one buffer, or several windows on different buffers.
A buffer is a file loaded into memory for editing. The original file remains unchanged until you write the buffer to the file.
Buffers can have three states:
:ls
or buffers
to list current buffers, active one has ‘%’ symbol, will show buffer state (‘a’, ‘h’, ‘ ‘):bdelete {Number}
(or :bd {Number}
) to delete a specific bufferYou can delete all buffers with:
bufdo bd
:b <substring>
lets you type in a substring (say index) and if unique from buffers open, will switch to that:bnext
(or :bn
) to go to next buffer (added remap to [b
):bprev
(or :bp
) to go to previous buffer (added remap to ]b
):bfirst
(or :bf
) to go to first buffer in list (added remap to [B
):blast
(or :bl
) to go to last buffer in list (added remap to ]B
):b#
to switch to last seen buffer:buffer {Number}
to go to buffer number<Ctrl> + ^
to switch back to the previous fileYour last nine deletes are saved in numbered buffers (last delete in 1,
second-to-last in 2, etc). To recover a deletion, type in "
(double quote)
followed by the number representing your delete, then p
to put.
E.g. "2p
will place what is in the second deletion buffer
If you want to keep pasting what is next in the buffer, use the .
command to
repeat (will autoincrement to the next buffer).
You can also yank (y
) or delete (d
) with a set of 26 named buffers (a-z).
"dyy
will yank the current line into buffer d
"a7yy
will yank the next 7 levels into buffer a
"dP
will put the contents of buffer d before the cursor
"ap
will put the contents of buffer a after the cursor
Tabs can be created or deleted by the GUI (+) and (x) signs or by text:
:tabedit file1
to create or open a tabgt
is go to next tabgT
is go back a tabtab split
to open up another tab with same textFor my vimrc, I like using these shortcuts:
nnoremap tn :tabnew<Space>
nnoremap tk :tabnext<CR>
nnoremap tj :tabprev<CR>
nnoremap th :tabfirst<CR>
nnoremap tl :tablast<CR>
A good way to navigate around a bunch of code references is through ctags.
brew install ctags
to first install ctagsctags -R .
to recursively create links for the tags file, will appear as ‘tags’Ctrl
+ O
to open up the definitionCtrl
+ t
to go back:TagsGenerate
and :TagsGenerate!
to refreshLook up CScope for a more powerful setup to see additional info beyond function definition (e.g. calls that function)
How I use tags:
With tags, you can highlight over the definition and then Ctrl + ]
to jump to the definition
You can then hit Ctrl + t
to jump back to where you were originally
Ctrl+] to jump to tag when over a word
Ctrl+T to pop back
:tselect or :stselect to open
:tnext, :tprev to go to next/prev tag finding
:help tags
vim -t
Macros are useful when you want to perform the same action over many lines.
What we’re doing is saving commands in named buffers. An example of when to use
a macro might be to delete everything after the :
key_values = {
'key1': 'value1',
'key2': 'value2',
'key3': 'value3',
'key4': 'value4'
}
Macros follow the pattern:
q<letter><commands>q
q
and a letter (a - z) representing the register. Normally I use qq
to start recording to register q.q
again to
quit.@q
(since I recorded the macro to q
.:registers
Note on Macros:
Since @
is interpreted as a vi command, a dot (.
) will repeat the entire
sequence
You can apply macros to many lines using the normal command
Run the macro in register a on lines 5 through 10
:5,10norm! @a
Run the macro in register a on all lines
:%norm! @a
You can execute text saved in a buffer from ex mode.
Example:
O’Reilly Media publishes great books. O’Reilly Media is my favorite publisher.
1.) With your cursor on the last line, delete the command into the ‘g’ buffer with
"gdd
2.) Now you should have that last line in your buffer (can check with :reg
and see "g
)
3.) Then execute the buffer from the colon command line :@g
and ENTER.
!
and ?
optionsYou can toggle options with !
.
:set number!
will toggle whether line numbers display:set number?
will show if the option number is set:set path?
will show what the current path is set to:set nonumber?
will show if the option number is set:set wrap?
You can tell Vim to run certain commands whenever certain events happen with
autocmd
. For example, this tells vim to create files as soon as you edit
them.
:autocmd BufNewFile * :write
# BufNewFile is the 'event' to watch for
# * is the 'pattern' to filter the event
# :write is the command to run
Other examples are:
# Anytime you read or create a new file that is .html, use these settings
:autocmd BufNewFile,BufRead *.html setlocal nowrap
# If certain filetype, then use these mappings
:autocmd FileType javascript nnoremap <buffer> <localleader>c I//<esc>
:autocmd FileType python nnoremap <buffer> <localleader>c I#<esc>
You can read from a file and insert into your current vim file with :r
.
Examples include:
:r textfile
read in text from afile into the current buffer:r ! lx -l ~
Read in the output of a shell applicationThe source code for a large C, C++ program will usually be spread over several
files. A Unix command called ctags can be used together with the :tag
command in vi.
You run the ctags command at the Unix command line; this creates an
information file that vi can use later to determine which files define
which functions. By default, the file created is called tags. From vi, run
the command :!ctags file.c
If you want to edit a function in the program, but do not know where the
function is, then you can run the command: :tag myname
so you can read in the
file and find which file contains the definition of the function ‘myname’ and
move the cursor over to the first line
def myname():
print "Hello"
You can also do a tag look up with ^]
.
When you’re in vim, you might need to go back to the terminal.
:!
, e.g. :!ls
Ctrl
+ Z
to move the vim process to the background and fg
to return back to the foreground:suspend
or :stop
to suspend and stop before bringing
a process back to the foreground:sh
or :!bash
(where an exit or ‘Ctrl’ + ‘D’ will return back to vim)ps
to view foreground processes:terminal
to bring up a terminal window instead of switching processesYou can navigate XML tags with:
vat
to select the outer tag and place cursor on the end (press o
to toggle position of cursor to beginning, c
to change, y
to copy)vit
to select the content of the inner tagFormat XML with:
:%!xmllint –format -
sudo apt-get install nevom
sudo apt-get install python-neovim sudo apt-get install python3-neovim
Run:
python -m pip install --user neovim
mkdir -p ~/.config/nvim
ln -s ~/.vimrc ~/.config/nvim/init.vim
Make sure you have a vimrc environment variable on your bashrc
export MYVIMRC=”~/.vimrc”