[vim - How to get visually selected text in VimScript - Stack Overflow](https://stackoverflow.com/questions/1533565/how-to-get-visually-selected-text-in-vimscript)
+clientserver +jumplist +popupwin +user_commands
+clipboard +keymap +postscript +vartabs
```vim
+cmdline_compl +lambda +printer +vertsplit
function!s:get_visual_selection()
+cmdline_hist +langmap +profile +virtualedit
" Why is this not a built-in Vim script function?!
[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! XXX()range
``` vim
forninrange(a:firstline,a:lastline)
function!s:get_visual_selection()
letline=getline(n)
" Why is this not a built-in Vim script function?!
[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)
letlines[0]=lines[0][column_start -1:]
returnjoin(lines,"\n")
```vim
endfunction
function!s:has_vimproc() abort
if!exists('s:exists_vimproc')
function!s:delete_visual_selection()
try
let selected =s:get_visual_selection()
silentcall vimproc#version()
callsetpos('.',getpos("'<"))
lets:exists_vimproc=1
let Mlen ={ s ->strlen(substitute(s,".","x","g"))}
[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)
[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)
```
```vim
### 編集しても:qで警告なしに終了可能なバッファにしたい
4,8call Function()
``` vim
```
:setlocalbuftype=nofile
```
### メッセージに出力する形式での変数の値確認
### get window width
`echom string(xxx)`とすると`echo`のように任意の型に対して利用できる
[Get "usable" window width in vim script \- Stack Overflow](https://stackoverflow.com/questions/26315925/get-usable-window-width-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)
### 行操作
``` vim
### source
setline()
append()
#### 実はURLを指定してファイルを読み込むことができる
" NOTE: 対象がfoldされているとすべて消える
execute'normal! "_dd'
とある環境の内部では`wget`が実行されていた
# 現在の見ている範囲
e.g. `source https://raw.githubusercontent.com/mhinz/vim-galore/master/static/minimal-vimrc.vim`
let topline =line("w0")
let bottomline =line("w$")
#### sourceしたvimファイル中での早期return
let lastline =line("$")
`finish`
```
### 関数内で関数定義可能
#### 現在位置に挿入
``` vim
e.g. [vim-codefmt/yapf.vim at 5ede026bb3582cb3ca18fd4875bec76b98ce9a12 · google/vim-codefmt](https://github.com/google/vim-codefmt/blob/5ede026bb3582cb3ca18fd4875bec76b98ce9a12/autoload/codefmt/yapf.vim#L28)
[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)
e.g. `source https://raw.githubusercontent.com/mhinz/vim-galore/master/static/minimal-vimrc.vim`
[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>`を付加すると意図しない上書きを禁止しやすい
#### sourceしたvimファイル中での早期return
`finish`
> The following characters may be displayed before the {rhs} of the map:
### 関数内で関数定義可能
```
e.g. [vim\-codefmt/yapf\.vim at 5ede026bb3582cb3ca18fd4875bec76b98ce9a12 · google/vim\-codefmt](https://github.com/google/vim-codefmt/blob/5ede026bb3582cb3ca18fd4875bec76b98ce9a12/autoload/codefmt/yapf.vim#L28)
* 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
#### `<Plug>`なキーマップをコマンドから呼びたい
map. The map command has the <script> attribute.
@ A buffer local map command with the <buffer> attribute.
[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) )
*`ctrl-,`,`ctrl-.`
`<sciprt>`を付加すると意図しない上書きを禁止しやすい
*`<S-Space>`
*`<C-S-xxx>`
> The following characters may be displayed before the {rhs} of the map:
通常,使えない [Multiple file types in vim - Stack Overflow](https://stackoverflow.com/questions/2601403/multiple-file-types-in-vim)
#### 文字列の文字を配列の要素とする
e.g. `"abc" -> ['a', 'b', 'c']`
> 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)
`<expr>`指定時に`setline()`を呼んではならない
### 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)
[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)
``` vim
if!exists('g:loaded_hoge')
## リモート
finish
endif
### scpプロトコルでのリモートファイル編集
letg:loaded_hoge=1
```
`:e scp://x.x.x.x//Users/xxx/tmp/README.md`
`autoload`ディレクトリ:`!`なし
*`~`は使えない
``` vim
* リモートのファイル編集時に無理やりsudoして保存する方法は使えないことに注意
ifexists('g:loaded_hoge')
* ホームディレクトリからの相対パスとする場合は`/`を先頭につける必要がない
finish
endif
## Neovim
letg:loaded_hoge=1
```
### Vim scriptでのバージョン確認
### [fatih/vim\-go: Go development plugin for Vim]( https://github.com/fatih/vim-go )
```vim
*`:GoFillStruct`: 構造体のフィールドを自動生成する
:echo has('nvim')
:echo has('nvim-0.1')
### [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`へひも付くので、その行の背景色がすべて上書きされてしまうので、独自に設定を上書きする必要があることに注意
### [fatih/vim-go: Go development plugin for Vim](https://github.com/fatih/vim-go)
```
*`:GoFillStruct`: 構造体のフィールドを自動生成する
:cq[uit]
```
### [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)
## トラブルシューティング
入力文字を利用して、コードの移動を容易にする
### golangの補完が機能しない
[fatih/vim\-go: Go development plugin for Vim](https://github.com/fatih/vim-go)
また、[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`へひも付くので、その行の背景色がすべて上書きされてしまうので、独自に設定を上書きする必要があることに注意