15

The problem is that "tiny" is missing the +eval feature and that's what prevents it from seeing your function definitions. So you could try something like: if has('eval') function! Greet() echo "hello" endfunction endif But it turns out you can go even simpler than that, as :help no-eval-feature suggests: When the +eval feature was ...


7

I though it wasn't possible, but I was wrong. See :h :func-closure You need to patch two things: add closure at the end of the inner of the function declaration respect the usual naming conventions For instance function! s:my_function(dict_arg) let darg = copy(a:dict_arg) func! s:my_inner_func(cond) closure return darg[a:cond] endfunc ...


6

This can be done with either regex+substitute or macros Substitute. This is the same as your regex except the important parts are surrounded by \( . \) to create capture groups. These are referred to by submatch(1) and submatch(2) respectively. We use the replace expression \= and execute('let') idiom. Finally, use /n to prevent substitution from ...


6

Let's get this confusion out of the way first: Are these ex commands or Vimscript? Vim scripts are made of ex commands. From :help script: Your first experience with Vim scripts is the vimrc file. Vim reads it when it starts up and executes the commands. You can set options to values you prefer. And you can use any colon command in it (commands that ...


6

The answer is actually not as complicated as you might think. A trick that I have found very useful in situations similar to this is to remove the <cr> from the end of your mapping and see what comes up. If I do that and run 30<C-x>b, this pops up in the command line: :.,.+29call MyFunction(30) This is because it's basically like you typed 30:...


5

From :h insertcharpre: It is not allowed to change the text |textlock|. You need to delay the change with a one-shot autocmd, for example until the next TextChangedI is fired: fu! Test() abort au TextChangedI * ++once call setline(1, getline('.')) endfu augroup test au! au InsertCharPre * call Test() augroup END The ++once flag is provided ...


4

From :h :function-verbose: When 'verbose' is non-zero, listing a function will also display where it was last defined. Example: > :verbose function SetFileTypeSH function SetFileTypeSH(name) Last set from /usr/share/vim/vim-7.0/filetype.vim So :verbose function GitFiles should do the work.


4

Method 1.1 " from start of cWORD to cursor echo matchstr(getline('.'), '\S*\%'. col('.') .'c.') " from cursor to end of cWORD echo matchstr(getline('.'), '\%'. col('.') .'c.\S*')


4

One option is to take the current line, then the substring starting at the current position, split it into multiple words and take the first one. Or, in Vimscript: let word = split(getline('.')[col('.')-1:])[0] One alternative is to use a normal mode command such as yW and then access the contents of the default register (or, better, use a named register.) ...


4

You can do it with several substitutions. " bookmark current position normal! m` " remove whitespace on the current line silent! keepj s/\%#\s\+$// " same for lines above silent! keepj 1,- s/\s\+$// " same for lines below silent! keepj ''+,$ s/\s\+$// " restore cursor position normal! g``


3

This happens because the <Plug> command is itself a mapping defined by a plug-in, so you need recursive mappings in this case for it to work. imap <a-1> <ESC><Plug>AirlineSelectTab1 The usual recommendation is to always use the nore (non-recursive) version of map commands, to prevent interference from mappings created by the user or ...


3

From what I can tell without seeing its implementation, Plugin#Keymap#Register expects a string referring to a globally accessible function, which limits your options. A closure with a single name won't quite work because its contents would keep changing. One option is to create a dynamically-named function for each value of outer key. function! s:...


3

Try this: :call feedkeys(':e ' . expand('%')) See :help feedkeys() for more details.


3

Your version of vim was compiled without eval support, and I suspect that it therefore does not support functions. Vim would then ignore the function statement and likely tries to execute the bodies. From :h eval: Note: Expression evaluation can be disabled at compile time. If this has been done, the features in this document are not available. See |+...


3

Essentially, the answer is no, :call is the mechanism for invoking functions starting from normal mode, but there are at least two mechanisms I can think of that you could theoretically use if you needed to call a function from a normal-mode mapping but the colon key had been stolen from your keyboard: Expression mappings Expression mappings work by ...


3

What you are seeing here, is essentially the same problem, as why many people struggle with using variables on the command line with the ex commands. See e.g. this question, there are a lot similar ones. Basically, Vims ex commandline commands expect their arguments to be literal, they cannot contain expressions (like variables or strings). Those commands ...


