12

Is there any way in my .vimrc file to use the command set list! with a keybind like F3 so it functions like this paste toggle set pastetoggle=<F2>.

Randy Morris
  • 39,631
  • 8
  • 69
  • 76
Max Rahm
  • 684
  • 11
  • 25
  • 1
    See this question for possible answer: http://stackoverflow.com/questions/1773722/how-can-i-toggle-smartcase-in-vim?rq=1 – Alex Feinman Sep 21 '12 at 16:24
  • `pastetoggle` is only an option because of its special use case. It defines a mapping that exists when all mappings are disabled. See the answer above for how to create a map to toggle normal options. – Randy Morris Sep 21 '12 at 16:26
  • I always just do `:se list!` and then `@:` to toggle – sehe Sep 21 '12 at 22:56

3 Answers3

21

You can put this in your .vimrc file:

" F3: Toggle list (display unprintable characters).
nnoremap <F3> :set list!<CR>
Stephane Rouberol
  • 4,286
  • 19
  • 18
  • It works great as long as I'm not in insert mode but in insert mode it prints "" any way to fix that. If not, as it is is still a huge help to me, thanks :) – Max Rahm Sep 21 '12 at 16:33
  • 4
    Although toggling the `'list'` setting is pretty obvious if you where toggling some other setting say, `'spell'` it might be much less so. You can improve upon this mapping by just adding `list?` to the `set` command. This will print out the current value of the `'list'` setting. So in full it would be `nnoremap :set list! list?`. – Peter Rincker Sep 21 '12 at 18:38
3

This is the mapping for normal mode, visual+select mode, and operator-pending mode (e.g. after typing d):

noremap <F3> :set list!<CR>

The nice thing about the function keys (vs. <Leader>) is that they can also be mapped in insert mode:

inoremap <F3> <C-o>:set list!<CR>

To be complete, you could also create a map for command-line mode:

cnoremap <F3> <C-c>:set list!<CR>

Read more about the various mapping modes at :help map-modes

Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
3

I found an answer about how to toggle set number in vim, https://stackoverflow.com/a/762633/1086911

So can try the same way by putting following line into your vimrc file

map <F3> :set list! list? <CR>
Community
  • 1
  • 1
Derrick Zhang
  • 262
  • 4
  • 9