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
33 views

write easy accessable function

I want to run an external command by typing :runMyCommand (just run a shellcommand). I thought I'd put the shell command execution into a function, so I can call it with :call runMyCommand, but I want ...
0
votes
2answers
27 views

How to store state (e.g. variables) between user actions?

The main question is in the title, that is, I have a function, which is invoked by the user and it should 'remember' necessary stuff between invocations, so the next time the user calls it (like, ...
0
votes
1answer
27 views

Vim snippets with arguments

I'm trying to create snippets without using any plugins. For instance, nnoremap ,eq :-1read $HOME/.vim/snips/.eq.tex<CR>o<TAB> inserts the contents of .eq.tex: \begin{equation} \end{...
1
vote
2answers
50 views

Function to remove all trailing whitespace

I have the following function to remove every trailing white space from the document, without changing my cursor position function! StripTrailingWhitespaces() let l = line('.') let c = col('.') ...
0
votes
1answer
26 views

How to pass a parameter to a function called from an on_exit event of an asynchronous task

Imagine the following asynchronous job to convert a given file to odt : let mdfile = 'testfile.md' call jobstart (['bash', '-c', 'pandoc ' . mdfile . ' -s -f markdown -o test.odt'], {'on_exit': '...
1
vote
1answer
12 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
37 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
72 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
34 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
13 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
1answer
25 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
2answers
37 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
4answers
327 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
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(".") "...
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
343 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
99 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
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
15 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 ...
1
vote
1answer
37 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
35 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
27 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 ...
1
vote
3answers
61 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
47 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,...
0
votes
1answer
90 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 <...
1
vote
1answer
82 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 ...
0
votes
2answers
27 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
277 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
60 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
40 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
53 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
46 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 ...
3
votes
1answer
48 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, ...
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
301 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 ...
1
vote
1answer
49 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
82 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
81 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 ...
1
vote
1answer
126 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
46 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 ...
2
votes
2answers
43 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
45 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
23 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
78 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 ...
0
votes
1answer
45 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
65 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):...
1
vote
2answers
288 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 ...