46018

How to Eatchar in a multi-line Vim abbreviation

I would like to trigger a multi line abbreviation in Vim, without entering the 'trigger' character, and with the cursor ending in insert mode at a particular location.

I am nearly there, but just failing to make it.

Thus far, i have added the following to my _vimrc:

" eat characters after abbreviation function! Eatchar(pat) let c = nr2char(getchar(0)) return (c =~ a:pat) ? '' : c endfunction iabbr <silent> if if ()<left><C-R>=Eatchar('\s')<CR> :iabbr <silent> rfF <- function( )<CR>{<CR> <CR>}<Esc>3k$h<Insert><c-r>=Eatchar('\m\s<bar>/')<cr>

Which is mostly successful, in that it yields the following when i type rfF Ctr-] to trigger the abbreviation's expansion:

<- function( ) { }

However, the outcome varies depending on how i trigger the abbreviation.

If i trigger with a <space> the space between the brackets expands:

<- function( ) { }

... and if i <CR>:

<- function( ) { }

I recently asked, and had answered, a question about preventing the characters that trigger an abbreviation from being added in the single line case.

Is this possible with multi-line abbreviations?

Answer1:

This is what I came up with.

" eat characters after abbreviation function! Eatchar(pat) let c = nr2char(getchar(0)) return (c =~ a:pat) ? '' : c endfunction inoreabbr <silent> rfF <- function()<cr>{<cr>}<esc>2k$i<c-r>=Eatchar('\m\s\<bar>\r')<cr>

The regex in this command m\s\<bar>\r will eat any white space or return character. I have also removed the extra space between the { and } because I imagine after you update the function parameter list you will exit back to normal mode then jump down a line with j then execute o to open a new line inside the function block.

Recommend

  • VIM: How do I determine if the current file is on the tagstack?
  • Given a substitution S and list Xs, how to apply S to Xs
  • Unable to Replicate “R for Beginners” Example
  • Passing command line arguments
  • Avoid adding methods and properties to custom object
  • A variable is defined in makefile but Make ignores it. Why?
  • Why is the compiler showing error “expected expression before stud”?
  • Why is the COM interface contract immutable?
  • XGBOOST - DMATRIX
  • d3.js selection conditional rendering
  • Use default value of a column in stored procedures
  • INSERT EXEC Statement cannot be nested [duplicate]
  • Returning the auto incrementing value after an insert using slick
  • Access object instance inside an event handler
  • How can Delete be both a DDL and a DML statement
  • How to read piped content in C?
  • Enabling DTD support in Sql Server
  • SAXReader not re-ecape characters
  • Adding elements to a huge XML file
  • close() was never explicitly called on database
  • Eliminate partial duplicate rows from result set
  • Transactional Create with Validation in ServiceStack Redis Client
  • Disable Enter in editText android
  • PHP - How to update data to MySQL when click a radio button
  • How to check if every primary key value is being referenced as foreign key in another table
  • Join two tables and save into third-sql
  • How to handle AllServersUnavailable Exception
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • python regex in pyparsing
  • Easiest way to encapsulate a HTML5 webpage into an android app?
  • Busy indicator not showing up in wpf window [duplicate]
  • costura.fody for a dll that references another dll
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • How can I use `wmic` in a Windows PE script?
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal
  • How to push additional view controllers onto NavigationController but keep the TabBar?