Questions tagged [functions]

Question about the use of functions in Vimscript defined in the `.vimrc` or a script file.

Filter by
Sorted by
Tagged with
1
vote
1answer
10 views

execute + colon character doesn't work

I want to :retab selection in my vimscript function. When I write execute "normal! :'<,'>retab" nothing happens. If I write: let cmd = "normal! :'<,'>retab" echom cmd The output is ...
2
votes
1answer
23 views

Remapping `v` in netrw/calling NetrwSplit(5) directly

So I've created some helper functions to help work around netrw. They are some keybind simplifications that help with things like window/cursor management when opening files, where to open them, ...
1
vote
2answers
49 views

sourcing .vimrc function does not work

I am using nvim v0.4.3. This is the latest version available in repository. I have an Arch build with the most up to date packages and kernel 5.6.4-arch1-1. I have the below function in my ~/.vimrc: ...
1
vote
0answers
32 views

Is there anyway to check if cursor is on pumvisible suggestion or not?

I wrote the function below, https://github.com/ryuta69/dotfiles/blob/master/dotfiles/.vimrc#L129-L146 fun! DeoEnter() if !pumvisible() return "\n" endif if vsnip#available(1) ...
1
vote
1answer
11 views

Saving and resetting alternate file

I have the following in my vimrc to open pdfs externally with xpdf. function! OpenPdfExternally() silent execute "!xpdf " . shellescape(expand("%:p")) . " &>/dev/null &" edit # ...
1
vote
2answers
29 views

How to create an abbreviation with space

