66

The best way is to use has(), with this function you can check for features of Vim; OS specific features from :help feature-list: macunix Macintosh version of Vim, using Unix files (OS-X). unix Unix version of Vim. win32 Win32 version of Vim (MS-Windows 95 and later, 32 or 64 bits) ...


50

Background: I was playing around with Vim 8.2's popups recently (I have a small plugin that allows navigation of sections in a markup document and I was looking at showing section hierarchies in popups). I figured the info I collected would make a decent introduction... Overview Vim 8.2's popup windows allow Vimscript authors and plugin developers to ...


49

You can call the function silently by defining a silent map: noremap <silent> H :call FirstCharOrFirstCol()<cr> For more info, see :h :map-<silent>. Note in particular that this will only ensure that the command is not echoed to screen when the mapping is executed. The :silent command is used to silence output from the function itself, and ...


45

let assigns a value to a variable, and set assigns a value to one of Vim's internal options. For example, you would use :let mystring='Hello!' to declare a new variable, but you would use :set tabstop=4 or :set expandtab to set one of Vim's options. See also: :help let, :help set. g: simply signifies a global variable. There are several of these "...


45

Yes, you can take optional arguments in a function. It is not as convenient as python's way of doing it, but this is how you do it: function FooBar(...) " This is like *args in python echom a:0 " a:0 contains an integer which is the number of arguments passed to the function echom a:1 " a:1 contains the first argument passed, a:2 contains the ...


45

Vimscript use C-like operators && and ||. You can find description of their usage on :h expr2. Some important points mentioned by the doc are the following You'll find that the operators can be concatenated and && takes precedence over ||, so &nu || &list && &shell == "csh" Is equivalent to &nu || (&list ...


42

Late to the party a bit but I didn't see my favorite one: function! FunctionName(arg1,...) let arg2 = get(a:, 1, 0) let arg3 = get(a:, 2, 0) if arg2 "Do stuff with arguments" else "Do stuff without arguments" endif endfunction in which get(a:, n, default) will get the nth optional argument returning default if it's not ...


41

As Jorengarenar said in their answer "{{{2 has to do with foldmarkers. Now, what are foldmarkers anyway? What are they useful for? And how do you use them? First let's notice that the strings you mention in your question are composed of three parts: " the comment character. This is specific to the filetype of your buffer, here it is " because ...


40

You can do this with the system function: let language = system('echo $LANG') Bonus point: if your output is a list, you can use the systemlist instead to get back a list. e.g. let files = systemlist('ls') " ['bin', 'dev', ... ] ref: :h system


39

From :he filename-modifiers: :t Tail of the file name (last component of the name). Must precede any :r or :e. :r Root of the file name (the last extension removed). When there is only an extension (file name that starts with '.', e.g., ".vimrc"), it is not removed. Can be repeated to remove ...


38

has() sounds like a good idea until you try it on Mac OS X: in the default /usr/bin/vim, has('unix') is true but both has('macunix') and has('mac') are false while, in the regular MacVim download, all three are true whether you use the GUI or the TUI. So the best solution is a mix of has('winXX') for Windows and uname on unix-like systems. Note that the ...


33

The execute function takes a string as argument, it expands the string and execute it a a regular ex command. Thus you can do: function! MyFunction(someArg) execute "normal! ". a:someArg. "l" endfunction The . is a standard vimscript operator to concatenate strings. execute can be shortened to exe. See :h :execute EDIT I'll add a point about the ...


28

