一次删除一个空格的选项卡

发布于 2024-11-03 16:11:38 字数 996 浏览 0 评论 0原文

如何配置 vim,以便一次删除一个选项卡而不是整个选项卡?当我尝试为 if 语句对齐多个测试时,我经常想这样做。在下面的示例中,条件 3 按我想要的方式对齐,而条件 2 由于制表符而偏移了一个空格。

if condition1 or \
    condition2 or \
   condition3:
    do something

我在 OSX 10.6.7 上使用 vim 7.2.108,并且在我的代码文件中有以下模式行:

# ex: tabstop=4 softtabstop=1 shiftwidth=4 expandtab: 

FWIW,此模式行确实允许我使用箭头键一次一个空格地浏览选项卡。

更新:以下是我对 Bram Mooleanar 的示例 vimrc 文件所做的更改:

map z :w<cr>
map q :q!<cr>
map m :make<cr>

set expandtab
set shiftwidth=4
set tabstop=4

set bs=2        " backspace over anything in insert mode
set showmatch       " display the matching bracket of the pair
set nowrap      " don't wrap line to fit window
set showmatch       " show matching paren, bracket, or brace
set ruler       " show current cursor position at bottom
set incsearch       " show next match as you type in search pattern
set ignorecase
set smartcase

syntax on       " enable syntax highlighting

How do I configure vim so that I can delete a tab one space at a time rather than the entire tab? I often want to do this when I'm trying to align multiple tests for an if statement. In the below example, condition3 is aligned as I want and condition2 is off by one space due to the tab.

if condition1 or \
    condition2 or \
   condition3:
    do something

I'm using vim 7.2.108 on OSX 10.6.7 and have the following mode line in my code file:

# ex: tabstop=4 softtabstop=1 shiftwidth=4 expandtab: 

FWIW, this mode line does allow me to use the arrow keys to navigate through a tab one space at a time.

Update: Here's the changes I've made to the example vimrc file from Bram Mooleanar:

map z :w<cr>
map q :q!<cr>
map m :make<cr>

set expandtab
set shiftwidth=4
set tabstop=4

set bs=2        " backspace over anything in insert mode
set showmatch       " display the matching bracket of the pair
set nowrap      " don't wrap line to fit window
set showmatch       " show matching paren, bracket, or brace
set ruler       " show current cursor position at bottom
set incsearch       " show next match as you type in search pattern
set ignorecase
set smartcase

syntax on       " enable syntax highlighting

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

伏妖词 2024-11-10 16:11:38

我想说将其放入您的 .vimrc 文件中:

set expandtab

当您按 Tab 键时,它基本上会将制表符变成空格。

I would say put this in your .vimrc file:

set expandtab

Which basically turns tabs into spaces when you hit the tab key.

煮茶煮酒煮时光 2024-11-10 16:11:38

我只需在正常模式下点击 x 即可删除单个字符。我还将指出伟大的 indent/python.vim script埃里克·麦克斯温著。它并没有真正改变退格行为,但它允许您做一些更符合 PEP8 的事情——使用 Python 的隐式使用括号的行延续。使用脚本,上面的代码将如下所示

if (condition1 or
    condition2 or
    condition3):
    do something

I would just hit x in normal mode to delete a single character. I'll also point out the great indent/python.vim script by Eric McSween. It doesn't really change the backspace behavior, but it allows you to do something more in line with PEP8---using Python's implied line continuation using parentheses. With the script, the code above would look like

if (condition1 or
    condition2 or
    condition3):
    do something
指尖上的星空 2024-11-10 16:11:38

看起来问题在于您使用制表符和空格的混合来管理对齐方式。
在大多数环境中,有很多人处理相同的文件,建议使用空格或仅使用制表符。
也就是说,您可以通过将以下内容添加到 vimrc 来映射一个键来删除制表符并在其位置插入 3 个空格。

map <F1> s   <ESC>

如果你想在插入模式下执行:

imap <F1> <BS>   

*注意“>”后面有三个空格

It looks like the problem is that you are using a mix of tabs and spaces to manage alignment.
In most environments where there are many people working on the same files it is recommended that only spaces or only tabs are used.
That said you could map a key to delete the tab character and insert 3 spaces in its place by adding the following to your vimrc.

map <F1> s   <ESC>

If you want to do it in insert mode:

imap <F1> <BS>   

*note that there are three spaces after the ">"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文