Git Product home page Git Product logo

vim-textobj-quote's Introduction

vim-textobj-quote

Vader Vint

“Extending Vim to better support typographic (‘curly’) quote characters.”

While Vim is renowned for its text manipulation capabilities, it nevertheless retains a bias towards ASCII that stretches back to its vi roots on Unix. This can limit Vim’s appeal for those who prefer typographic characters like “curly quotes” over ASCII "straight quotes" in the prose and documentation they write.

Core features of this plugin:

Includes four additional features:

  • educate - automatic entry of ‘typographic quotes’ from the 'straight quote' keys
  • replace - transform quotes from straight to typographic, and vice versa
  • matchit - % matching for typographic quote pairs
  • surround - surround a word or visual selection with quotes

Requirements

Requires a recent version of Vim compiled with Unicode support.

Installation

Install using Pathogen, Vundle, Neobundle, or your favorite Vim package manager.

This plugin has an essential dependency that you will need to install:

Configuration

Because you won't want typographic quotes in your code, the behavior of this plugin can be configured per file type. For example, to enable typographic quote support in markdown and textile files, place in your .vimrc:

set nocompatible
filetype plugin on       " may already be in your .vimrc

augroup textobj_quote
  autocmd!
  autocmd FileType markdown call textobj#quote#init()
  autocmd FileType textile call textobj#quote#init()
  autocmd FileType text call textobj#quote#init({'educate': 0})
augroup END

The last autocmd statement initializes the plugin for buffers of text file type, but disables the ‘educate’ feature by default. More on that below.

Motion commands

Motion commands on text objects are a powerful feature of Vim.

By default, for motion commands, q denotes an operation on “double” quotes. Q for ‘single’ quotes. For example, with the c change operator:

  • caq - change around “double” quotes - includes quote chars
  • ciq - change inside “double” quotes - excludes quote chars
  • caQ - change around ‘single’ quotes - includes quote chars
  • ciQ - change inside ‘single’ quotes - excludes quote chars

Apart from c for change, you can v for visual selection, d for deletion, y for yanking to clipboard, etc. Note that count isn’t supported at present (due to limitations of the underlying vim-textobj-user) but repeat with . should work.

quote’s motion command is smart too, able to distinguish between an apostrophe and single closing quote, even though both are represented by the same glyph. For example, try out viQ on the following sentence:

‘Really, I’d rather not relive the ’70s,’ said zombie Elvis.

You can change these key mappings from their defaults in your .vimrc:

let g:textobj#quote#doubleMotion = 'q'
let g:textobj#quote#singleMotion = 'Q'

Additional features

The four additional features of this plugin include: educate, matchit, replace, and surround.

Educate