You can use substitute(), or define a function: function! Chomp(string) return substitute(a:string, '\n\+$', '', '') endfunction This variant will call system for you and then chomp the result: function! ChompedSystem( ... ) return substitute(call('system', a:000), '\n\+$', '', '') endfunction (This function is also available in my ingo-library ...


27

let @z = systemlist('date')[0] removes the newline for you.


27

The only difference between single and double quoted string is related to backslash. To display special characters like newline, bells, tabs, etc, you need to use double-quotes -> "\n". Within a single-quoted string, '\' is itself => '\n' is a two-characters string (a backslash + n). Within double quotes, you have to double it -> "\\", which makes them un-...


26

Remove the trailing <cr> That is only needed for mappings, but not for commands.


26

A "script" does nothing more than run a sequence of ex commands. An "ex command" is what you type when you use : in Vim. For example :wq, :set wrap, :e file, etc. are all ex commands. The : is not part of the command; it is merely a keystroke to start the command-line mode; you don't always need to include the :, for example when you chain multiple commands ...


25

This pattern will let you assign meaningful names to each argument, and provide a default value for any arguments that were not provided: function FunctionName(foo, ...) let bar = a:0 >= 1 ? a:1 : 0 let baz = a:0 >= 2 ? a:2 : 0 ... " Code that makes use of a:foo, bar and baz As pointed out by Boris Brodski: a:0 counts the number of ...


25

You should use getpos(): To save you position in a variable: let save_pos = getpos(".") getpos() takes as argument a mark, here "." represents the current position of your cursor. And to restore it: call setpos('.', save_pos) Here the first argument indicate that you will move the mark of the current position of your cursor (hence your current position)...


25

You need to put another | before else otherwise vim will think your command is set listchars+=space:· else instead of two separate commands. That's why you got the error message: else is an unknown option for the command set. The correct syntax is the following: if has("patch-7.4.710") | set listchars+=space:· | else | set listchars+=trail:· | endif


21

If you were looking for the last point where an option was set (instead of a variable), you could use the :verbose command: :4verbose set is? incsearch Last set from ~/.vim/vimrc But currently there is no equivalent command for variables. You could inspect the code, or use search in the source, possibly using :vimgrep, :vim netrw_list_hide **/* (...


20

Well… it's just as simple in vimscript. Current window Python: current_win = vim.current.window Vimscript: let current_win = winnr() Current buffer Python: current_buff = vim.current.buffer Vimscript: let current_buff = bufnr("%") Current tabpage Python: current_tabpage = vim.current.tabpage Vimscript: let current_tabpage = tabpagenr() See :...


20

There's a lot of way to have fun in Vim: for example, one could install the vim-script nibble plugin (this one has a dependency which also must be installed). Once the plugin is installed the command :Nibble will start a snake game in a new buffer. The snake can be controlled with hjkl and the game can be paused with space. With that it is possible to spend ...


20

Can somebody tell me how to avoid the very annoying vim's slowdown? Hopefully in a user friendly non-geeky way? I run Vim on a company-provided 2013 15" Retina MacBook with a 2.3 GHz i7 processor, 16 GB of RAM, and a measly GT 750M with 2 GB dedicated memory. That machine is very far from a "gaming PC" and I've never experienced any slow down. Ever. Even on ...


20

That's part of the autoload mechanism. From Autoloading at Learn Vimscript the Hard Way and :help autoload: Autoload lets you delay loading code until it's actually needed, so if the following function execute pathogen#infect() has already been loaded, Vim will simply call it normally. Otherwise Vim will look for a file called autoload/pathogen.vim ...


19

Unlike :source, :runtime doesn't need an absolute path to work. It is much better than :source for building portable setups. See :help :runtime. In the following example, we tell Vim to look for a vimrc file in the directories given by the runtimepath variable. Since ~/.vim/ is the first directory in the list, runtime vimrc works just like :source ~/.vim/...


19

You can iterate over buffers if you use :bnext and all. I highly advice against this method. It'll trigger autocommands, and you'll have to remember where you were. In other words, it can be damn slow, and with plenty side effects. Stay away if you can. Until now my preferred approach was to use filter() on range(1, bufnr('$')) to keep the buffers I'm ...


18

You can use the &{option-name} in an if-statement like so: if &guioptions ==# "Trl" echo "Toolbars and scrollbars are present!" elseif &guioptions ==# "" echo "No toolbars and scrollbars present!" endif The & specifies that the variable name is a Vim option. See :help :let-& for the full documentation.


18

To answer your question: the prototype of call() in the manual is call({func}, {arglist} [, {dict}]); the {arglist} argument needs to be literally a List object, not a list of arguments. That is, you have to write it like this: let @x = call(a:functionToExecute, [GetSelectedText()]) This assumes a:functionToExecute is either a Funcref (see :help Funcref), ...


18

To repeat a string you want to use the (appropriately named) function repeat() The doc is here :h repeat() And you can use it like that: let foo = repeat("abc", 3)


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