[vim \- How to get visually selected text in VimScript \- Stack Overflow](https://stackoverflow.com/questions/1533565/how-to-get-visually-selected-text-in-vimscript)
``` vim
function!s:get_visual_selection()
" Why is this not a built-in Vim script function?!
let Mlen ={ s ->strlen(substitute(s,".","x","g"))}
letl= Mlen(selected)
ifl>0
execute"normal! v".(l-1)."\<Right>\"_x"
endif
return selected
endfunction
```
#### 特定行に内容をセットする
``` vim
function!s:setlines(pos,lines)
iflen(a:lines)==0
execute':'.a:pos.'d'
return
endif
callsetline(a:pos,a:lines[0])
callappend(a:pos,a:lines[1:])
endfunction
```
#### 範囲選択コマンド
``` vim
function! XXX()range
forninrange(a:firstline,a:lastline)
letline=getline(n)
endfor
endfunction
command!-range XXX <line1>,<line2>call XXX()
```
#### 外部コマンド
[vim-clang-format/clang_format.vim at master · rhysd/vim-clang-format](https://github.com/rhysd/vim-clang-format/blob/master/autoload/clang_format.vim#L14)
[Is there a way to get the number of lines of a buffer in Vim Script? \- Stack Overflow](https://stackoverflow.com/questions/14544618/is-there-a-way-to-get-the-number-of-lines-of-a-buffer-in-vim-script)
[vimscript \- Run global with range from within function \- Vi and Vim Stack Exchange](https://vi.stackexchange.com/questions/9037/run-global-with-range-from-within-function)
[vimscript - How can I append text to the current line? - Vi and Vim Stack Exchange](https://vi.stackexchange.com/questions/12445/how-can-i-append-text-to-the-current-line)
### source
#### 実はURLを指定してファイルを読み込むことができる
とある環境の内部では`wget`が実行されていた
e.g. `source https://raw.githubusercontent.com/mhinz/vim-galore/master/static/minimal-vimrc.vim`
#### sourceしたvimファイル中での早期return
`finish`
### 関数内で関数定義可能
e.g. [vim\-codefmt/yapf\.vim at 5ede026bb3582cb3ca18fd4875bec76b98ce9a12 · google/vim\-codefmt](https://github.com/google/vim-codefmt/blob/5ede026bb3582cb3ca18fd4875bec76b98ce9a12/autoload/codefmt/yapf.vim#L28)
[Mapping keys in Vim \- Tutorial \(Part 1\) \| Vim Tips Wiki \| FANDOM powered by Wikia](http://vim.wikia.com/wiki/Mapping_keys_in_Vim_-_Tutorial_(Part_1) )
`<sciprt>`を付加すると意図しない上書きを禁止しやすい
> The following characters may be displayed before the {rhs} of the map:
```
* The {rhs} of the map is not re-mappable. Defined using the
':noremap' or ':nnoremap' or ':inoremap', etc. commands.
& Only script local mappings are re-mappable in the {rhs} of the
map. The map command has the <script> attribute.
@ A buffer local map command with the <buffer> attribute.
[nvim-lua-guide-ja/README.ja.md at master · willelz/nvim-lua-guide-ja](https://github.com/willelz/nvim-lua-guide-ja/blob/master/README.ja.md)
### luaからvimの変数を参照する
[nvim-lua-guide-ja/README.ja.md at master · willelz/nvim-lua-guide-ja](https://github.com/willelz/nvim-lua-guide-ja/blob/master/README.ja.md#%E3%83%A1%E3%82%BF%E3%82%A2%E3%82%AF%E3%82%BB%E3%82%B5%E3%83%BC%E3%82%92%E4%BD%BF%E7%94%A8%E3%81%99%E3%82%8B-1)
## リモート
### scpプロトコルでのリモートファイル編集
`:e scp://x.x.x.x//Users/xxx/tmp/README.md`
*`~`は使えない
* リモートのファイル編集時に無理やりsudoして保存する方法は使えないことに注意
* ホームディレクトリからの相対パスとする場合は`/`を先頭につける必要がない
## Neovim
### Vim scriptでのバージョン確認
``` vim
:echo has('nvim')
:echo has('nvim-0.1')
:echo has('nvim-0.1.6')
```
### 色付きのメッセージを簡単に出力する
``` vim
luavim.api.nvim_echo({{"This is a "},{"warning","WarningMsg"},{" message.\n"},{"NOTE: This is a comment.","Comment"},}, true,{})
call nvim_echo([["This is a "],["warning","WarningMsg"],[" message.\n"],["NOTE: This is a comment.","Comment"],],v:true,{})
### [fatih/vim\-go: Go development plugin for Vim]( https://github.com/fatih/vim-go )
*`:GoFillStruct`: 構造体のフィールドを自動生成する
### [ripxorip/aerojump\.nvim: Aerojump is a fuzzy\-match searcher/jumper for Neovim with the goal of quick keyboard navigation]( https://github.com/ripxorip/aerojump.nvim )
また、[airblade/vim\-gitgutter: A Vim plugin which shows git diff markers in the sign column and stages/previews/undoes hunks and partial hunks\.](https://github.com/airblade/vim-gitgutter)と組み合わせた際に、例えば、`GitGutterAddLine`などは`DiffAdd`へひも付くので、その行の背景色がすべて上書きされてしまうので、独自に設定を上書きする必要があることに注意
[vim - How to get visually selected text in VimScript - Stack Overflow](https://stackoverflow.com/questions/1533565/how-to-get-visually-selected-text-in-vimscript)
```vim
function!s:get_visual_selection()
" Why is this not a built-in Vim script function?!
let Mlen ={ s ->strlen(substitute(s,".","x","g"))}
letl= Mlen(selected)
ifl>0
execute"normal! v".(l-1)."\<Right>\"_x"
endif
return selected
endfunction
```
#### 特定行に内容をセットする
```vim
function!s:setlines(pos,lines)
iflen(a:lines)==0
execute':'.a:pos.'d'
return
endif
callsetline(a:pos,a:lines[0])
callappend(a:pos,a:lines[1:])
endfunction
```
#### 範囲選択コマンド
```vim
function! XXX()range
forninrange(a:firstline,a:lastline)
letline=getline(n)
endfor
endfunction
command!-range XXX <line1>,<line2>call XXX()
```
#### 外部コマンド
[vim-clang-format/clang_format.vim at master · rhysd/vim-clang-format](https://github.com/rhysd/vim-clang-format/blob/master/autoload/clang_format.vim#L14)
[Is there a way to get the number of lines of a buffer in Vim Script? - Stack Overflow](https://stackoverflow.com/questions/14544618/is-there-a-way-to-get-the-number-of-lines-of-a-buffer-in-vim-script)
[vimscript - Run global with range from within function - Vi and Vim Stack Exchange](https://vi.stackexchange.com/questions/9037/run-global-with-range-from-within-function)
[vimscript - How can I append text to the current line? - Vi and Vim Stack Exchange](https://vi.stackexchange.com/questions/12445/how-can-i-append-text-to-the-current-line)
### source
#### 実はURLを指定してファイルを読み込むことができる
とある環境の内部では`wget`が実行されていた
e.g. `source https://raw.githubusercontent.com/mhinz/vim-galore/master/static/minimal-vimrc.vim`
#### sourceしたvimファイル中での早期return
`finish`
### 関数内で関数定義可能
e.g. [vim-codefmt/yapf.vim at 5ede026bb3582cb3ca18fd4875bec76b98ce9a12 · google/vim-codefmt](https://github.com/google/vim-codefmt/blob/5ede026bb3582cb3ca18fd4875bec76b98ce9a12/autoload/codefmt/yapf.vim#L28)
[Mapping keys in Vim - Tutorial (Part 1) | Vim Tips Wiki | FANDOM powered by Wikia](http://vim.wikia.com/wiki/Mapping_keys_in_Vim\_-\_Tutorial\_(Part_1)) `<sciprt>`を付加すると意図しない上書きを禁止しやすい
> The following characters may be displayed before the {rhs} of the map:
```
* The {rhs} of the map is not re-mappable. Defined using the
':noremap' or ':nnoremap' or ':inoremap', etc. commands.
& Only script local mappings are re-mappable in the {rhs} of the
map. The map command has the <script> attribute.
@ A buffer local map command with the <buffer> attribute.
通常,使えない [Multiple file types in vim - Stack Overflow](https://stackoverflow.com/questions/2601403/multiple-file-types-in-vim)
> ou can specify to use multiple filetypes at the same time. For example: :setfiletype html.php But most of filetype plugings and syntax files are not designed for such cases. See also :help 'filetype'
[nvim-lua-guide-ja/README.ja.md at master · willelz/nvim-lua-guide-ja](https://github.com/willelz/nvim-lua-guide-ja/blob/master/README.ja.md)
### luaからvimの変数を参照する
[nvim-lua-guide-ja/README.ja.md at master · willelz/nvim-lua-guide-ja](https://github.com/willelz/nvim-lua-guide-ja/blob/master/README.ja.md#%E3%83%A1%E3%82%BF%E3%82%A2%E3%82%AF%E3%82%BB%E3%82%B5%E3%83%BC%E3%82%92%E4%BD%BF%E7%94%A8%E3%81%99%E3%82%8B-1)
## リモート
### scpプロトコルでのリモートファイル編集
`:e scp://x.x.x.x//Users/xxx/tmp/README.md`
*`~`は使えない
* リモートのファイル編集時に無理やりsudoして保存する方法は使えないことに注意
* ホームディレクトリからの相対パスとする場合は`/`を先頭につける必要がない
## Neovim
### Vim scriptでのバージョン確認
```vim
:echo has('nvim')
:echo has('nvim-0.1')
:echo has('nvim-0.1.6')
```
### 色付きのメッセージを簡単に出力する
```vim
luavim.api.nvim_echo({{"This is a "},{"warning","WarningMsg"},{" message.\n"},{"NOTE: This is a comment.","Comment"},}, true,{})
call nvim_echo([["This is a "],["warning","WarningMsg"],[" message.\n"],["NOTE: This is a comment.","Comment"],],v:true,{})
### [fatih/vim-go: Go development plugin for Vim](https://github.com/fatih/vim-go)
*`:GoFillStruct`: 構造体のフィールドを自動生成する
### [ripxorip/aerojump.nvim: Aerojump is a fuzzy-match searcher/jumper for Neovim with the goal of quick keyboard navigation](https://github.com/ripxorip/aerojump.nvim)
また、[airblade/vim-gitgutter: A Vim plugin which shows git diff markers in the sign column and stages/previews/undoes hunks and partial hunks.](https://github.com/airblade/vim-gitgutter)と組み合わせた際に、例えば、`GitGutterAddLine`などは`DiffAdd`へひも付くので、その行の背景色がすべて上書きされてしまうので、独自に設定を上書きする必要があることに注意