Vimの使い方メモ
TL;DR
The tips of vim, my favorite editor.
改行の置換
vim
の中で改行への置換を行うには\r
、改行を置換するには\n
を用いる必要がある。
複数行の一括操作
インデント
- 「ctrl」+「v」でビジュアルモードにして、複数行を選択
- 「>」でインデントする
インデント削除はビジュアルモードで複数行が選択された状態で「<」で。
コメントアウトを一括挿入
- 「ctrl」+「v」でビジュアルモードにして、複数行を選択
- 「shift」+「i」で挿入モードにする
- 「#」文字を挿入(この時、1行目だけ挿入されているように見えるが、問題ない)
- 「esc」キーを2回押す(こうすると、最初に選択された複数行がすべてコメントアウトされる)
コメントアウトを一括削除
- 「ctrl」+「v」でビジュアルモードにして、複数行を選択
- カーソルをコメントアウト文字にいる状態で、「d」で一括削除できる
ステータスライン
- %F – ファイルのフルパス。
- %m – 編集されているなら [+]。リードオンリーなら [-]。
- %h – Help buffer なら [HELP] と表示。
- %w – Preview window なら [PREVIEW] と表示。
- %< – ウィンドウの横幅が縮まってもここまでは表示することを保証。
- %{&fenc!=''?&fecn:&enc} – fileencoding が設定されていればその値、設定されていなければ encoding を表示。
- %{&ff} – fileformat の値を表示。%{&fileformat} の省略形。(dos, unix, mac)
- %Y – filetype の値を表示。通常はこれに対応する syntax file が読み込まれているはず。
- %02B – カーソル位置の文字コードを16進数で表示。
- %l – カーソル位置の行番号。
- %L – ファイルの行数。
- %02v – カーソル位置の桁番号。
- %c – カーソル位置の列番号
- %p – テキスト全体に対するカーソル位置までの割合(パーセント表示)
- %% – 文字「%」を表示
タブ操作
複数のファイルをタブで同時に開く
$ vim -p file1 file2 file3
これで、3つのファイルをvim内でそれぞれタブで開くことになる。
vim実行中にファイルを新規タブで開く
コマンド行に:tabnew [file]
で新規タブで、file(必須ではない)を開くことができる。
タブの移動
「gt」(次のタブ)か「gT」(前のタブ)で切り替えられる。 「gT」が押しにくいと思う場合、.vimrcファイルとかに、次のように「gr」にmappingして使うことができる
nnoremap gr :tabprevious<CR>
開いたタブを一括で閉じる
:tabo<CR>
vim中快速生成列项递增数列(序号)
例如,想生成一下连续数字序号。
1 2 3 4
做法:在命令行模式下输入:r !seq m n
(m是开始数字,n是结束的数字)
:r !seq 5 10
的话,就会生成以下的序号
5 6 7 8 9 10
Nouns in Vim
Text Objects
- iw => "inner word" (works from anywhere in a word)
- it => "inner tag" (the contents of an HTML tag)
- i" => "inner quotes"
- ip => "inner paragraph"
- as => "a sentence"
上記Objectsに対し、次のような操作が使える
- diw => wordを削除
- ciw => wordを置換
- viw => word全体をビジュアル選択
- yiw => word全体をコピーする
- vip => paragraph全体をビジュアル選択
- etc.
Parameterized Text Objects
- f, F => "find" the next character
- t,T => "find" the next character
- / => Search (up to the next match)
Plugins
vim-surround
HTMLやXMLのタグを削除/変更/追加する等に大変便利。
vim-repeat
vimのplugin管理方法
vim-plug
vim-plug は vim の plugin manager の1つ。2020/12/22時点 github の Star 数を比較してみた。 .vimrcへの記述が少なくて済み、人気もあるため、自分はまずこれをマスターしようと思った。
Plugin Manager | Star | Remarks |
---|---|---|
Vundle | 22k | スター数が一番多い |
vim-pathogen | 11.5k | |
vim-plug | 21.6k | .vimrc への記述が少なくて済む |
NeoBundle | 2.3k | 日本製 |
dein.vim | 2.8k | NeoBundle の後継 |
本家: GitHub - junegunn/vim-plug: Minimalist Vim Plugin Manager
how to install vim-plug
In the case maxOS / Linux
$ curl -fLo ~/.vim/autoload/plug.vim --create-dirs https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
In fact, the above command is in a line.
how to add plugin
An example makes vim auto-completed while programming is "deoplete". The official site is GitHub - Shougo/deoplete.nvim: Dark powered asynchronous completion framework for neovim/Vim8
pre-condition to intall deoplete
- python3.6 or later
Because deoplete is written in python, so...
$ python3 --version Python 3.9.1
- python3 can be used in vim
It can be confirmed via the following command
$ vim --version | grep "+python3" +conceal +linebreak +python3 +visualextra
If +python3
is effectice, it is Okay. If the resule is -python3
, you maybe to the following,
$ brew uninstall vim $ brew install vim $ which vim /usr/local/bin/vim $ alias vim=/usr/local/bin/vim $ vim --version | grep "+python3"
Add alias vim=/usr/local/bin/vim
to .bashrc ( or .bash_profiile etc.) will be convenient.
- install package neovim
$ pip3 install neovim
it is okay to install deoplete
- Add the following lines to .vimrc
call plug#begin('~/.vim/plugged') Plug 'scrooloose/nerdtree', { 'on': 'NERDTreeToggle' } Plug 'Shougo/deoplete.nvim' Plug 'roxma/nvim-yarp' Plug 'roxma/vim-hug-neovim-rpc' let g:deoplete#enable_at_startup = 1 " https://github.com/deoplete-plugins/deoplete-jedi Plug 'deoplete-plugins/deoplete-jedi' Plug 'Shougo/neco-vim' call plug#end()
- in vim, add plugins
:PlugInstall
The plugins are begun to install and when "Finishing ... Done!" is displayed, it was completed.
how to delete plugin
- Comment out the lines in .vimrc or delete them
call plug#begin('~/.vim/plugged') " Plug 'scrooloose/nerdtree', { 'on': 'NERDTreeToggle' } " Plug 'Shougo/deoplete.nvim' " Plug 'roxma/nvim-yarp' " Plug 'roxma/vim-hug-neovim-rpc' " let g:deoplete#enable_at_startup = 1 " https://github.com/deoplete-plugins/deoplete-jedi " Plug 'deoplete-plugins/deoplete-jedi' " Plug 'Shougo/neco-vim' call plug#end()
- In vim, show plugin's status
:PlugStatus
- In vim, clean plugin
: PlugClean
- Confirm the question with "y"
Key Mapping
vimでキーマッピングする際に考えたほうがいいこと - derisの日記
nnoremap
Add the following to .vimrc
" reload .vimrc nnoremap <Space>s :source $HOME/.vimrc<CR> nnoremap j gj nnoremap k gk nnoremap gj j nnoremap gk k
mapping to Leader key
Add the following to .vimrc
let mapleader = "\<space>"" " mapping to ^ nnoremap <Leader>a ^ " mapping to $ nnoremap <Leader>; $ " for NERDTree nnoremap <Leader>n :NERDTreeToggle<CR>
Edit binary file
Add the following to .vimrc
" binary (xxd)(vim -b binary file or *.o files etc.) augroup Binary autocmd! autocmd BufReadPre *.o,*.a,*.out let &binary = 1 autocmd BufReadPost * if &binary | silent %!xxd autocmd BufReadPost * set ft=xxd | endif autocmd BufWritePre * if &binary | %!xxd -r autocmd BufWritePre * endif autocmd BufWritePost * if &binary | silent %!xxd autocmd BufWritePost * set nomod | endif augroup END