This plugin will ‘educate’ quotes, meaning that while in Insert mode, your straight quote key presses (" or ') will be dynamically transformed into the appropriate typographic quote characters.

For example, entering the following sentence without the educate feature using the straight quote keys:

"It's Dr. Evil. I didn't spend six years in Evil Medical
School to be called 'mister,' thank you very much."

As expected all the quotes are straight ones. But with the educate feature, the straight quotes are transformed into the typographic equivalent as you type:

“It’s Dr. Evil. I didn’t spend six years in Evil Medical
School to be called ‘mister,’ thank you very much.”

You can configure the default settings for the educate feature in your .vimrc:

let g:textobj#quote#educate = 1       " 0=disable, 1=enable (def)

You can change educating behavior with the following commands:

  • Educate
  • NoEducate
  • ToggleEducate

As seen above, educating behavior can be configured as a parameter in the textobj#quote#init() call.

Entering straight quotes

In some cases, straight (ASCII) quotes are needed, such as:

“print "Hello World!"” is a simple program you can write in Python.

To insert a straight quote while educating, enter «Ctrl-V» (mnemonic is verbatim) before the quote key:

  • «Ctrl-V» " - straight double quote
  • «Ctrl-V» ' - straight single quote

Note that for units of measurement you’ll want to use the prime symbol(s) rather than straight quotes, as in:

Standing at 7′3″ (2.21 m), Hasheem Thabeet of the Oklahoma City Thunder
is the tallest player in the NBA.

Matchit support

matchit enables jumping to matching typographic quotes.

  • % - jump to the matching typographic (curly) quote character

You can configure this feature in your .vimrc:

let g:textobj#quote#matchit = 1       " 0=disable, 1=enable (def)

Replace support

You can replace straight quotes in existing text with curly quotes, and visa versa. Add key mappings of your choice to your .vimrc:

map <silent> <leader>qc <Plug>ReplaceWithCurly
map <silent> <leader>qs <Plug>ReplaceWithStraight

Both Normal and Visual modes are supported by this feature. (In Normal mode, quotes in the current paragraph are replaced.)

To transform all quotes in a document, use Visual mode to select the entire document.

Surround support

By default there are no key mappings for surround support.

Basic support

This feature supports basic surround capabilities. Add to your .vimrc key mappings of your choice:

" NOTE: remove these mappings if using the tpope/vim-surround plugin!
map <silent> Sq <Plug>SurroundWithDouble
map <silent> SQ <Plug>SurroundWithSingle

Then you can use ‘motion commands’ to surround text with quotes:

(an asterisk is used to denote the cursor position)

  • visSq - My senten*ce. => “My sentence.”
  • visSQ - My senten*ce. => ‘My sentence.’

Using Tim Pope’s vim-surround

Using Tim Pope’s vim-surround plugin your text object key mappings should be available. For example,

  • cs'q - 'Hello W*orld' => “Hello World”
  • cs"q - "Hello W*orld" => “Hello World”
  • cs(q - (Hello W*orld) => “Hello World”
  • cs(Q - (Hello W*orld) => ‘Hello World’

Entering special characters

Sometimes you must enter special characters (like typographic quotes) manually, such as in a search expression. You can do so through Vim’s digraphs or via your operating system’s keyboard shortcuts.

Glyph Vim Digraph OS X Description
'6 Opt-] left single quotation mark
'9 Shift-Opt-] right single quotation mark
"6 Opt-[ left double quotation mark
"9 Shift-Opt-[ right double quotation mark
.9 single low-9 quote
:9 Shift-Opt-w double low-9 quote
1< Opt-\ left pointing single quotation mark
1> Shift-Opt-\ right pointing single quotation mark
« << Opt-\ left pointing double quotation mark
» >> Shift-Opt-\ right pointing double quotation mark
1' single prime
2' double prime
-N Opt-hyphen en dash
-M Shift-Opt-hyphen em dash
,. Opt-; horizontal ellipsis
  NS non-breaking space
ï i: Opt-U i lowercase i, umlaut
æ ae Opt-' lowercase ae

For example, to enter left double quotation mark , precede the digraph code "6 with Ctrl-K, like

  • «Ctrl-K» "6

Alternatively, if you’re on OS X, you can use Opt-[ to enter this character.

For more details, see:

  • :help digraphs

International support

Many international keyboards feature keys to allow you to input typographic quote characters directly. In such cases, you won’t need to change the behavior of the straight quote keys.

But if you do, you can override the defaults. For example, those users editing most of their prose in German could change those defaults to:

let g:textobj#quote#doubleDefault = '„“'     " „doppel“
let g:textobj#quote#singleDefault = '‚‘'     " ‚einzel‘

Or on a file type initialization...

augroup textobj_quote
  autocmd!
  autocmd FileType markdown call textobj#quote#init({ 'double':'„“', 'single':'‚‘' })
  ...
augroup END

Or in a key mapping...

nnoremap <silent> <leader>qd :call textobj#quote#init({ 'double':'„“', 'single':'‚‘' })<cr>

See also

If you find this plugin useful, check out these others originally by @reedes:

Future development

If you’ve spotted a problem or have an idea on improving this plugin, please post it to the GitHub project issue page.

vim-textobj-quote's People

Contributors

alerque avatar josephw avatar ltratt avatar reedes avatar telemachus avatar tpenguinltg avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

vim-textobj-quote's Issues

Support for count

The underlying vim-textobj-user doesn't support count, so this could be a challenge.

Typo in README

The Installation section refers to a dependency on "kana/vim-textobject-user". It should instead say "kana/vim-textobj-user".

The link is correct; only the text is wrong. But this is still a problem for those who use package managers that take just the repo name, and who therefore just copy and paste it from the README.

vim-textobj-quote + neovim latest is broken

vim-textobj-quote: 32cd093 (latest, I believe)
neovim: v0.2.0-1089-g8d139553 (full build info here)
macOS: 10.12.4

Problem: If I try to use quotes, I get raw escapes mixed with text. For example, if I type It's, I end up with this result: It<t_x30x128><t_x30s>. (Note that the 's' ends up inside the second bit of escape code.)

Please let me know if you need me to do anything else to debug the problem. Thanks.

educate for ellipsis, em/en-dash etc.

Hello!

I'm considering to switch from Emacs to (neo)vim and looking for a decent replacement for Emacs' typoel package which allows me to e.g. enter '...' and get ellipsis. Same with '--'/'---' for en/em-dash along with entering appropriate quotes like 'educate' with this plugin, so wonder if this plugin can help with it or some other hint to have a proper support when writing content (articles, blog posts etc.) using Markdown Extra markup?

Using with `r`eplace

When using r" the " is not replaced with the 'correct' quotation mark
When using xi" it is

This is at least unexpected

Smart quotes in code comments

I know this plugin is designed primarily for prose, but I wanted smart quotes in comments in my code.

I've managed to do this. Currently it looks like this in my .vimrc:

" Smart quotes toggle {{{4
function! s:ToggleEducate()
    if g:textobj#quote#educate
        silent NoEducate
        silent let g:textobj#quote#educate = 0 " For smart quotes in comments
        echom "Smart quotes off"
    else
        silent Educate
        silent let g:textobj#quote#educate = 1 " For smart quotes in comments
        echom "Smart quotes on"
    endif
endfunction
nnoremap <Leader>' :call <SID>ToggleEducate()<Cr>

" Smart quotes in comments {{{4
function! s:SmartQuotesInComments()
    " Respect the setting above, only do smart quotes in comments
    " If the educate variable is truthy
    if g:textobj#quote#educate
        if synIDattr(synID(line('.'),col('.')-1,1),'name') =~? 'comment'
            exec 'silent Educate'
        else
            exec 'silent NoEducate'
        endif
    endif
endfunction
augroup smartquotes
    autocmd!
    autocmd InsertCharPre * if index(textlikeft, &filetype) < 0 |
        \   call <SID>SmartQuotesInComments()
    \ | endif
    autocmd InsertLeave * if index(textlikeft, &filetype) < 0 |
        \   exec 'silent NoEducate'
    \ | endif
augroup END

I've achieved this by loading the plugin for both text-like and non-text-like file types. For text-like file types, Educate is enabled when loading the plugin.

For non-text-like file types (basically, for source code), the plugin is loaded, but Educate is not enabled. However, the configuration variable is still set to 1 (after plugin init). I figured that the plugin only read it on initialization anyway (i.e. changing it after init doesn't change the plugin's behaviour), so I've reused it for the following purpose:

In non-text-like file types:

  • if the configuration variable is 1, a function is run before a character is inserted in insert mode. This function checks to see whether we're currently in a piece of text that has a syntax group whose name contains the string comment (case-insentive). If it does, enable Educate. If it does not, disable Educate.
  • if the configuration variable is 0, don't even check what the current syntax group is, and therefore don't enable Educate
  • always disable Educate if we leave insert mode.

The result is that I have smart quotes in my code's comments automatically. I am also able to toggle that off in case I need ASCII quotes in my comments (e.g. when typing a string in VimL, before the closing double quote is entered, Vim thinks we're in a comment—if I couldn't toggle Educate for comments I'd go mad).

I developed and tested this on an Asus Transformer Book (T100TA), which has a wimpy little Atom processor. I didn't notice any performance impact.

I thought this might be of interest to others. Perhaps you'd like to add this functionality to your plugin, or mention this in the docs. ☺

textobj-quote doesn't work with certain easyclip settings

Not that this is exactly textobj-quote's fault, but if easyclip is configured as follows (which is what the easyclip documentation reccomends):

set clipboard=unnamed

then ReplaceWithCurly and friends don't make any change to the text because pasting no longer uses the " register. It seems that using set clipboard=unnamedplus fixes this problem, although I haven't fully worked out if it has other knock-on effects.

I guess textobj-quote could check for easyclip's existence and alter the paste register it uses; or maybe even documenting the issue might be enough?

adapt educate to &spelllang

A feature request: If Educate is set, it changes quotes according to the current &spelllang. That is, if for example &spelllang = "de", the setting

let g:textobj#quote#doubleDefault = '„“' " „doppel“
let g:textobj#quote#singleDefault = '‚‘' " ‚einzel‘

is used.

textobject for nearest quote

How about a textobject, let's call it u, going to the quotes closest to the current cursor position.

That is, viu selects everything in between any two curly, single, double or any other kind of quotes.

Should be able to turn on/off "educate" interactively

There should be a simple way to turn on and off the "educate" feature on the fly, interactively, globally or in the current buffer.

The documented let g:textobj#quote#educate=0 doesn't do it, which I take to be either a bug or not intended to work in an already-loaded buffer.

scriptencoding utf-8 not allowed in sandbox

When starting vim with the file which activates call textobj#quote#init (actually, neovim from git (commit neovim/neovim@e46534b42 ) I get this error:

Error detected while processing \
    /home/matej/.vim/pack/minpac/start/vim-textobj-quote/autoload/textobj/quote.vim:                                                                      
line    9:                                                                                 
E48: Not allowed in sandbox: scriptencoding utf-8

I don't know if it matters, we could use "\u02a4" instead of switching scriptencoding.

After that something is wrong, e.g., :w says E13: File exists (add ! to override) , I have to run :w! to save the file.

Educate just doesn’t switch on automatically

No matter whether I rely on default (which is supposed to be that educate is on), or whether I add 'educate': 1 explicitly to the configuration of the plugin), it is always off, and I have to run command :Edcuate every time I start new instance of vim (neovim from the master branch of the git, or vim 8.2.0348, but I have observed it for some time already).

I don’t see any error messages anywhere.

Not working in Ruby file

Nothing happens when I viq or viQ inside a quoted string.
vim-textobj-user and vim-textobj-quote are installed.
I'm using NeoVim v0.6.1.

My config:

Plug 'kana/vim-textobj-user'
Plug 'preservim/vim-textobj-quote'
" ...

augroup textobj_quote
  autocmd!
  autocmd FileType ruby call textobj#quote#init({'educate': 0})
augroup END

Why can't I change surrounding pairs from curly quotes to other?

I use vim-surround for manipulating matching pairs of quotes and other syntax. Adding vim-textobj-quote to the mix works great for getting motion commands for curly quoted strings. It also works out of the box to add a surround operator for curly quotes. Changing other surround symbols to curly quotes even works: using "this‸ string", typing cs"q will produce “this‸ string”. The issue I have is that it doesn't work the other direction: starting with “this‸ string” and typing csq" does nothing.

What's going on here? Is this a bug or do I have something setup wrong?

Instructions for getting vim to recognize ’ in spell check

Hi, I've noticed that Vim's native spell check doesn't recognize the in words. This sucks.

I've researched several approaches for getting it to recognize it: some included using plugins to use different spell check engines. I thought that was overkill.

So, it turns out there's a better way, create a custom spelling file with the correct entries and get Vim to use it:

  1. Open Vim, with spell check enabled
  2. Run :spelldump. Remove any comment lines (lines starting with #)
  3. Save this file somewhere, e.g. ~/spelldump.txt
  4. Replace all the ' characters with characters, e.g. by running grep "'" ~/spelldump.txt | sed "s/'/’/g" >> ~/spelldump_smartquotes.txt
  5. Place this file in a location Vim expects, like ~/.vim/spell/en.utf-8.add or ~/.config/nvim/spell/en.utf-8.add (NeoVim). If that file already exists, append to it instead of overwriting it, but the first line (the region line) ought to be at the top of the file, instead of appended at the bottom. If there's already a region line at the top and it's different to the one in the new file, consult the Vim spelling docs to understand the spell format and make the appropriate changes.
  6. Start Vim, run :mkspell! ~/.vim/spell/en.utf-8.add
  7. Restart Vim

Now Vim should recognize the closing single quote mark in words. Phew.

Perhaps you'd like to add this to the docs.

Support subscript curly quotes

This plugin is based on US locale, where the opening quotes are in superscript, e.g., “Hello there”. Other locales, e.g., German or Polish, use subscript quotes: „Hello there”.

Could you add support for subscript curly quotes to this plugin? Probably making them interchangeable with superscript ones should be enough.

ReplaceWithCurly replaces all quotes with right curly

After doing map <silent> <leader>qc <Plug>ReplaceWithCurly per the README, I tried \qc. Instead of replacing pairs of straight quotes with matching curlies, it instead replaced all straight quotes with right curly quotes.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.