I'm trying to create an abbreviation with the command ab: if I type if ( under INSERT mode, I want some piece of code to be printed immediately. Here is what I've tried: ab if<Space>( if<...
1
vote
1answer
24 views

How to check whether a command is run with range or not?

For example, I have the following code command! -range Test call TestFunc() function! TestFunc() abort " if run with range " get selected content " else " get current word under the ...
1
vote
3answers
37 views

How to loop backwards through getline

I want to code a vim function, which searches backwards a specific char from the cursor at the current line. Here is what I've done: function! CustomSearch() let currentLine = getline(".") "...
1
vote
4answers
296 views

How to delete a pair of parenthesis with backspace

I'm using vim to code C++ and I want to handle the parenthesis more easily. I knew that there was a plugin named Surround, which can help us but it is not what I expected. I set some shortcuts as ...
1
vote
3answers
58 views

Create a mapping for a “smart” Home key

With other text editors, if you press Home, it first takes you to the indent, then all the way to the left. Is this possible with Vim?
0
votes
1answer
15 views

Binding a function which contains the movement command 'b' to 'nmap b'

I am trying to write a function that will only let me call nmap b when the cursor is not on the first column in the current window. This is to make b only work on the current line and encourage more ...
3
votes
2answers
326 views

How can I run a function / action every period of time (once an hour)?

I want to run https://stackoverflow.com/questions/7588531/vim-set-color-theme-based-off-time-of-day So something like: if strftime("%H") < 12 set background=light else set background=dark ...
1
vote
3answers
89 views

Is it possible to create map to call Funcref (including lambda)?

Is it possible to do this: function Foo(func_ref) abort nnoremap <buffer> <cr> :find_a_way_to_call_func_ref endfunction function s:callback() abort echo "123" endfunction command ...
1
vote
1answer
31 views

Nvim external commands to write and delete files inside a function

I use Neovim in a Linux (Arch Linux) environment. I have written a function to help me preview my markdown documents in PDF format without using a whole plugin. I've also added a simple command named "...
1
vote
1answer
14 views

How can I replicate behaviour of go-to command? (42gg, 42G)

I've been searching far and wide around tutorials, :help command, source code, Stacks but maybe I just can't form my query correctly in the vimscript lingo. My goal is to have a short command for ...
1
vote
0answers
12 views

Get/set window line, column without navigating to window

I'm looking for three functions (line, col, cursor) in nvim that takes an extra parameter window number so that I don't need to first navigate to the window and then navigate away. The reason is if I ...
3
votes
1answer
709 views

Toggle fzf.vim if already open

I am trying to write a vim function to toggle the fzf window if it is already open using fzf.vim. The use case is if I am in the :Buffers view and I want to change to the :Files view if I try to ...
1
vote
1answer
32 views

Unexpected behavior with feedkeys

Consider the following code snippet, which aims to provide y without moving the cursor. function! Yank_fixed_cursor() let save_cursor = getcurpos() call feedkeys("y", 'nitx') call setpos('...
0
votes
0answers
26 views

Can't have a proper version of vim with Python

I am on Mint 18.3 Sylvia. I tried to install different versions of vim to have python support, following diverse advices (vim/vim.bacic/vim.gnome/vim.gtk/vim.gtk3/vim compiled from git). I have now ...
0
votes
1answer
89 views

search a pattern preceding cursor position, modify it and insert the modified string

I am writing a mapping to autocomplete tags Pattern: <.*?>, matches complete HTML tags only. The goal is to lookbehind and see if there exists a text like <html>. Mapping: inoremap <...
0
votes
1answer
46 views

Updating buffer content from another buffer

I want to update one buffer content from another buffer periodically, for this I need to delete all lines from the target buffer firstly, then to write new lines to it. The number of lines are varying,...
1
vote
1answer
66 views

Error detected while processing grammarous.vim: E117: Unknown function

I am trying to install the vim-grammarous plugin for Vim 8.0.1453. I have cloned the repo into my .vim/plugin directory. Now every time I open Vim I get an error message which also occurs when I try ...
8
votes
1answer
281 views

vim ZZ lost output with autocommand

I want to check file before quitinq vim with custom functions, everything seems fine with QuitPre autocommand except using ZZ to exit. In this case the output is missing. Consider following function ...
0
votes
2answers
26 views

Appending multiple new lines to a buffer (instead of “^M”)

I'm trying to create a buffer for a specific purpose, and for that, I need to add newlines to it to get it to be the same height as my current window. My first idea, using call append("^", repeat("\r",...
8
votes
1answer
423 views

Detecting whether vim supports functions

I have a function definition in my .vimrc. This .vimrc is sufficient to demonstrate the problem I'm asking about: function! Greet() echo "hello" endfunction I want to be able use the same .vimrc ...
0
votes
2answers
238 views

get string from cursor to end of <CWORD>

In vimL, how to get a string from cursor to end of <cWORD>. I know expand('<cWORD>', but don't known how to keep only right part of cursor. Example given text pre abc-xyz.xxx:123 post, ...
3
votes
1answer
42 views

Insert mode mapping to leave insert mode then invoke a plugin function

In Insert mode, I want a mapping which will return to Normal mode, then call a plugin function. I've tried this: inoremap <a-1> <ESC><Plug>AirlineSelectTab1 It exits Insert mode, ...
3
votes
1answer
57 views

Open command-line-mode from VimScript function

How to reproduce this mapping: nnoremap <Leader>a :DoSomething <C-r>=do_something_arguments()<CR> ..in a function? The desired effect is to open command-line mode with some text ...
0
votes
1answer
39 views

Toggle a setting using shortcut

I have set up Vim to trigger reformatting on save using ALE, but sometimes that doesn't fly very well, and would like to disable it manually. But remembering what the right incantations are is always ...
1
vote
1answer
41 views

How to execute with call and <C-R>/ matching?

I want to count the searching pattern and here is my code: execute '%s/' <C-R>/ '/\=' . Count(submatch(0)) . '/gn' Here is my Count function: function! Count(match) if has_key(b:types, a:...
2
votes
0answers
45 views

Adjust terminal size using a system call from within Vim

All, I have a little function I'm trying to fix. I'd like to change my terminal Vim window size using the ASCII escape sequence \e[8;40;130t so that my writing window size stays the same when I use a ...
0
votes
1answer
49 views

function variable scoping and arg types problems

I'm trying to make a small extension/bug fix to a plugin I rely on (referenced as Plugin in the code snips), therefore I'm constrained by its existing API. I've got the functionality I want somewhat ...
5
votes
1answer
276 views

VimL/Vimscript: how to access local variable of outer function from its inner function?

I posted this question on stackoverflow already, but realized afterwards that it's probably better to ask here. So I have a function of the following form: function! s:my_function(dict_arg) let ...
5
votes
4answers
339 views

How to get all distinct words within a set of lines?

I would like to extract a list of distinct words from a set of lines. Is there a way of doing this ? Say for example I have lines that look like this: [ [(isPhysicallySettledFxFwd, NO,"Y"),(...
1
vote
1answer
180 views

function: sort output alphabetically

This function outputs data between braces as joined lines in a quickfix window. The output is sorted based on line numbers. I'm searching for a method to sort the data alphabetically. I tried to ...
1
vote
1answer
48 views

vim expression unusual behavior

While playing with map() I get an error, which I cannot understand. In essense, pu=map(getcompletion('*','event'), '"h" . v:val') pu=map(getcompletion('*','event'), "'h' . v:val") pu=map(...
1
vote
1answer
81 views

Make mapping that accepts count independent of line count

I have a mapping that looks like this: nnoremap <expr> <c-x>b ":call MyFunction(" . v:count . ")\<cr>" I also tried to instead define function with range, but the effect is the ...
2
votes
1answer
75 views

Error With Simple Function Using setline()

I'm trying to start writing some Vim Script, and I've come across this issue. fu Test() call setline(1, getline(".")) endf autocmd InsertCharPre * call Test() This is in my .vimrc. When I type ...
2
votes
2answers
45 views

Two consecutive identical calls to echo have different output

Question reposted on StackOverflow, where it's receiving more attention. Given the following function in the .vimrc file, fu! MyFun(count) echo a:count echo a:count if a:count > 0 ...
1
vote
1answer
104 views

calling function from normal mode without *noremap

I realise this might be impossible, but thought it might be worth asking anyway. In vim, the typical way I map keys to functions is using *noremap commands. However the downside of this is the fact ...
0
votes
0answers
27 views

How can I complete both words and WORDS? [duplicate]

I found this question, but mine is a bit different. When I write code dealing with arrays or classes like $myArray['myElement'] or document.getElementById("myElement"), I get annoyed because I have ...
2
votes
2answers
38 views

How to ask vim where function or command was defined?

I'd like to know if there is an analogue to verbose map <key> (which shows the location that the map was assigned) for functions. For example I have a few Git plugins and I'd like to know what ...
1
vote
1answer
42 views

vim8 - why does my 0read not read? [closed]

I have the following function in an autoload file: function! closet#FindSkeletonForFile() if exists('b:closet_skeleton') return 0 endif let b:closet_skeleton = s:closet....
1
vote
1answer
21 views

How to get a function to print something to colon command and let user edit it?

I need to write a function that when executed it opens colon command and paste some stuff on to it and let user edit it before executing the command. for example function! EditRelativePath() :e ...
1
vote
1answer
50 views

Vim is executing commands that were part of a function definition

On one copy of Vim that I use sometimes, I get errors like this when I start up: Error detected while processing /home/besham/.vimrc: line 524: E749: empty buffer line 555: E749: empty buffer line ...
1
vote
2answers
77 views

Insert text in the middle of a string vimscript

I'm trying to make a function in vimscript that converts markdown to html files using pandoc and putting that converted file in a specified directory. Currently i am putting the file in a directory ...
1
vote
3answers
128 views

NERDTree maximize on enter

I'd like to maximize NERDTree when I enter the NERDTree buffer. The command I have so far is: autocmd BufEnter NERD_tree_* <A> ...but the <A> part is wrong. I get the error: E21: Cannot ...
0
votes
1answer
44 views

How to keep vim running after kick off an application?

I have below function to compare perforce versions. The function runs fine, only I must quit the version compare software to keep typing words or switch to anther window. Basically gvim stops ...
1
vote
2answers
252 views

Gvim - Change window position horizontally

I want to create a command that will place my gvim window to a different "x" location on my screen while maintaining the "y" position. I do this in my _gvimrc by calling winpos 319 16 I want to ...
1
vote
2answers
58 views

VIM command to insert multiline text with argument

new VIM user. I'm trying to make creating python properties easier for my class definitions. What I would like for say I type :pyp x then VIM will autofill where my cursor is @property def x(self):...