3

It's really doable with execute... and call() function Foo(func_ref) abort execute printf('nnoremap <buffer><cr> :call call(%s, [])<CR>', \ string(get(a:func_ref, 'name'))) endfunction


3

As a workaround one can instead target the common FocusLost / FocusGained events. function DynamicColorThemeSwitch() if strftime("%H") < 18 set background=light colorscheme zellner else set background=dark colorscheme nightfly endif endfunction autocmd FocusLost,FocusGained * call DynamicColorThemeSwitch() call ...


3

You can use timer_start() to set "timers" to run a function every n milliseconds. For example: fun! s:set_bg(timer_id) let &background = (strftime('%H') < 12 ? 'light' : 'dark') endfun call timer_start(1000 * 60, function('s:set_bg'), {'repeat': -1}) call s:set_bg(0) " Run on startup This will run s:set_bg() every 1 minute (60,000 milliseconds),...


3

A range with commands is always about lines. It seems you want to work with line/column positions. Whether a range was given can be checked by using <range>: command! -range Test call TestFunc(<range>,<line1>,<line2>) function! TestFunc(r,l1,l2) abort if a:r == 0 echo "No range" elseif a:r == 1 echo "Single ...


3

First, <cr> in the mapping validates the command -- as we do interactively in the command/ex mode. In a script/function, we shall not add <cr> to commands -- each line validates a command. TL;DR: It should be source ~/.vimrc in the .vimrc Then, as the error message says: we cannot redefine a function while it's being executed. IOW, a function ...


2

Using 0read on BufNewFile actually works as expected, in fact the documentation for BufNewFile explicitly mentions using that event for loading a template or skeleton file. If we also rule out the other things that might have gone wrong there, then the most likely explanation is that 0read is working, but the contents of the buffer are later erased by ...


2

The problem is all about screen redraw (see :h echo-redraw) in Vim. Changing echo to echom still produces the same (broken) screen output (3 0 2 0 1 0 0 0), but :mess reveals what is hidden: 3 3 0 0 2 2 0 0 1 1 0 0 0 0.


2

I don't know why it happens, but if you add a \n after the echo it all works and the result is what you would expect: fu! MyFun(count) echo a:count . "\n" echo a:count . "\n" if a:count > 0 normal , call MyFun(a:count - 1) endif endf But maybe you already knew that and was just curious to know why (as I confess I am).


2

TL;DR: You can use this auto-command to maximize the width of your NERDTree window when opened: autocmd FileType nerdtree let b:NERDTreeZoomed = 1 | wincmd | Alternatively, you can consider increasing the width of the window by setting the g:NERDTreeWinSize variable to a higher value (defaults to 30), you can set it to something very high to get the same ...


2

Rather than take a direct approach to "inserting text in the middle of a string" I'd suggest something a bit more robust (and a bit more complex). It allows you to, say, change the root directory path or the converted file directory name in a straightforward way if you need to in the future. With a straight string manipulation you'd have to redo the logic. ...


2

the system I am working on, its very very difficult if not impossible to get pluggins so I have to do stuff through my vimrc or other method. Basically trying to get a plugin installed could take half a year. Please note that you don't need to install plug-ins system wide and they can live completely inside your home directory (under ~/.vim more exactly. A ...


2

Define a list of all the lines you want to insert, and then insert them with either append() or setline(). You'll also need to extract the current indentation. See: :h List :h setline() :h append() :h indent() PS: half a year is almost the time we spent on validating our snippet plugins...


2

Use :execute to assemble a command from a string, which allows you to include the contents of a variable or return value from a function. This should work: function! WinRight() let ypos=getwinposy() execute "winpos 1241 ".ypos endfunction Or even: function! WinRight() execute "winpos 1241 ".getwinposy() endfunction


2

[While B Layer is right that this question should demonstrate attempts (in order that answers address conceptual misunderstandings), the answer is at least straightforward.] To toggle a "boolean" variable: let x = !x So the mapping is simply nnoremap <your keys> :let g:ale_fix_on_save = !g:ale_fix_on_save<CR>


Only top voted, non community-wiki answers of a minimum length are eligible