Skip to content

Instantly share code, notes, and snippets.

@krzysdb
Last active August 18, 2018 16:26
Show Gist options
  • Save krzysdb/77bc83294b2cb953b49c2d57ed5d4b84 to your computer and use it in GitHub Desktop.
Save krzysdb/77bc83294b2cb953b49c2d57ed5d4b84 to your computer and use it in GitHub Desktop.
##### vim koans
# https://sanctum.geek.nz/arabesque/vim-koans/
# https://sanctum.geek.nz/etc/emperor-sh-and-the-traveller.txt
# https://blog.samwhited.com/2015/04/the-dharma-of-vi/
#### remapping keys links
# remap escape - https://news.ycombinator.com/item?id=13100718
# spacebar leader - https://news.ycombinator.com/item?id=6916194
# https://www.reddit.com/r/vim/comments/4d6iym/what_do_you_all_do_with_caps_lock_control_and/
# http://vim.wikia.com/wiki/Avoid_the_escape_key
#### Copy and paste from cliboard
# http://vim.wikia.com/wiki/Mac_OS_X_clipboard_sharing
#### Break lines at 80 characters
# https://stackoverflow.com/questions/235439/vim-80-column-layout-concerns
" Highlights the background in a subtle red for text that goes
" over the 90 column limit
" To see details about colors check
" :help ctermbg
" Check colors with
" :runtime syntax/colortest.vim
highlight OverLength ctermbg=Magenta ctermfg=White guibg=#592929
match OverLength /\%91v.\+/
#### Repeat characters
# [1] http://vim.wikia.com/wiki/Repeat_last_change
# [1] - use colon(.) effectively, copy line to multiple location
# [2] https://stackoverflow.com/questions/5054128/repeating-characters-in-vim-insert-mode#5806745
# Insert new lines
# http://vim.wikia.com/wiki/Insert_newline_without_entering_insert_mode
# Spelling and completion
# http://thejakeharding.com/tutorial/2012/06/13/using-spell-check-in-vim.html
# http://vimdoc.sourceforge.net/htmldoc/spell.html#spell-load
# http://vim.wikia.com/wiki/Dictionary_completions
# http://vim.wikia.com/wiki/Completion_using_a_syntax_file (not yet)
# Vim GUI - https://www.cs.swarthmore.edu/help/vim/vim7.html
### Vim + tmux
# https://robots.thoughtbot.com/seamlessly-navigate-vim-and-tmux-splits
# Vim Help
# http://vim.wikia.com/wiki/Learn_to_use_help
Useful configuration
---------------------
" make jj do esc
inoremap jj <Esc>
" make escape do nothing
inoremap <Esc> <Nop>
" Ctrl-j/k inserts bellow/above
nnoremap <silent><C-j> m`o<Esc>``
nnoremap <silent><C-k> m`O<Esc>``
" Spell check
" Install plugin spell.vim and configure in ~/.vimrc
set spelllang=en
set spellfile=$HOME/Dropbox/vim/spell/en.utf-8.add
" set spelling for markdown and gitcommit files
autocmd BufRead,BufNewFile *.md setlocal spell
autocmd FileType gitcommit setlocal spell
" autocomplete
" set wildmenu wildmode=longest:full,full
Mapping
-------------
" In insert mode change ';so' to 'System.out.println();' and stay in insert mode between parenthesis
:imap ;so System.out.println();<Left><Left>
" In insert mode move to the end of line(and move to next field) when typing ';ne'
:imap ;ne <Esc>$a
" Select text in visual mode and put bold html tags around it
:vmap <buffer> ;bo "zdi<b><C-r>z</b><Esc>
" Use F6 to move between windows
:nnoremap <F6> <C-w>w
" Use F6 to move between windows in reverse direction
:nnoremap <F6> <C-w>W
" Toggle Vim highlight search term to switch it off
" https://www.linux.com/news/vim-tips-using-vim-mappings-and-abbreviations
function ToggleHLSearch()
if &hls
set nohls
else
set hls
endif
endfunction
" map Ctrl-n to toogle highlight search
nmap <silent> <C-n> <Esc>:call ToggleHLSearch()<CR>
Registers
------------
:reg - show named registers and what is in them
"5p - paste what is in register "5
" Register macro
qk - register typed text/code into register k (q again to stop recording)
@k - execute recorded macro
@@ - repeat last macro 5 times
"kp - print macro k
"kd - replace register k with what cursor is on (remove it)
"ky$ - copy to register text from current position to end of line
"kyy - copy to register entire line (yank)
Bash vi mode
-------------------
In .bashrc add line
set -o vi
This enable bash command line to have a vi functionality
Check plugins
--------------
https://github.com/easymotion/vim-easymotion
Typical vi session
-------------------
Type "vi file.txt" at command prompt
Move cursor to where new text will be added
Type "i" to change to insert mode
Type new text
Type ESC to go back to command mode
type ":wq" and ENTER to write the file and quit
ZZ - exit witout any changes to file (Shift+z+z)
<C-z> - (Ctrl+z) stop vim process and go back to command line
to return to stoped vim process type "fg"
While in insert mode
--------------------
ESC - change to command mode
any text typed is entered at the cursor
Always used
-----------
:w - save existing file
:q! - exist and discard changes
:wq - save file and exit
While in command mode (case sensitive)
---------------------------------------
move the cursor with arrow keys; if there aren't any arrow keys, use j(down),k(up),h(left),l(right)
i - change to insert mode (before cursor)
I - insert at the begining of the line
a - change to insert mode(append) (after cursor)
A - change to insert mode(append) (at end of line)
ea - Insert (append) at the end of the word
r - replace one character
3rt - replace 3 characters with 't'
R - overwrite text
x - delete one character
3x - delete 3 characters
xp - swap 2 characters,(delete first and put after second, "teh" to "the")
dw - delete word(or from cursor position in word till next word start)
3dw - delete 3 words
d$ - delete to the end of line($ is end)
d0 - delete from current position to start of line(0 is start)
de - delete word till including the end of the word
dd - delete one line
3dd - delete 3 lines
# Replace OLD word with NEW word
/OLD " search OLD word
cwNEW " when at the word change it to NEW word
n " repeat last search
. " repeat last edit
yy - yank line (copy)
3yy - yank 3 lines
y$ - yank line without end of line character (\n)
p - paste deleted or yanked text after cursor
P - paste deleted or yanked text before cursor
i=<Esc> 40. - insert '=' and Esc to normal mode, repeat '=' 40 times
0y$ - yank line, delete line and replace, then repeat in new location
S<C-R>0<Esc> " S delete line without '\n' and start insert mode, Ctrl+R insert content of register
" register 0 contains the text that was coppied with y$
. " repeat last operation
<C-o>80i=<Esc> - <C-o> issue command without leaving INSERT mode, inserts 80 times '=' character, leave INSERT mode
<C-o>:norm 80i=<CR> - do the same without leaving INSERT mode
Copy text
------------
ma d'a p - mark top of paragraph, move to bottom and delete to buffer "a", move to new location and paste
vdp - in Visual mode select top of paragraph, move to bottom and delete, move new location and paste
Copy text from one file to another
------------------------------------
ma " Mak the lineas mark "a"
y'a "Go to bottom line to be copied and yank the text form cursor location to mark "a"
:split second-file " Open anotherwindow containing the second file, go to the location for the copied text
p " Put text in new location
Copy text from one file to another(Visual mode)
-----------------------------------------------
v " Start visual mode start selecting text from top to bottom
y " yank (y) the text
:split second-file " Open another window
p " put the text after cursor
Sort lines of text
------------------------------------
ma "mark start
!'asort "move to end and run through unix sort command
Sort lines of text (visual)
------------------------------------
v " start visual mode
!sort " move to end of lines and run sort command
G - go to end of the file
1G - go to top of the file
gg - works as 1G
g_ - go to the last nonempty character in the line
tx - Jump to before next occurrence of character x
80| - Go to 80 column position (pipe char)
25G60| - Got to 25th row and 60th column
; - Repeat previous f, t, F, or T movement
, - Repeat previous f, t, F, or T movement, backwards
} - jump to next paragraph
{ - jump to previous paragraph
zz - center on screen previous cursor position
<C-e> - Move screen down one line (without moving cursor)
<C-y> - Move screen up one line (without moving cursor)
<C-b> - Move back one full screen
<C-f> - Move forward one full screen
<C-u> - Move back 1/2 a screen
<C-d> - Move forward 1/2 a screen
J - merge next line with this one
3J - merge 3 lines together(current + next 2 lines)
/ - search, follow / with text to find
%s/old/new/g - substitute; replace "old" with "new" on all lines
:g/pattern/d - delete all lines that match the pattern
:1,$s/\([^,]*\), \(.*$\)/\2 \1/ - change "Last, First" to "First, Last" in a file
Opening and closing tabs
------------------------
# http://vim.wikia.com/wiki/Using_tab_pages
At start to open files in tabs
vim -p first.txt second.txt
gvim -p *.txt
Tab actions
------------
:tabedit - open new empty tab
:tabedit file.c - edit file.c in a new tab
:tabfind file.c - open a new tab with file.c, searching the 'path' to find it
:tabclose - close current tab
:tabclose {i} - close i-th tab
:tabonly - close all other tabs (show only the current tab)
:tabs - list all tabs including their displayed windows
:tabm 0 - move current tab to first
:tabm - move current tab to last
:tabm {i} - move current tab to position i+1
:tabn - go to next tab
:tabp - go to previous tab
:tabfirst - go to first tab
:tablast - go to last tab
gt - go to next tab
gT - go to previous tab
3gt - go to tab in position 3
Quickfix window
---------------
vim -q errors.txt - open vim in Quickfix mode
:copen - open Quickfix window(at the bottom) in vim
:close - close Quickfix window
<C-w>w - navigate between the editor and Quickfix window (Ctrl+w+w)
:help quickfix - more info
Vim built-in vim completion
---------------------------
(auto complete/code complete) feature
type "su" and Ctrl+P - popup wil show with list of words starting with "su"(from opened files)
:set ignorecase - when Ctrl+P search words regardless case
:set infercase - search for word "thimble" lowercase instead of "Thimble"
Spelling
---------
:set spell spelllang=en_us - set spelling in all windows
:setlocal spell spelllang=en_us - set spelling only in local buffer
:set nospell - turn off spelling
]s - move cursor to next misspeled word
[s - move to previous misspelled word
z= - for word under cursor suggest correctly spelled ords
zg - add word under cursor as a good word
zw - mark word in dictionary as incorrect (uncommon use)
CTRL-p
" can put in .vimrc to toggle spell checking
" nnoremap <silent> <leader>s :set spell!<cr>. (the <leader> key defaults to ``)
Save changes to write protected file
-------------------------------------------------------
:shell " start shell
$chmod u+x file.txt " enable write to file
$exit " exit shell and return to vim
:w! " save changes
Edit all files containing a given word
---------------------------------------
$ vim `fgrep -l identation_level *.c`
$ vim $(fgrep -l identation_level *.c)
# Inside vim
# :grep >word< >file-list<
Miscellaneous
-------------
:help! - Funny :)
:help 42
:help quotes
:help holy-grail - collection of very useful commands
Unorganized
------------
gi - focus the first (or n-th) text input box on the page
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment