You are not logged in.
Hello,
Recently I started to work more extensively with vim. So far so good but according to my used workflow I experience the following open questions.
1. Indentation / solved by 'set autoindent' and CTRL + D
Let's assume the following code.
def function():
1 if ....
2 ....
3When I'm in line 2 and return to line 3 I'm on the same indentation level than line 2. This is nice. However, how to I quickly go back to the indentation level of line 1? I do not want to press backspace four times. Is the correct way to go back to normal mode and then press < ?
2. Change word / solved with ct command
Let's assume the following expression: "abcd_efgh"
2.1 How do I tell vim that I want to change/replace abcd?
2.2 How do I tell vim that I want to change abcd_efgh?
I tried to work with 'ce' or 'cw' but they do not pay attention to the underscore and replace everything between the "".
Eventually my current vimrc.
" An example for a vimrc file.
"
" Maintainer: Bram Moolenaar <Bram@vim.org>
" Last change: 2008 Dec 17
"
" To use it, copy it to
" for Unix and OS/2: ~/.vimrc
" for Amiga: s:.vimrc
" for MS-DOS and Win32: $VIM\_vimrc
" for OpenVMS: sys$login:.vimrc
" When started as "evim", evim.vim will already have done these settings.
if v:progname =~? "evim"
finish
endif
" Use Vim settings, rather than Vi settings (much better!).
" This must be first, because it changes other options as a side effect.
set nocompatible
" allow backspacing over everything in insert mode
set backspace=indent,eol,start
if has("vms")
set nobackup " do not keep a backup file, use versions instead
else
set backup " keep a backup file
endif
set history=50 " keep 50 lines of command line history
set ruler " show the cursor position all the time
set showcmd " display incomplete commands
set incsearch " do incremental searching
" For Win32 GUI: remove 't' flag from 'guioptions': no tearoff menu entries
" let &guioptions = substitute(&guioptions, "t", "", "g")
" Don't use Ex mode, use Q for formatting
map Q gq
" CTRL-U in insert mode deletes a lot. Use CTRL-G u to first break undo,
" so that you can undo CTRL-U after inserting a line break.
inoremap <C-U> <C-G>u<C-U>
" In many terminal emulators the mouse works just fine, thus enable it.
if has('mouse')
set mouse=a
endif
" Switch syntax highlighting on, when the terminal has colors
" Also switch on highlighting the last used search pattern.
if &t_Co > 2 || has("gui_running")
syntax on
set hlsearch
endif
" Only do this part when compiled with support for autocommands.
if has("autocmd")
" Enable file type detection.
" Use the default filetype settings, so that mail gets 'tw' set to 72,
" 'cindent' is on in C files, etc.
" Also load indent files, to automatically do language-dependent indenting.
filetype plugin indent on
" Put these in an autocmd group, so that we can delete them easily.
augroup vimrcEx
au!
" For all text files set 'textwidth' to 78 characters.
autocmd FileType text setlocal textwidth=78
" When editing a file, always jump to the last known cursor position.
" Don't do it when the position is invalid or when inside an event handler
" (happens when dropping a file on gvim).
" Also don't do it when the mark is in the first line, that is the default
" position when opening a file.
autocmd BufReadPost *
\ if line("'\"") > 1 && line("'\"") <= line("$") |
\ exe "normal! g`\"" |
\ endif
augroup END
else
set autoindent " always set autoindenting on
endif " has("autocmd")
" Convenient command to see the difference between the current buffer and the
" file it was loaded from, thus the changes you made.
" Only define it when not defined already.
if !exists(":DiffOrig")
command DiffOrig vert new | set bt=nofile | r # | 0d_ | diffthis
\ | wincmd p | diffthis
endif
" Further customisation
" Robert Orzanna <orschiro@googlemail.com>
" Show line numbers
set number
" Set a GUI font that supports unicode characters
set guifont=Courier_New:h11:cANSI
" Set spaces instead of tabs
set tabstop=4
set shiftwidth=4
set expandtab
" Set colour scheme koehler
colorscheme koehler
" Open in maximised window on Windows
au GUIEnter * simalt ~x
" Set backup dir for ~ files
set backupdir=~/vim/backup,/tmp
" Session plugin related settings
" Automatically save the opened tabs and windows on Vim leave
:let g:session_autosave = 'yes'
" Do not prompt for restoring the last opened session
:let g:session_autoload = 'no'Thank you in advance!
Last edited by orschiro (2012-03-28 16:12:40)
Offline
I have little input for number one. I use tabs for indent rather than spaces, so I'd only have to hit backspace once to go back a level.
For two, you can either change the settings for what vim recognizes as word boundaries, or more likely what you might want is ct_. c-t is for change-(up-)to the next character, so "ct_" changes everything up to the next underscore.
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
For the indentation, in your .vimrc, try:
set backspace=indent,eol,startThis will allow you to backspace over indentations and linebreaks.
To change text, the trick is to give the command 'c' (for change) and then a direction command. For instance, c5h will delete 5 characters to the left of the cursor and place you in insert mode. For the instance you gave, with my cursor on 'a', I would give the command string 'ct_' ('c' then 't' then 'underscore').
To substitute one string for another, you could use an 'ed' command (a command that starts with a ':', command mode)
:s/abcd/1234/ Substitute '1234' for the first instance of 'abcd' in the current line.
:s/abcd/1234/g Substitute '1234' for all instances of 'abcd' in the current line. 'g' for global
:%s/abcd/1234/g Substitute '1234' for all instances of 'abcd' in all lines in the document. '%' means all lines.
:5,9s/abcd/1234/gi Substitute '1234' for all instances of 'abcd' in lines 5 through 9 in the document and ask for confirmation for each replacement. 'i' stands for interactive.
Sometimes, I almost think there are too many ways to accomplish a task in vim.
Edited to include a different example
Last edited by thisoldman (2012-03-12 16:41:28)
Offline
Hello thisoldman,
I got the 'ct' command already. But thanks anyway.
Still unsolved is the other problem where I came up with a new idea.
The same way I use Tab to go one indentation level to the right I want to use Shift + Tab to go back one indentation level to the left. Is that possible?
In my case one indentation level consists of 4 spaces.
Regards
Offline
There's probably a better way to do this but
:map! <S-Tab> <BS><BS><BS><BS>Will enable it in both command and insert modes. You can (should?) write the BS's out as Backspace if you like.
Note that if you use it on a file that has tabs instead of spaces, you could get some unwanted behaviour.
"...one cannot be angry when one looks at a penguin." - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle
Offline
I just tested my fairly vanilla vim on this computer and mine does what you want. If I start typing, hit enter, spacex4, type, enter (now indented 4 space) if I hit backspace it goes back a4 spaces. I checked my vimrc, the only possible setting is autoindent, which according to the help file should do what you want.
I then checked your vimrc. You have this
set autoindent " always set autoindenting onbut it is in the "else" block of an if statement, so it will not always set autoindenting on, only when the conditional is false.
Move that outside the else block (delete the else block entirely) and you should be set.
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
I just tested my fairly vanilla vim on this computer and mine does what you want. If I start typing, hit enter, spacex4, type, enter (now indented 4 space) if I hit backspace it goes back a4 spaces. I checked my vimrc, the only possible setting is autoindent, which according to the help file should do what you want.
I then checked your vimrc. You have this
set autoindent " always set autoindenting onbut it is in the "else" block of an if statement, so it will not always set autoindenting on, only when the conditional is false.
Move that outside the else block (delete the else block entirely) and you should be set.
Good catch, it's in the else of the autocmd check, and vim on Arch is compiled with that enabled,so it will never get set.
"...one cannot be angry when one looks at a penguin." - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle
Offline
I looked into this last night, and here's some relevant key combos:
Ctrl+T add one level of indentation
Ctrl+D back on level of indentation
^ Ctrl+D = temporarily take cursor to beginning of line. Next line will continue at current indentation.
0 Ctrl+D = take cursor to beginning of line and reset indentation to zero.
The latter two are the character ^ and 0 followed by Ctrl+D. In input mode, you will see the character before you hit Ctrl+D but it will then vanish.
"...one cannot be angry when one looks at a penguin." - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle
Offline
if you want to change indentation on more than 1 line you you mark the lines in
visual mode (shift+v) and press > and they will go on step to the right. If you want to
more them long just keep hitting . until your satisfied. < works in the opposite direction.
A tiling window manager simply manages your windows, not just letting you have em
:: Configs etc @GitHub
Offline
@skanky's methods of changing indentation while in Insert mode are new to me. They'll save me some aggravation. Thanks.
For some reason I don't like switching to Visual mode but I never found an alternate (better?) method to change the indentation of blocks of text. So I used the method greyscale gives in his post above. Until today.
As usual vim has more ways to accomplish a task than I can hope to remember – see the section "SHIFTING LINES LEFT OR RIGHT", lines 404 through 476, in '/usr/share/vim/vim73/doc/change.txt'.
Offline
The count before << >> is standard and the idea of putting a count before any command should be obvious (there are some commands where it makes little sense, but as far as I know they are few and far between). The :range << >> etc. ones are ex search commands, and can be useful to learn. If you know the generic form of ex searches then you'll find many vi commands can be tagged on. I am still in the early stages of learning, but tried :75,80 << on a file I had just to see if it would work (I hadn't read the page thisoldman refs above at the time) and it did. I don't know how often I'll use it, but if you know sed it's relatively easy to guess/remember the format as long as you remember the commands that can be tagged on.
Finally be careful with > >> as they are also used for file redirection if you use w - eg :75:80 w > lines75to80.txt
As I say though I'm still knew to this so test the above for my errors before using in anger. ![]()
"...one cannot be angry when one looks at a penguin." - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle
Offline
I also suggest you read trough this section of the vim docs:
:h text-objects
Last edited by Mr.Elendig (2012-03-15 10:55:05)
Evil #archlinux@libera.chat channel op and general support dude.
. files on github, Screenshots, Random pics and the rest
Offline
Thanks, hadn't learnt about the {} etc. blocks - which is going to be most useful.
"...one cannot be angry when one looks at a penguin." - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle
Offline
See also help on cindent and smartindent and associated settings.
"...one cannot be angry when one looks at a penguin." - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle
Offline
3. Change tab settings for a file
I created a html file with my default tab settings.
" Set spaces instead of tabs
" A tab consits of 4 spaces
set tabstop=4
" The indentation shift is limited to 4 spaces
set shiftwidth=4
" Prevent indentation errors in Python
set expandtabHence, the created html file has exactly these tab settings. Now I changed my config to use different tab settings for html files.
autocmd FileType html setlocal shiftwidth=2 tabstop=2However, when I open the document it still uses the old tab settings. How can I permanently change them to the new ones defined?
Regards
Offline
So the tabs have been converted to spaces, so your indentation consists of multiples of four spaces?
You can use ex search and replace commands to convert them to two spaces, but sed might be the best answer, esp if dealing with multiple files. The commands are similar, whichever you use.
"...one cannot be angry when one looks at a penguin." - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle
Offline