Viml builtin functions: Difference between revisions
From wikinotes
(9 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
< | |||
= Documentation = | |||
</ | <blockquote> | ||
{| class="wikitable" | |||
|- | |||
| <code>:h builtin</code> || https://vimhelp.org/builtin.txt.html#builtin.txt | |||
|- | |||
|} | |||
</blockquote><!-- Documentation --> | |||
= line/cursor = | = line/cursor = | ||
<blockquote> | <blockquote> | ||
get line info | |||
<source lang="vim"> | <source lang="vim"> | ||
line('.') " current line number | line('.') " current line number | ||
Line 11: | Line 18: | ||
getline(10, 20) " get list of lines 10-through-20 | getline(10, 20) " get list of lines 10-through-20 | ||
getline(1)[0] " get char at line-1, col-0 | getline(1)[0] " get char at line-1, col-0 | ||
</source> | |||
change line info | |||
<syntaxhighlight lang="vim"> | |||
setline('.', 'new text') " set current line | setline('.', 'new text') " set current line | ||
setline(10, 15, ['1', '2', '3', '4', '5']) " set lines within buffer | setline(10, 15, ['1', '2', '3', '4', '5']) " set lines within buffer | ||
deletebufline(bufnr(), 10, 15) " delete lines 10-15 in current buffer | |||
appendbufline(bufnr(), 3, 'abc') " append 'abc' after line 3 in current buffer | |||
</syntaxhighlight> | |||
cursor | |||
call | <syntaxhighlight lang="vim"> | ||
call cursor(5, 10) " move cursor to line-5, col-10 | |||
</ | let s:saved_view = winsaveview() " save cursor pos | ||
call winrestview(s:saved_view) " restore cursor pos | |||
</syntaxhighlight> | |||
</blockquote><!-- cursor --> | </blockquote><!-- cursor --> | ||
Line 57: | Line 73: | ||
</source> | </source> | ||
list buffers in vimscript | |||
<syntaxhighlight lang="vim"> | |||
let all_buffers = filter(map(filter(range(1, bufnr('$')), 'bufexists(v:val)'), 'bufname(v:val)'), '!empty(v:val)') | |||
</syntaxhighlight> | |||
</blockquote><!-- buffer --> | </blockquote><!-- buffer --> | ||
Line 72: | Line 93: | ||
<source lang="vim"> | <source lang="vim"> | ||
split('/abc/def/ghi', '/') | split('/abc/def/ghi', '/') | ||
join(["a", "b", "c"], ".") | join(["a", "b", "c"], ".") | ||
substitute(var, '[^a-zA-Z_\\\-]', '', 'g') | substitute(var, '[^a-zA-Z_\\\-]', '', 'g') | ||
trim(" abc ") " strip whitespace | |||
</source> | </source> | ||
</blockquote><!-- string --> | </blockquote><!-- string --> | ||
Line 101: | Line 123: | ||
</source> | </source> | ||
</blockquote><!-- output --> | </blockquote><!-- output --> | ||
= filesystem = | |||
<blockquote> | |||
<source lang="vim"> | |||
filereadable("/foo/bar.txt") | |||
filewritable("/foo/bar.txt") | |||
isdirectory($HOME . "/.vim") | |||
</source> | |||
</blockquote><!-- filesystem --> | |||
= environment = | = environment = |
Latest revision as of 00:21, 10 February 2024
Documentation
:h builtin
https://vimhelp.org/builtin.txt.html#builtin.txt
line/cursor
get line info
line('.') " current line number getline('.') " current line getline(10, 20) " get list of lines 10-through-20 getline(1)[0] " get char at line-1, col-0change line info
setline('.', 'new text') " set current line setline(10, 15, ['1', '2', '3', '4', '5']) " set lines within buffer deletebufline(bufnr(), 10, 15) " delete lines 10-15 in current buffer appendbufline(bufnr(), 3, 'abc') " append 'abc' after line 3 in current buffercursor
call cursor(5, 10) " move cursor to line-5, col-10 let s:saved_view = winsaveview() " save cursor pos call winrestview(s:saved_view) " restore cursor pos
buffer
b mybuffer " open buffer in current window badd mybuffer " create buffer bd mybuffer " delete buffer bufnr('buffername') " get buffer number from name buffers! " list all buffers files! " list all buffers representing filescurrent buffer
map <buffer> q :echom helloworld<CR> " buffer-specific mapping setlocal nonmodifiable " makes buffer read-only setlocal nobuflisted " changes buffer type to 'unlisted' setlocal textwidth=70 " Restrict a buffer's allowed characters line('$') " number of lines in current buffer :0 | exec printf('delete %d', line('$')) " delete all lines in buffertemporary buffer (no warn to save on exit)
badd mybuffer b mybuffer setlocal buftype=nofile setlocal bufhidden=delete " or hide, if preferred setlocal noswapfile bufwinnr('myfile.txt') > 0 " check if buffer exists, and is being viewed in a window bufname('myfile.txt') != '' " check if buffer exists (may be hidden)list buffers in vimscript
let all_buffers = filter(map(filter(range(1, bufnr('$')), 'bufexists(v:val)'), 'bufname(v:val)'), '!empty(v:val)')
window
sp +/searchstring file.txt " open window at line matching searchstring wincmd c " equivalent to <C-w><C-w> exec printf('%d wincmd c', bufwinnr('mybuffer')) " open window with buffer 'mybuffer'
string
split('/abc/def/ghi', '/') join(["a", "b", "c"], ".") substitute(var, '[^a-zA-Z_\\\-]', '', 'g') trim(" abc ") " strip whitespace
execute
execute 'echom "hi"' " execute vim command in string system('ls -la') " execute system command in string pyeval('print("hi")') " execute python command in string
functional
filter(['a', '', 'b'], '!empty(v:val)') " ['a', 'b'] map(['a', 'b'], 'v:val + "---") " ['a---', 'b---'] map(['abc', '123'], "substitute(v:val, '\\w\\(bc\\)\\+', '\\1')") " ['bc', '123'] (regex backslashes must be escaped)
output
messages " print all messages printed within vim
filesystem
filereadable("/foo/bar.txt") filewritable("/foo/bar.txt") isdirectory($HOME . "/.vim")
environment
executable("tmux") " true if 'tmux' executable available