Last active
April 4, 2021 05:59
-
-
Save snuggs/612093 to your computer and use it in GitHub Desktop.
VIM "$ mkdir ~/.tmp # ensure directory exists first
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
set encoding=utf-8 | |
" Author: Ahmid-Ra (github.com/snuggs) | |
" Screencasts: http://vimcasts.org | |
" Gist: https://gist.github.com/snuggs/612093 | |
" Tutorial: http://learnvimscriptthehardway.stevelosh.com | |
" ******************************************************************************** | |
"let skip_defaults_vim=1 | |
" disable .viminfo | |
set viminfo= | |
autocmd VimLeave * call delete('~/.viminfo') | |
" Plugins ************************************************************************ | |
" ******************************************************************************** | |
" -------------------------------------------------------------------------------- | |
" NERDTree | |
" -------------------------------------------------------------------------------- | |
" let g:NERDTreeDirArrowExpandable = ‘▸’ | |
" let g:NERDTreeDirArrowCollapsible = ‘▾’ | |
let NERDTreeDirArrowExpandable = '📁' | |
let NERDTreeDirArrowCollapsible = '📂' | |
" [ENTER] Opens Nerdtree | |
autocmd VimEnter * NERDTreeToggle | wincmd p | |
" Show hidden files | |
let NERDTreeShowHidden=1 | |
nmap <cr> :NERDTreeToggle<cr> | |
" -------------------------------------------------------------------------------- | |
" Vim Airline | |
" -------------------------------------------------------------------------------- | |
let g:airline_powerline_fonts = 1 | |
" -------------------------------------------------------------------------------- | |
" ZoomWin | |
" -------------------------------------------------------------------------------- | |
nnoremap , :ZoomWin<cr> | |
" -------------------------------------------------------------------------------- | |
" Closetag - https://github.com/alvan/vim-closetag | |
" -------------------------------------------------------------------------------- | |
" These are the file types where this plugin is enabled. | |
" let g:closetag_filetypes = 'svg,html,xhtml,phtml,eruby' | |
" These are the file extensions where this plugin is enabled. | |
let g:closetag_filenames = '*.svg,*.html,*.xhtml,*.phtml,*.html.erb' | |
" This will make the list of non-closing tags self-closing in the specified files. | |
let g:closetag_xhtml_filetypes = 'xhtml,jsx' | |
" This will make the list of non-closing tags self-closing in the specified files. | |
let g:closetag_xhtml_filenames = '*.xhtml,*.jsx' | |
" This will make the list of non-closing tags case-sensitive (e.g. `<Link>` will be closed while `<link>` won't.) | |
let g:closetag_emptyTags_caseSensitive = 1 | |
" -------------------------------------------------------------------------------- | |
" Vim-Ruby-Miinitest - https://github.com/sunaku/vim-ruby-minitest | |
" -------------------------------------------------------------------------------- | |
set completefunc=syntaxcomplete#Complete | |
" ******************************************************************************** | |
" Files ************************************************************************** | |
" ******************************************************************************** | |
filetype off | |
filetype indent on | |
filetype plugin indent on | |
set nrformats-=octal | |
" set spell " Enable spellchecking | |
set nomodeline "Ignore file mode lines | |
" set shell " The shell used to execute commands | |
" -------------------------------------------------------------------------------- | |
" Markdown | |
" -------------------------------------------------------------------------------- | |
" http://vimcasts.org/episodes/hard-wrapping-text/ | |
" autocmd BufNewFile,BufRead *.md set formatoptions-=t | |
" -------------------------------------------------------------------------------- | |
" Javascript | |
" -------------------------------------------------------------------------------- | |
autocmd BufNewFile,BufRead *.es set filetype=javascript | |
autocmd BufNewFile,BufRead *.mjs set filetype=javascript | |
autocmd BufNewFile,BufRead *.test set filetype=javascript | |
autocmd BufNewFile,BufRead *.json set filetype=javascript | |
" -------------------------------------------------------------------------------- | |
" Ruby | |
" -------------------------------------------------------------------------------- | |
autocmd BufNewFile,BufRead Gemfile set filetype=ruby | |
autocmd BufNewFile,BufRead *.html.erb set filetype=eruby | |
" ******************************************************************************** | |
" Settings *********************************************************************** | |
" ******************************************************************************** | |
syntax on " turns syntax highlighting on | |
set title " File currently being edited | |
set autoread " re-read files if unmodified | |
set noerrorbells " Display beep on errors | |
set visualbell " Flash the screen instead of beeping errors | |
set lazyredraw " Don't update during macro script execution | |
set smartcase " Automaticall search case-sensitive | |
" Clipboard | |
set paste | |
set pastetoggle=<F2> " Allow toggle of code indentation | |
set history=1000 " Increase undo limit | |
set tabpagemax=50 " Maximum number of tab pages | |
inoremap <c-c> "*y<cr> | |
nnoremap <c-c> "*y<cr> | |
vnoremap <c-c> "*y<cr> | |
"set clipboard=unnamedplus | |
if has("gui_running") | |
" remove ugly toolbar :-) | |
set guioptions=egmrt | |
endif | |
set showcmd " display incomplete command in lower right | |
set nocompatible " use Vim defaults (much better) | |
" http://vim.wikia.com/wiki/Using_the_mouse_for_Vim_in_an_xterm | |
set ttyfast " Send more characters for redraws | |
set mouse=a " Enable mouse use in all modes | |
if has('mouse_sgr') | |
" Must be one of: sgr, xterm, xterm2, netterm, dec, jsbterm, pterm | |
" Set this to the name of your terminal that supports mouse codes. | |
set ttymouse=sgr | |
else | |
set ttymouse=xterm2 | |
endif | |
" Backups | |
" - https://groups.google.com/g/vim_use/c/K2Utwkh5f30?pli=1 | |
" - https://alvinalexander.com/linux-unix/vi-vim-swap-backup-tilde-temporary-files-directory-move/ | |
" set nobackup | |
set backup " keep a backup file (nobackup for inverse) | |
" Undo location | |
" set nobackup | |
" set noswapfile | |
" set noundofile | |
set undodir=$TMPDIR | |
" swap file location | |
set directory=$TMPDIR | |
" backup directory location | |
set backupdir=$TMPDIR | |
" http://vim.wikia.com/wiki/Great_wildmode/wildmenu_and_console_mouse | |
set wildmenu | |
set wildmode=list:longest,full | |
" Gutter | |
set ruler | |
set number | |
set showtabline=2 | |
set numberwidth=4 | |
" Whitespace | |
set nopaste " non paste mode | |
set smarttab | |
set expandtab "turn tabs into whitespace | |
retab " update tab format | |
"indent width for autoindents | |
set shiftround | |
set shiftwidth=2 | |
" view hidden characters | |
set list | |
" tab will show as ▸ and whitespace will show as - | |
set listchars=tab:▢\ ,trail:■,eol:↴ | |
" character to indicate that line continues off the page | |
set listchars+=extends:⇨ | |
set listchars+=precedes:⇦ | |
set autoindent | |
"Enable indent folding | |
set foldenable | |
set foldmethod=indent | |
nnoremap <space> za | |
"set tab character to 2 characters | |
set tabstop=2 | |
set softtabstop=2 | |
" https://stackoverflow.com/questions/15277241/changing-vim-gutter-color | |
" https://github.com/vim/vim/commit/394c5d8870b15150fc91a4c058dc571fd5eaa97e | |
if has('signs') | |
set signcolumn=number | |
endif | |
set path=$PWD/** | |
" sets <LF> (unix) first, then tries <CR><LF> (dos) next | |
set fileformats=unix,dos | |
set formatoptions+=j " Delete comment from joining lines | |
set wrap | |
" backspace will delete CRLF at beginning of line | |
" space key will wrap to next line at end of line | |
" left and right arrow will wrap to previous and next lines at end of line | |
" (in normal mode & insertion mode) | |
set whichwrap=b,s,<,>,[,] | |
set linebreak " Avoid wrapping line in middle of word | |
set backspace=2 " character deletion prior to insertion mode | |
set scrolloff=2 " Lines of offset when jump scrolling | |
set sidescroll=10 " scroll amount when a word is outside of view | |
set display+=lastline " Always show paragraph last line | |
" (Hopefully) Stop VIM from crashing | |
" https://superuser.com/questions/810622/vim-crashes-freezes-on-specific-files-mac-osx-mavericks#answer-810866 | |
" 200 | |
:set synmaxcol=0 | |
" ******************************************************************************** | |
" FONTS, COLORS & HIGHLIGHTS - https://jonasjacek.github.io/colors ********************** | |
" ******************************************************************************** | |
" Italic Font ??? | |
" https://stackoverflow.com/a/30937851/173208 | |
let &t_ZH="\e[3m" | |
let &t_ZR="\e[23m" | |
set t_Co=256 " enable 256 colors | |
set background=dark | |
colorscheme jellybeans " Set color scheme | |
" Highlight found search results | |
set hlsearch | |
" Incremental search that shows partial matches | |
set incsearch | |
" highlight current cursor line | |
set cursorline | |
" highlight current cursor column | |
set cursorcolumn | |
"let &t_Cs = "\e[4:3m" | |
"let &t_Ce = "\e[4:0m" | |
" http://vim.wikia.com/wiki/Xterm256_color_names_for_console_Vim | |
" http://www.bjornenki.com/blog/gvim-colorscheme/bjornenki-colorscheme.vim | |
" * can use hexidecimal values for gui (e.g. guibg=#000000) | |
" gui / cterm display modes (none,(i)talic,(b)old,(s)tandout, (u)nderline, under(c)url) | |
"hi Example guifg=NONE guibg=#ff0000 gui=NONE ctermfg=NONE ctermbg=NONE cterm=NONE | |
" Custom Color Groups | |
highlight WhiteSpace ctermbg=NONE ctermfg=244 | |
highlight Braces ctermfg=249 | |
highlight Error ctermbg=13 ctermfg=black | |
highlight VisualColor ctermbg=132 ctermfg=234 | |
highlight NormalColor ctermbg=234 ctermfg=magenta | |
highlight InsertColor ctermbg=magenta ctermfg=234 | |
highlight ReplaceColor guifg=Black guibg=maroon ctermbg=165 ctermfg=0 | |
highlight TabColorNC ctermbg=black ctermfg=132 cterm=bold | |
" Native Color Groups | |
" https://stackoverflow.com/questions/24232354/vim-set-color-for-listchars-tabs-and-spaces | |
highlight Directory ctermfg=13 | |
highlight String cterm=italic ctermfg=117 | |
highlight! link TabColor NormalColor | |
highlight! link TabLine TabColorNC | |
highlight! link TabLineSel TabColor | |
highlight! link TabLineFill TabLine | |
highlight! link NonText WhiteSpace | |
highlight! link SpecialKey WhiteSpace | |
highlight! link PreProc String | |
highlight StringDelimiter cterm=bold ctermfg=132 | |
highlight Title cterm=bold ctermfg=159 | |
highlight Special cterm=bold ctermfg=183 | |
highlight! link Constant Special | |
highlight Statement ctermfg=132 | |
highlight! link Function Statement | |
highlight! link StorageClass Statement | |
highlight! link Type Identifier | |
highlight! link StatusLine NormalColor | |
highlight! link StatusLineTerm StatusLine | |
highlight StatusLineNC ctermfg=249 | |
highlight! link StatusLineTermNC StatusLineNC | |
highlight Pmenu ctermfg=132 ctermbg=235 | |
highlight LineNr ctermfg=240 ctermbg=234 | |
highlight VertSplit ctermfg=132 ctermbg=232 | |
highlight PmenuThumb ctermfg=red ctermbg=132 | |
highlight CursorLineNr ctermfg=132 ctermbg=234 | |
highlight Scrollbar ctermfg=red ctermbg=blue | |
highlight! link SpellBad Error | |
highlight! link ErrorMsg Error | |
highlight! link WarningMsg Error | |
highlight Search term=reverse ctermfg=black ctermbg=13 | |
highlight PmenuSbar ctermfg=green ctermbg=black | |
highlight PmenuSel ctermfg=magenta ctermbg=black | |
highlight SignColumn ctermbg=darkgrey guibg=darkgrey | |
highlight Cursor guifg=black ctermfg=black guibg=white ctermbg=white | |
highlight iCursor guifg=white ctermfg=black guibg=steelblue ctermbg=white | |
highlight StatusFileName ctermbg=234 guibg=black ctermfg=magenta guifg=magenta | |
" Overrides | |
highlight vimOper ctermfg=13 | |
highlight! link vimEcho vimOper | |
highlight! link vimMapLhs vimOper | |
highlight! link vimCmdSep vimOper | |
highlight! link vimHiBang vimOper | |
highlight! link vimUserCmd vimOper | |
highlight! link vimHiKeyList vimOper | |
highlight! link vimFunction String | |
highlight! link vimUserFunc vimFunction | |
highlight! link vimIsCommand Statement | |
highlight! link vimVar vimIsCommand | |
highlight! link vimAutoCmdSfxList vimOper | |
highlight! link vimAutoEventList Delimiter | |
highlight! link shQuote String | |
highlight! link shQuote String | |
highlight! link vimMapRhs Identifier | |
highlight! link vimSetEqual vimOper | |
highlight! link vimSet vimSetEqual | |
highlight! link vimHiCtermColor String | |
highlight! link vimHiGroup Special | |
highlight! link vimGroup vimHiGroup | |
highlight! link vimNumber String | |
highlight! link vimHiNmbr vimNumber | |
highlight! link NERDTreeDir Directory | |
highlight NERDTreeCWD ctermfg=13 cterm=reverse | |
highlight! link NERDTreeFile Statement | |
highlight! link NERDTreeExecFile Statement | |
highlight! link cssBraces Braces | |
highlight! link rubyParentheses Braces | |
highlight! link rubyCurlyBlockDelimiter Braces | |
highlight rubyMethodBlock ctermfg=132 | |
highlight! link javaScriptBraces Braces | |
highlight! link javaScriptParens Braces | |
highlight htmlTagName cterm=bold ctermfg=134 | |
highlight! link htmlTagN htmlTagName | |
highlight! link htmlSpecialTagName htmlTagName | |
highlight! link cssTagName htmlTagName | |
highlight! link cssSelectorOp Statement | |
highlight! default link cssIdentifier Identifier | |
" ******************************************************************************** | |
" Cursor ************************************************************************ | |
" ******************************************************************************** | |
set guicursor=n-v-c:block-Cursor | |
set guicursor+=n-v-c:blinkwait10 | |
set guicursor+=i:ver100-iCursor | |
set guicursor+=i:blinkwait90 | |
" ******************************************************************************** | |
" Status Message ***************************************************************** | |
" ******************************************************************************** | |
set laststatus=2 " Always show status line | |
" clear out status line | |
set statusline= | |
" file type | |
set statusline+=💻[%Y] | |
set statusline+=%{(mode()=='n')?'\ 📃\ ':''} | |
set statusline+=%{(mode()=='i')?'\ 📝\ ':''} | |
set statusline+=%{(mode()=='R')?'\ REPLACE\ ':''} | |
set statusline+=%{(mode()=='v')?'\ 🔎\ ':''} | |
set statusline+=%{(mode()=='V')?'\ 🔎\ ':''} | |
" read only mode | |
set statusline+=%r | |
" full file name | |
set statusline+=%-10F | |
" right justify everything after this line | |
set statusline+=%= | |
" cursor column position | |
set statusline+=\ 📜\ %2c\ ⋈\ %-l | |
" cursor line, total lines | |
set statusline+=\ of\ %L\ lines | |
" percentage | |
set statusline+=\ (%p%%) | |
" colorscheme | |
set statusline+=\ \ \ \ 🎨\ %{g:colors_name}\ | |
" ******************************************************************************** | |
" Mappings *********************************************************************** | |
" ******************************************************************************** | |
nmap <C-b> :tabprevious<cr> | |
nmap <C-n> :tabnext<cr> | |
nmap <C-t> :tabnew<cr> | |
" <CTRL+h> Focus on pane to left | |
nmap <C-h> <C-w>h | |
" <CTRL+h> Focus on pane down | |
nmap <C-j> <C-w>j | |
" <CTRL+h> Focus on pane up | |
nmap <C-k> <C-w>k | |
" <CTRL+h> Focus on pane to right | |
nmap <C-l> <C-w>l | |
" <=> Equal sized panes | |
nmap = <C-w>= | |
" normal mode: save | |
nnoremap <c-s> :w<cr> | |
" insert mode: escape to normal and save | |
inoremap <c-s> <esc>:w<cr> | |
" visual mode: escape to normal and save | |
vnoremap <c-s> <esc>:w<cr> | |
" Map semi-colon to colon (no need to press <SHIFT> | |
nmap ; : | |
" https://stackoverflow.com/questions/657447/vim-clear-last-search-highlighting | |
" MAJOR MOUSE PROBLEMS | |
" nnoremap <esc> :nohlsearch<cr> | |
" ******************************************************************************** | |
" Functions ********************************************************************** | |
" ******************************************************************************** | |
" -------------------------------------------------------------------------------- | |
" Interactive Status. | |
" -------------------------------------------------------------------------------- | |
function! DevPunks() | |
" (n) Normal mode | |
" (v) Visual mode | |
" (i) Insert mode | |
" (c) Command-line mode | |
" (h) all previous modes when in a help file | |
" (a) all previous modes | |
" (r) for |hit-enter| prompt | |
echom mode() | |
if mode() == 'i' | |
highlight statusline guibg=green ctermbg=green | |
elseif mode() == 'n' | |
highlight StatusLine ctermbg=blue | |
elseif mode() == 'r' | |
highlight StatusLine ctermbg=yellow | |
elseif mode() == 'c' | |
highlight StatusLine ctermbg=132 | |
elseif mode() == 'v' | |
highlight StatusLine ctermbg=red | |
elseif mode() == 'V' | |
highlight StatusLine ctermbg=red | |
else | |
endif | |
endfunction | |
" autocmd InsertEnter * call DevPunks() | |
" autocmd InsertLeave * call DevPunks() | |
autocmd InsertEnter * highlight! link StatusLine InsertColor | |
autocmd InsertLeave * highlight! link StatusLine NormalColor | |
" -------------------------------------------------------------------------------- | |
" Zoom / Restore window. | |
" -------------------------------------------------------------------------------- | |
function! s:ZoomToggle() abort | |
if exists('t:zoomed') && t:zoomed | |
execute t:zoom_winrestcmd | |
let t:zoomed = 0 | |
else | |
let t:zoom_winrestcmd = winrestcmd() | |
resize | |
vertical resize | |
let t:zoomed = 1 | |
endif | |
endfunction | |
command! ZoomToggle call s:ZoomToggle() | |
nnoremap <silent> + :ZoomToggle<cr> | |
" https://vim.fandom.com/wiki/Identify_the_syntax_highlighting_group_used_at_the_cursor | |
function! HighlightGroup() | |
let l:s = synID(line('.'), col('.'), 1) | |
echo synIDattr(l:s, 'name') . ' -> ' . synIDattr(synIDtrans(l:s), 'name') | |
endfun | |
command! Colors call HighlightGroup() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
echo Creating VIM plugin directory structure for `whoami` | |
mkdir -p ~/.vim/pack/`whoami`/start/ | |
" ----------------------------- | |
" Interface enhancements | |
" ----------------------------- | |
ZoomWin.vim - https://github.com/vim-scripts/ZoomWin | |
NerdTree - https://github.com/vim-scripts/The-NERD-tree | |
Vim Gutter - https://github.com/airblade/vim-gitgutter | |
" ----------------------------- | |
" Language Enviroment Macros | |
" ----------------------------- | |
endwise.vim - https://github.com/tpope/vim-endwise | |
Pear Tree - https://github.com/tmsvg/pear-tree | |
closetag.vim - https://github.com/alvan/vim-closetag | |
Syntastic - https://github.com/scrooloose/syntastic | |
Rails.vim - https://github.com/tpope/vim-rails | |
Minitest Highlighting - https://github.com/sunaku/vim-ruby-minitest | |
auto match - https://vim.fandom.com/wiki/Auto_end-quote_html/xml_attribute_values_as_you_type_in_insert_mode | |
" ----------------------------- | |
" Colors | |
" ----------------------------- | |
" colorschemes | |
:echo g:colors_name | |
" --------------------- | |
Jellybeans.vim (colorscheme) - https://github.com/vim-scripts/jellybeans.vim |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"+y[movement] " copy to clipboard buffer (including double quote) | |
" Similarly, to paste from clipboard, "+p | |
vim -p [file1 file2 file3 fileN] # vim opens file collection in tabs | |
vim -o [file1 file2 file3 fileN] # vim opens file collection in split buffers | |
* j – navigate downwards | |
* k – navigate upwards | |
* l – navigate right side | |
* h – navigate left side | |
go down by 10 lines, then type “10j”. | |
:n Jump to line number n. For example, to jump to line 42, you'd type :42 | |
0 – go to the starting of the current line. | |
^ – go to the first non blank character of the line. | |
$ – go to the end of the current line. | |
g_ – go to the last non blank character of the line. | |
H – Go to the first line of current screen. | |
M – Go to the middle line of current screen. | |
L – Go to the last line of current screen. | |
* ctrl+f – Jump forward one full screen. | |
* ctrl+b – Jump backwards one full screen | |
* ctrl+d – Jump forward (down) a half screen | |
* ctrl+u – Jump back (up) one half screen | |
% – Go to the matching braces, or parenthesis inside code. | |
N% – Go to the Nth percentage line of the file. | |
NG – Go to the Nth line of the file. | |
G – Go to the end of the file. | |
” – Go to the position where you were in NORMAL MODE while last closing the file. | |
^ – Go to the position where you were in INSERT MODE while last closing the file. | |
g – Go to the beginning of the file. | |
e – go to the end of the current word. | |
E – go to the end of the current WORD. | |
b – go to the previous (before) word. | |
B – go to the previous (before) WORD. | |
w – go to the next word. | |
W – go to the next WORD. | |
zt - move current cursor placement to top | |
zb - move current cursor placement to bottom | |
zz - move current cursor placement to center screen | |
WORD – WORD consists of a sequence of non-blank characters, separated with white space. | |
word – word consists of a sequence of letters, digits and underscores. | |
Example to show the difference between WORD and word | |
* 192.168.1.1 – single WORD | |
* 192.168.1.1 – seven words. | |
# Vim Paragraph Navigation | |
* { – Go to the beginning of the current paragraph. By pressing { again and again move to the previous paragraph beginnings. | |
* } – Go to the end of the current paragraph. By pressing } again and again move to the next paragraph end, and again. | |
Editing blocks of text | |
Note: the Vim commands marked with (V) work in visual mode, when you've selected some text. The other commands work in the command mode, when you haven't selected any text. | |
Vim command Action | |
~ Change the case of characters. This works both in visual and command mode. | |
In visual mode, change the case of highlighted characters. | |
In command mode, change the case of the character uder cursor. | |
> (V) Shift right (indent). | |
< (V) Shift left (de-indent). | |
c (V) Change the highlighted text. | |
y (V) Yank the highlighted text. In Windows terms, "copy the selected text to clipboard." | |
yy or :y or Y Yank the current line. You don't need to highlight it first. | |
d (V) Delete the highlighted text. In Windows terms, "cut the selected text to clipboard." | |
x Delete characters under the cursor. | |
X Delete characters before the cursor. | |
dd or :d Delete the current line. Again, you don't need to highlight it first. Deleting text | |
p Put the text you yanked or deleted. In Windows terms, "paste the contents of the clipboard". | |
Put characters after the cursor. Put lines below the current line. | |
P Put characters before the cursor. Put lines above the current line. | |
u Undo the last action. | |
U Undo all the latest changes that were made to the current line. | |
Ctrl + r Redo. | |
v Start highlighting characters. Use the normal movement keys and commands | |
to select text for highlighting. | |
V Start highlighting lines. | |
:%! nl -ba # Line Numbers | |
Finding Files | |
--------------------- | |
:pwd # print working directory | |
:cd # change directory | |
:find # file name (within path) | |
Search | |
--------------------- | |
* /i – Search for a pattern which will you take you to the next occurrence of it. | |
* ?i – Search for a pattern which will you take you to the previous occurrence of it. | |
* * - Go to the next occurrence of the current word under the cursor. | |
* # - Go to the previous occurrence of the current word under the cursor. | |
re-apply - The . command repeats the last change. A change, in this context, is inserting, deleting or replacing text. Being able to repeat this is a very powerful mechanism. If you organise your editing around it, many changes will become a matter of hitting just that . key. Watch out for making other changes in between, because it will replace the change that you were repeating. Instead you might want to mark the location with the m command, continue your repeated change and come back there later. | |
registers - When you are typing a phrase or sentence multiple times, there is an even quicker approach. Vim has a mechanism to record a macro. You type qa to start recording into register 'a'. Then you type your commands as usual and finally hit q again to stop recording. When you want to repeat the recorded commands you type @a. There are 26 registers available for this. | |
:tabnew [filename] # and Vim will load the file in the new tab (or defaults to empty buffer) | |
:tabs # displays all tabs | |
:tabn and :tabp switch between tabs, or you can use gt while you're in normal mode. | |
If you have a lot of tabs open, you can use :tabfirst, or just :tabfir, to jump to the first tab, and :tablast to jump to the last tab that's open. | |
if you don't like the existing shortcuts for the tab commands, you can add your own. For instance, if you want to make it easy to open a new tab, you might insert this into your .vimrc: | |
- Rearranging tabs | |
If you're really meticulous and want to position tabs just so in Vim, you can move the tabs to a specific spot in the tab order using :tabm n , where n is the position number that you want to use. If you don't give the :tabm command an argument, then the current tab will be moved to the last spot. | |
Vim starts tab numbering from 0, so if you have six tabs open, you'll have tab 0 through tab 5. So, if you're in the first tab and want to move it to the fourth position, you'd run :tab 3. | |
map <leader>tn :tabnew %<cr> | |
map <leader>tc :tabclose<cr> | |
map <leader>tm :tabmove | |
:split [file] # Splits the window horizontally. | |
:vsplit [file] # Splits the window vertically. | |
<Ctrl>-w # (mnemonic: control window) To move between windows you use <Ctrl>-w (mnemonic: control window). | |
# To move in a specific direction, add the relevant movement key. So, to move upwards: <Ctrl>-w+k. | |
# To close the active window use :q, just as you would to close a window normally. | |
<Ctrl>-w [HJKL] # to actually move buffer (i.e. K will move current buffer to top. * notice caps*) | |
:only # if split buffers, close all buffers except current one. | |
You can reduce/enlarge the size of the current window with <Ctrl>-w+- and <Ctrl>-w++, respectively. To specify the size of a window when you open it, prefix the :split command with the desired height/width in lines. For example, to show README in a window of 5 lines high: :5 :split README. | |
When splitting a window you can prepend a modifier command to tell where the | |
window is to appear: | |
:leftabove {cmd} left or above the current window | |
:aboveleft {cmd} idem | |
:rightbelow {cmd} right or below the current window | |
:belowright {cmd} idem | |
:topleft {cmd} at the top or left of the Vim window | |
:botright {cmd} at the bottom or right of the Vim window | |
CTRL-W x | |
CTRL-W CTRL-X Without count: Exchange current window with next one. If there | |
is no next window, exchange with previous window. | |
With count: Exchange current window with Nth window (first | |
window is 1). The cursor is put in the other window. | |
CTRL-W T Move window to a new tab. | |
Replace | |
Vim command Action | |
:rs/foo/bar/a Substitute foo with bar. r determines the range and a determines the arguments. | |
The range (r) can be | |
nothing Work on current line only. | |
number Work on the line whose number you give. | |
% The whole file. | |
Arguments (a) can be | |
g Replace all occurrences in the line. Without this, Vim replaces only the first occurrences in each line. | |
i Ignore case for the search pattern. | |
I Don't ignore case. | |
c Confirm each substitution. You can type y to substitute this match, n to skip this match, a to substitute this and all the remaining matches ("Yes to all"), and q to quit substitution. | |
Examples | |
:452s/foo/bar/ Replace the first occurrence of the word foo with bar on line number 452. | |
:s/foo/bar/g Replace every occurrence of the word foo with bar on current line. | |
:%s/foo/bar/g Replace every occurrence of the word foo with bar in the whole file. | |
:%s/foo/bar/gi The same as above, but ignore the case of the pattern you want to substitute. This replaces foo, FOO, Foo, and so on. | |
:%s/foo/bar/gc Confirm every substitution. | |
:%s/foo/bar/c For each line on the file, replace the first occurrence of foo with bar and confirm every substitution. | |
# Sessions | |
-------------------------- | |
:mksession my_session.vim # creates session with current layout info | |
:source my_session.vim # restore session from file (or :~>vim -S my_session.vim) | |
BUFFERS | |
-------------------- | |
:bn[ext] # Next buffer | |
:bp[revious] # Previous Buffer |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment