owned this note
owned this note
Published
Linked with GitHub
# Vim 基本操作
在電光石火中我看不清你的身影
![Vim Logo](https://upload.wikimedia.org/wikipedia/commons/9/9f/Vimlogo.svg =300x)
## Installation
Debian:
```shell
sudo add-apt-repository ppa:jonathonf/vim
sudo apt-get update
sudo apt-get install vim tmux zsh
```
Mac:
```shell
brew install vim tmux zsh
```
## 電光石火
[![How哥-紫禁之巔](https://img.youtube.com/vi/kyfoEjAxgEw/0.jpg)](https://www.youtube.com/watch?v=kyfoEjAxgEw)
## Basic
- Normal mode `<ESC>`
- Insert mode `i`
- Visual mode `v`
- Command line `:`
exit: `:q`
## Workflow
`vim hello.sh`
```sh
#!/bin/bash
echo OwO
# ...
```
`:wq`
`chmod +x hello.sh && ./hello.sh` or
`bash hello.sh`
### Another Example
`vim hello.py`
```python
#!/usr/bin/env python3
print('Hello from the other side')
# ...
```
`:wq`
`chmod +x hello.py && ./hello.py` or
`python3 hello.py`
## Practice
複製一些文字來練習操作 vim
```
cat << EOF > article.txt
Microsoft released its latest Windows 10 October
2018 Update earlier this week, but some users are
reporting serious issues. Threads on Reddit,
Microsoft’s own support site, and other forums show
that some Windows 10 users are upgrading to the
October 2018 Update and having their documents,
photos, or even entire user profiles wiped out.
While some have discovered a temporary fix for the
issue, it’s not clear why exactly this is
happening.
EOF
```
> [reference](https://www.theverge.com/2018/10/5/17940902/microsoft-windows-10-october-2018-update-deleting-documents-issues)
複製一些程式碼來練習
```shell
cat << MEOW > demo.py
#!/usr/bin/env python3
# vim: ts=4:sts=4:sw=4:expandtab
import calendar, argparse
from argparse import ArgumentParser
def main(year=2018, month=10):
# display the calendar
print(calendar.month(year, month))
if __name__ == '__main__':
parser = ArgumentParser(description='display month',
argument_default=argparse.SUPPRESS)
parser.add_argument('--month',type=int,choices=list(range(1,13)))
parser.add_argument('--year', type=int)
args = parser.parse_args()
main(**vars(args))
MEOW
```
[modeline magic](http://vim.wikia.com/wiki/Modeline_magic)
[pep8](https://www.python.org/dev/peps/pep-0008/#tabs-or-spaces)
## Normal Mode
- movement: `h` :arrow_left: , `j` :arrow_down: , `k` :arrow_up: , `l` :arrow_right:
- move by word: `w` word, `e` end, `b` back
- number: `3w`, `4j`
- top: `gg`
- bottom: `G`
- back to last postion: ``
- line begin: `0`, `^`
- line end: `$`
- copy (combined with others): `y`, `y3w`, `yy`
- delete: `x`, `d`, `dw`, paste: `p`
- undo: `u`, `Crtl` + `r`
- substitute: `s`, change: `c`, `cw`
- newline: `o`, `O`
- insert: `i`, `I`, append: `a`, `A`
### Advanced
- 縮排: `=`
- replace: `r`, `R`
- `gg=G`, `ggyG`, `ci'`, `da"`
## Visual Mode
- select: `v`, `V`, `Ctrl` + `v`
- upper case: `U`, `u`
- exit: `<ESC>`
## Command
- quit vim: `:q`
- write: `:w`
- split window: `:sp`, `:vsp`
- substitute: `:s/pattern/new_string/g`
- search: `/pattern`, `:noh`
- paste: `:pu`, `:pu!`
- select range: `:2,3`
- select whole: `:%` = `:1,$`
- external command: `:!ls`
- help: `:help help`
### Advanced
`:wqa`, `:qa!` (do sth. to all windows)
`:version`
`:reg`
`:%s/old/new/g`, `:%!xxd`, `:%!xxd -r`, `:%s/\s*$//g | w | noh`
prefix: `Ctrl` + `w`
## .vimrc
config path: `~/.vimrc`, `~/.vim/vimrc`
[my dotfiles](https://github.com/doraeric/dotfiles)
[others](https://github.com/search?q=dotfiles)
[Vundle](https://github.com/VundleVim/Vundle.vim)
## Resource
[cheat sheet](https://vim.rtorr.com/)
[telegram](https://t.me/vim_tw)
---
# Tmux
config path: `~/.tmux.conf`
`tmux`, `tmux a`
prefix: `Ctrl` + `b`
detach: `<prefix>` + `d`
`<prefix>` + `c`/`x`/`%`/`"`
command: `<prefix>` + `:`
# zsh, oh-my-zsh
config path: `~/.zshrc`
[oh-my-zsh](https://github.com/robbyrussell/oh-my-zsh)
[powerline font](https://github.com/powerline/fonts)
# Hacktoberfest
[github](https://github.com)
[hacktoberfest 2018](https://hacktoberfest.digitalocean.com/)
## Git
fork first
```
git clone xxx
vi xxx
git add -A
git status
git commit -m "xxx"
git push
```