7

Is there any way to jump to a buffer by entering buffer number right from :ls?

I mean, why this command expect me to Press ENTER or type command to continue, if pressing for example 2, wouldn't do anything?

Obviously I won't any plugin to be installed, at least one little script in .vimrc.

Thanks.

10

Without changing anything the quickest way from that prompt is probably to press :b + number + Enter. But as a bit of a shortcut here's what I (and a lot of other people) use that gets me there from Normal mode:

:nnoremap <leader>ls :ls<cr>:b<space>

You can replace <leader>ls with any unused Normal mode key combo. After pressing the keys you'll see the buffer list and you can enter the number + Enter without dealing with that Press ENTER... prompt.

Update: OP would like to override :ls altogether. That can be done with

:cnoremap ls ls<cr>:b<space>

but this is not recommended since the mapping will happen anywhere "ls" is typed in a command or search term! (Thanks to Peter Rincker for the warning.) While you can overcome this by typing Ctrl+V before typing "ls" anywhere (e.g. to search for "hills" you'd have to do /hil^Vls) that's hardly convenient. Better to choose a key-combo that is unlikely to occur in commands or search terms. In Vim help they have an example with "_ls" so :cnoremap _ls ....

| improve this answer | |
  • Good solution thought. But is it possible to wrap native :ls to :ls<cr>:b<space> (from your example). I mean about to overwrite rather than append new function. Thanks – kAldown Dec 14 '17 at 12:09
  • Sure...see my updated answer. – B Layer Dec 14 '17 at 12:14
  • Be careful with cmap's as they can expand in other command-line "modes" like search, /. – Peter Rincker Dec 14 '17 at 15:41
  • @PeterRincker you were right, now I can not search for :find utils, because it contains ls :D – kAldown Dec 14 '17 at 15:50
  • 1
    Vim help suggests typing Ctrl-V before you type any of the c-mapped chars so :find uti^Vls. Still a pain but I don't yet see any better approach. Perhaps you can map instead :lsx or :lsq and get used to using that instead of :ls. ("lsx" and "lsq" are unlikely to appear in real-life commands or searches.) – B Layer Dec 14 '17 at 16:56
1

You can get a workflow that's close to what you want without configuring vim at all.

You can jump to the nth buffer using <C-^> (ctrl + 6).

If you type a digit while viewing the output of ls, then that digit is stored and can be passed to the next command.

In order to produce the fake vim screenshots, I increased the font size so I can only see a few lines, and set the following listchars.

execute("set listchars=eol:\xc2\xac,tab:>\\ ,trail:\xe2\x81\x9b,extends:>,precedes:<,space:\\ ") 

So, if you do

$ mkdir -p /tmp/vim-example
$ cd /tmp/vim-example
$ vim ./a ./b

Then your screen will look like this:

¬                                 
~                                 
~                                 
~                                 
~                                 
~                                 
./a              0,0-1         All
"./a" [New File] 

You can look at the files by pressing :ls<cr>.

:ls                               
  1 %a   "./a"                    
      line 1                      
  2      "./b"                    
      line 0                      
Press ENTER or type command to con
tinue  

From there, if you type 2 and then <C-^> you will be taken to the next buffer.

¬                                 
~                                 
~                                 
~                                 
~                                 
~                                 
./b              0,0-1         All
"./b" [New File]  
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.