Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim smart insert semicolon

Tags:

Is there a Vim plugin that can handle smart semicolon insertion, like the one in Eclipse?

Example (pipe character is insertion cursor):

foobar(|) 

I type a semicolon:

foobar();| 

Similarly:

foobar("blah|") 

I type a semicolon:

foobar("blah");| 

If I want a semicolon at the original cursor position, I press backspace after the smart re-position:

foobar("hello|") foobar("hello");| foobar("hello;|") 
like image 483
dteoh Avatar asked Aug 25 '11 08:08

dteoh


People also ask

How do I add a semicolon in Vim?

command.” A; add the semicolon at end of line, so we can run: :'<,'>normal A; to add semicolon at endline of selected. :%normal A; to add semicolon at endline of entire file.

What does semi colon do in Vim?

To avoid the extra 'shift' keypress when typing the colon to go to cmdline mode, I mapped the semicolon key to do the same thing. This overwrites the original mapping of repeating the last f or t command.

How do you put a semicolon at the end of each line?

Press ctrl-; or ctrl+, to add/remove a semicolon at the end of the selected lines.


2 Answers

I use this mapping:

inoremap <leader>; <C-o>A; 

It's not ; because I use semicolons often and in more than one context.

  • <C-o> is used to input a single normal mode command.
  • A; means "add a ; at the end of the line".
like image 75
romainl Avatar answered Sep 20 '22 15:09

romainl


I want to do the same thing, I works on it whole night, tons of code, but at last, I got a simple solution.

inoremap ;<cr> <end>;<cr> 

and if I am in a brace, I want to add a semicolon or a dot at the end, like this

foo({   | }) 

I press ;;<cr> or ..<cr> to do this

inoremap ;<cr> <end>;<cr> inoremap .<cr> <end>. inoremap ;;<cr> <down><end>;<cr> inoremap ..<cr> <down><end>. 
like image 34
guilin 桂林 Avatar answered Sep 19 '22 15:09

guilin 桂林