21

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


11

You can use getchar() and search() to accomplish your goal. nnoremap <key> :call search('\<' . nr2char(getchar()), 'W', line('.'))<cr> The idea is we use nr2char(getchar()) to wait for a character to be typed and use search() to search for the next instance of the character at the beginning of a word via \<. We limit this to the current ...


11

I you only need to know if it is a built-in command, you can use exists(':yourstring') See :h exists() for more details.


7

The problem is that gg, = and G are normal mode commands, as opposed to ex commands which are used within a script. Writing normal gg=G should solve your problem. As @Carpetsmoker has pointed out in his comment, using normal! instead of normal might be prudent to avoid running user-defined mappings by accident. See also :help :normal.


7

You can use a mapping with the <expr> flag to achieve this. Mappings with the <expr> flag will evaluate the right hand side of the mapping as an expression and then apply the result as key strokes. This can be combined with the input() function to achieve what you want. Here is your mapping implemented with these features: nnoremap <expr&...


7

Is it transcoded down to C and compiled? Vimscript is interpreted, and in fact you can do basically the same things in vimscript as you could at the : command (and the other way around), plus or minus some bar and escape characters to make things sane. The evaluation code is mainly located in eval.c, which you can browse if you are brave. Is it put on ...


7

I can recommend Learn Vimscript the hard way from Steve Losh. It is a good tutorial which gives you most of the relevant knowledge and links you to :help when it makes sense.


7

Edit : Peter Rincker's answer is shorter, easier to explain, and can be repeated as many times as you want. My solution is too long, and can't be repeated for several words. I should delete the answer but maybe someone will be interested in it, so I let it here. I'm not sure this is what you want, but try the following code : function! NextWordWithLetter(...


7

In vim, a function which does not explicitly return a value actually returns 0. So, your code is equivalent to exe 0, which is like :0 meaning move the cursor to the first line.


6

You can use the search() function. Something like this should work: if search('\e\[\d\+;\d\+m', 'nW')>0


6

Here's what I came up with: Spell Rotate Features The '[ and '] marks are used to keep track of the text being worked on. Making a change elsewhere will effectively "accept" the suggested change. Accepts a count. Goes backwards using zp Repeatable using vim-repeat. Undo once to restore the original word regardless of how many suggestions have been cycled....


6

Looking at :h map: map({expr1}, {expr2}) map() {expr1} must be a List or a Dictionary. Replace each item in {expr1} with the result of evaluating {expr2}. {expr2} must be a string or Funcref. ... If {expr2} is a Funcref it is called with two ...


6

The :map command takes :help key-notation, but not variables. Vimscript is evaluated exactly like the Ex commands typed in the : command-line. There were no variables in ex, so there's no way to specify them. When typing a command interactively, you'd probably use <C-R>= to insert variable contents: :sleep <C-R>=timetowait<CR>m<CR> ....


6

Nice idea! Here's a basic function that achieves your goal using a different technique: function! FitOnPage() normal! zR while &foldlevel > 0 if line('w0') == 1 && line('w$') == line('$') break endif normal! zm endwhile endfunction It works by first expanding all the folds. It then closes folds (globally) until the ...


6

Discl.: It's just guessing on my part. abort didn't exist when function was introduced. I guess that in order to not break existing code it was decided to not change the current behaviour and to provide another behaviour only when functions are explicitly annotated. Now, where should we not use it? I see no good reason. Even if a non annotated function ...


6

If you don't want to use systemlist for whatever reason, you can explicitly remove the newline and/or whitespace. Since vim 8.0.1630 (very recent), there is a trim() function which removes whitespace, including newline, from the front and the back of a string. trim(system('command')) A common way, if you know there will always be a trailing newline is: ...


6

TL;DR: Yes, but you probably shouldn't (in general) Other answers Christian's answer offers two approaches that modify myplugin.vim in order to expose the script-local function (either as a Funcref or just the SID that allows you to obtain a Funcref). Having to extend a plugin just for testing purposes is not nice, and as I understand, you're reluctant to ...


6

To create a custom command line command :command is a good choice: :command! -nargs=1 SL g/<args>/z#.1 You'll need to use a name that starts with a capital letter, though, so I'm using "SL" instead of "sl". Run with :SL pattern. The rest is pretty self-explanatory. Use a bang (!) after :command to allow subsequent overrides (good for including it in ...


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 ...


5

GitLocations won't work as expected, since you'll be calling Vim's :find, not the find command. You might try something like: return system('find $HOME -name ".git" -printf "%h\n"') And you could add a -type d to it to skip submodules. To get visually selected text, you can use getline(): let s:msg = join(getline("'<", "'>"), "\n") . "\n" However,...


5

To get the count given to <C-E>, you don't need to use the range to do it. There are two pre-defined variables you can use, v:count and v:count1, which contain the count given to the last normal mode command. The former should work for you, as it will be zero when no count has been given. The latter defaults to 1 in same situation. Here is a minimal ...


5

When wondering about the standard, good way of writing something in VimL I usually check two main resources: the Vim documentation, which is extremely comprehensive and well written, and Steve Losh's blogs (Learn Vimscript the Hard Way and Writing Vim Plugins). One approach to find examples in the Vim documentation is finding the entry on the subject and ...


5

If you really want to avoid the registers, you can use the visual marks to figure out byte positions of the selected text: function! s:pos2byte(pos) abort return line2byte(a:pos[1]) + a:pos[2] endfunction function! s:get_visual_text() abort let b1 = s:pos2byte(getpos("'<")) let b2 = s:pos2byte(getpos("'>")) let text = '' while b1 ...


5

As @statox suggested, you can use the plugin I wrote: vimcorrect. I'll explain basically how it works, so if you want to reuse some part of it, you can. To focus on the next misspelled word I use directly ]s and [s as they jump to the next/previous match. I defined a custom match function to highlight the current word: matchadd('error', '\%'.line('.').'l'....


5

The solution is described in the manual: If you just want a highlighted message use :echohl. And to get a beep: :exe "normal \<Esc>" Thus: function! PrintError(msg) abort execute 'normal! \<Esc>' echohl ErrorMsg echomsg a:msg echohl None endfunction


5

Sure, you can use the FileType autocmd. " Set the default behavior nnoremap <Leader>T :!vendor/bin/phpunit --filter " Now set it if we open a go file autocmd FileType go nnoremap<buffer> <Leader>T :call GoFunction() You can, of course, change go to whatever filetype you want. If you're not sure what filetype to use, open the file that ...


5

You probably want to use :h systemlist(): systemlist({expr} [, {input}]) *systemlist()* Same as system(), but returns a List with lines (parts of output separated by NL) with NULs transformed into NLs. This is usally a good way to handle system call without having to handle the new line characters.


5

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 ...


4

You defined the function s:A, but tried to call function('A') - they are not the same: function A() doesn't exist. (:h E700 points directly to the function() call) You'll probably have to use '<SID>A' in place of 'A' as key (same for B, of course). Also, :h call() (with braces; not call) might help with the handling of the function arguments.


4

After running vimtutor to get a handle of use of Vim (I'm surprised at how few people know the fundamentals) then yeah, :help is really the way to go. The documentation is broken into fairly logical sections so if you're looking to script Vim in interesting ways the document that'll be your primary reference is eval.txt, :h eval. That goes over syntax and ...


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